Skip to content
Snippets Groups Projects
Verified Commit 5acfddf4 authored by Jiří Pudil's avatar Jiří Pudil
Browse files

generate array type in reconstitute()

parent 1cc222e0
No related branches found
No related tags found
1 merge request!15generate array type in reconstitute()
Pipeline #36753 passed
......@@ -5,8 +5,7 @@ namespace Grifart\Tables\Scaffolding;
use Grifart\ClassScaffolder\Capabilities\Capability;
use Grifart\ClassScaffolder\ClassInNamespace;
use Grifart\ClassScaffolder\Definition\ClassDefinition;
use Grifart\ClassScaffolder\Definition\Field;
use Nette\PhpGenerator as Code;
use Nette\PhpGenerator\PhpLiteral;
final class ReconstituteConstructor implements Capability
{
......@@ -20,13 +19,21 @@ final class ReconstituteConstructor implements Capability
$reconstitute = $classType->addMethod('reconstitute')
->setReturnType('static')
->setParameters([(new Code\Parameter('values'))->setTypeHint('array')])
->setStatic();
$reconstitute->addParameter('values')->setTypeHint('array');
$fields = $definition->getFields();
$questionMarks = \implode(', ', array_fill(0, \count($fields), '?'));
$literals = \array_map(function(Field $field): Code\PhpLiteral {return new Code\PhpLiteral("\$values['" . $field->getName() . "']");}, $fields);
$shapeFields = $literals = [];
foreach ($fields as $field) {
$name = $field->getName();
$type = $field->getType();
$shapeFields[] = \sprintf('%s: %s', $name, $type->getDocCommentType($draft->getNamespace()));
$literals[] = new PhpLiteral("\$values['" . $name . "']");
}
$reconstitute->addBody("return new static($questionMarks);", \array_values($literals));
$reconstitute->addComment(\sprintf('@param array{%s} $values', \implode(', ', $shapeFields)));
$reconstitute->addBody("return new static(...?);", [$literals]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment