Skip to content

Commit

Permalink
Add property array values rendering in the PropertyRenderer (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster authored Nov 18, 2021
1 parent 9ac5162 commit 8fae316
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
27 changes: 25 additions & 2 deletions src/ConsoleRenderer/Renderer/PropertyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,37 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
{
$row = sprintf('%s: ', $formatter->title($this->title));

if (!array_key_exists($this->property, $schema)) {
if (!isset($schema[$this->property])) {
return $this->required ? $row . $formatter->error('not defined') : null;
}

$propertyValue = $schema[$this->property];

if (is_array($propertyValue)) {
if (count($propertyValue) >= 1) {
return $row . $this->convertArrayToString($formatter, $propertyValue);
}
$propertyValue = '[]';
}

return sprintf(
'%s%s',
$row,
$formatter->typecast($schema[$this->property])
$formatter->typecast($propertyValue)
);
}

private function convertArrayToString(Formatter $formatter, array $values): string
{
$string = implode(
"\n",
array_map(static fn ($property) => sprintf(
' %s%s',
$formatter->title(' '),
$formatter->typecast($property)
), $values)
);

return ltrim($string);
}
}
2 changes: 1 addition & 1 deletion src/OutputSchemaRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class OutputSchemaRenderer extends OutputRenderer
'ROLE' => 'Role',
'ENTITY' => 'Entity',
'MAPPER' => 'Mapper',
'SCOPE' => 'Constrain',
'SCOPE' => 'Scope',
'REPOSITORY' => 'Repository',
];

Expand Down
1 change: 1 addition & 0 deletions tests/Schema/Renderer/Fixture/console_output.stub.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
[Cycle\Schema\Renderer\Tests\Fixture\Tag] :: default.tag
Role: tag
Mapper: Cycle\ORM\Mapper\Mapper
App\FooMapper
Primary key: id, name
Fields:
(property -> db.field -> typecast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
[tag] :: default.tag
Entity: Cycle\Schema\Renderer\Tests\Fixture\Tag
Mapper: Cycle\ORM\Mapper\Mapper
App\FooMapper
Primary key: id, name
Fields:
(property -> db.field -> typecast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
[Cycle\Schema\Renderer\Tests\Fixture\Tag] :: default.tag
Role: tag
Mapper: Cycle\ORM\Mapper\Mapper
App\FooMapper
Primary key: id, name
Fields:
(property -> db.field -> typecast)
Expand Down
9 changes: 7 additions & 2 deletions tests/Schema/Renderer/OutputSchemaRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ protected function setUp(): void
],
Tag::class => [
SchemaInterface::ROLE => 'tag',
SchemaInterface::MAPPER => Mapper::class,
SchemaInterface::MAPPER => [
Mapper::class,
'App\FooMapper',
],
SchemaInterface::DATABASE => 'default',
SchemaInterface::TABLE => 'tag',
SchemaInterface::PRIMARY_KEY => ['id', 'name'],
Expand All @@ -82,7 +85,9 @@ protected function setUp(): void
],
TagContext::class => [
SchemaInterface::ROLE => 'tag_context',
SchemaInterface::MAPPER => Mapper::class,
SchemaInterface::MAPPER => [
Mapper::class,
],
SchemaInterface::DATABASE => 'default',
SchemaInterface::TABLE => 'tag_user_map',
SchemaInterface::COLUMNS => [],
Expand Down

0 comments on commit 8fae316

Please sign in to comment.