Skip to content

Commit

Permalink
feat: generete multy line expecting for property
Browse files Browse the repository at this point in the history
- support multy line expecting for property (template generor)
  • Loading branch information
SonyPradana committed Nov 6, 2021
1 parent 4040e75 commit 75a58ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
24 changes: 19 additions & 5 deletions src/System/Template/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}
15 changes: 12 additions & 3 deletions tests/Template/BasicTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
9 changes: 7 additions & 2 deletions tests/Template/expected/class_with_complex_property
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

}

0 comments on commit 75a58ca

Please sign in to comment.