From 8fae3165eac5b37b91c9353a813b260405d4534b Mon Sep 17 00:00:00 2001 From: Pavel Buchnev Date: Thu, 18 Nov 2021 20:23:04 +0300 Subject: [PATCH] Add property array values rendering in the PropertyRenderer (#5) --- .../Renderer/PropertyRenderer.php | 27 +++++++++++++++++-- src/OutputSchemaRenderer.php | 2 +- .../Renderer/Fixture/console_output.stub.txt | 1 + .../Fixture/console_output_plain.stub.txt | 1 + ...ole_output_with_custom_properties.stub.txt | 1 + .../Renderer/OutputSchemaRendererTest.php | 9 +++++-- 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/ConsoleRenderer/Renderer/PropertyRenderer.php b/src/ConsoleRenderer/Renderer/PropertyRenderer.php index 9416c8f..daddc80 100644 --- a/src/ConsoleRenderer/Renderer/PropertyRenderer.php +++ b/src/ConsoleRenderer/Renderer/PropertyRenderer.php @@ -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); } } diff --git a/src/OutputSchemaRenderer.php b/src/OutputSchemaRenderer.php index 556abcd..fb6c7e1 100644 --- a/src/OutputSchemaRenderer.php +++ b/src/OutputSchemaRenderer.php @@ -28,7 +28,7 @@ final class OutputSchemaRenderer extends OutputRenderer 'ROLE' => 'Role', 'ENTITY' => 'Entity', 'MAPPER' => 'Mapper', - 'SCOPE' => 'Constrain', + 'SCOPE' => 'Scope', 'REPOSITORY' => 'Repository', ]; diff --git a/tests/Schema/Renderer/Fixture/console_output.stub.txt b/tests/Schema/Renderer/Fixture/console_output.stub.txt index 376d08b..8476e6d 100644 --- a/tests/Schema/Renderer/Fixture/console_output.stub.txt +++ b/tests/Schema/Renderer/Fixture/console_output.stub.txt @@ -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) diff --git a/tests/Schema/Renderer/Fixture/console_output_plain.stub.txt b/tests/Schema/Renderer/Fixture/console_output_plain.stub.txt index 93a9f2b..76f3283 100644 --- a/tests/Schema/Renderer/Fixture/console_output_plain.stub.txt +++ b/tests/Schema/Renderer/Fixture/console_output_plain.stub.txt @@ -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) diff --git a/tests/Schema/Renderer/Fixture/console_output_with_custom_properties.stub.txt b/tests/Schema/Renderer/Fixture/console_output_with_custom_properties.stub.txt index ef96217..9db7b97 100644 --- a/tests/Schema/Renderer/Fixture/console_output_with_custom_properties.stub.txt +++ b/tests/Schema/Renderer/Fixture/console_output_with_custom_properties.stub.txt @@ -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) diff --git a/tests/Schema/Renderer/OutputSchemaRendererTest.php b/tests/Schema/Renderer/OutputSchemaRendererTest.php index 8affa67..8f07f23 100644 --- a/tests/Schema/Renderer/OutputSchemaRendererTest.php +++ b/tests/Schema/Renderer/OutputSchemaRendererTest.php @@ -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'], @@ -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 => [],