Commit 66d1ad8c authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Simplification des tests de tableau.

parent e820f98b
......@@ -21,6 +21,8 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Doctrine\Common\Inflector\Inflector;
/**
* Class ArrayType.
*/
......@@ -47,9 +49,10 @@ class ArrayType extends AbstractCollection
*/
public function castToStringOrStringArray(string $expr): string
{
$cast = $this->valueType->castToStringOrStringArray('x');
if ($cast !== 'x') {
return sprintf('%s.map(x => %s)', $expr, $cast);
$itemName = $this->getItemName();
$cast = $this->valueType->castToStringOrStringArray( $itemName);
if ($cast !== $itemName) {
return sprintf('%s.map(%s => %s)', $expr, $itemName, $cast);
}
return $expr;
......@@ -60,11 +63,23 @@ class ArrayType extends AbstractCollection
*/
public function checkType(string $expr, bool $explicit = false): string
{
$itemName = $this->getItemName();
return sprintf(
'(Array.isArray(%s) && %s.findIndex(x => !%s) === -1)',
'(Array.isArray(%s) && %s.every(%s => %s))',
$expr,
$expr,
$this->valueType->checkType('x', $explicit)
$itemName,
$this->valueType->checkType($itemName, $explicit)
);
}
/**
* @return string
*/
private function getItemName(): string
{
$valueUsage = $this->getValueType()->getUsage();
$valueName = strtr($valueUsage, '[]<>', '[]__');
return Inflector::singularize(Inflector::camelize( $valueName));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment