Skip to content
Snippets Groups Projects
Commit 41957682 authored by Jan Kuchař's avatar Jan Kuchař
Browse files

Merge branch 'reconstitute-param-type' into 'master'

generate array type in reconstitute()

See merge request !15
parents 1cc222e0 5acfddf4
No related branches found
Tags 0.7.1
1 merge request!15generate array type in reconstitute()
Pipeline #36756 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