From 75a58cac90ad3056f37c56378fb71a7475418241 Mon Sep 17 00:00:00 2001 From: Angger Pradana Date: Sun, 7 Nov 2021 06:48:57 +0700 Subject: [PATCH] feat: generete multy line expecting for property - support multy line expecting for property (template generor) --- src/System/Template/Property.php | 24 +++++++++++++++---- tests/Template/BasicTemplateTest.php | 15 +++++++++--- .../expected/class_with_complex_property | 9 +++++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/System/Template/Property.php b/src/System/Template/Property.php index b69368c4..279e12f7 100644 --- a/src/System/Template/Property.php +++ b/src/System/Template/Property.php @@ -76,9 +76,17 @@ public function generate(): string $name = '$' . $this->name; // generate value or expecting - $expecting = $this->expecting == null - ? "" - : " " . $this->expecting; + $expecting = ""; + if ($this->expecting != null) { + $single_line = $this->expecting[0] ?? ""; + $multy_line = implode( + "\n" . $tab_dept(1), + array_filter($this->expecting, fn($key) => $key > 0, ARRAY_FILTER_USE_KEY) + ); + $expecting = count($this->expecting) > 1 + ? " " . $single_line . "\n" . $tab_dept(1) . $multy_line + : " " . $single_line; + } // final return str_replace( @@ -114,9 +122,15 @@ public function name(string $name) return $this; } - public function expecting(string $expecting) + /** + * @param string|array $expecting Add expecting as string or array for multy line + */ + public function expecting($expecting) { - $this->expecting = $expecting; + $this->expecting = is_array($expecting) + ? $expecting + : [$expecting]; + return $this; } } diff --git a/tests/Template/BasicTemplateTest.php b/tests/Template/BasicTemplateTest.php index 1808fa7a..d2fc6d42 100644 --- a/tests/Template/BasicTemplateTest.php +++ b/tests/Template/BasicTemplateTest.php @@ -122,9 +122,18 @@ public function it_can_generate_class_with_complex_propertys() $class ->addProperty('some_property') ->visibility(Property::PUBLIC_) - ->dataType('int') - ->expecting("= 1") - ->addVaribaleComment('int'); + ->dataType('array') + ->expecting( + [ + '= array(', + ' \'one\' => 1,', + ' \'two\' => 2,', + ' \'bool\' => false,', + ' \'string\' => \'string\'', + ')' + ] + ) + ->addVaribaleComment('array'); $this->assertEquals( $this->getExpected('class_with_complex_property'), diff --git a/tests/Template/expected/class_with_complex_property b/tests/Template/expected/class_with_complex_property index 87042ff7..a38a31f0 100644 --- a/tests/Template/expected/class_with_complex_property +++ b/tests/Template/expected/class_with_complex_property @@ -18,7 +18,12 @@ class NewClass private $test_7; private $test_8; private $test_9; - /** @var int */ - public int $some_property = 1; + /** @var array */ + public array $some_property = array( + 'one' => 1, + 'two' => 2, + 'bool' => false, + 'string' => 'string' + ); }