Total Complexity | 7 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
10 | class PropertyRenderer implements Renderer |
||
11 | { |
||
12 | private int $property; |
||
13 | private string $title; |
||
14 | private bool $required; |
||
15 | |||
16 | public function __construct(int $property, string $title, bool $required = false) |
||
17 | { |
||
18 | $this->property = $property; |
||
19 | $this->title = $title; |
||
20 | $this->required = $required; |
||
21 | } |
||
22 | |||
23 | public function render(Formatter $formatter, array $schema, string $role): ?string |
||
24 | { |
||
25 | $row = \sprintf('%s: ', $formatter->title($this->title)); |
||
26 | |||
27 | if (! isset($schema[$this->property])) { |
||
28 | return $this->required ? $row . $formatter->error('not defined') : null; |
||
29 | } |
||
30 | |||
31 | $propertyValue = $schema[$this->property]; |
||
32 | |||
33 | if (\is_array($propertyValue)) { |
||
34 | if (\count($propertyValue) >= 1) { |
||
35 | return $row . $this->convertArrayToString($formatter, $propertyValue); |
||
36 | } |
||
37 | $propertyValue = '[]'; |
||
38 | } |
||
39 | |||
40 | return \sprintf( |
||
41 | '%s%s', |
||
42 | $row, |
||
43 | $formatter->typecast($propertyValue) |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | private function convertArrayToString(Formatter $formatter, array $values): string |
||
59 | } |
||
60 | } |
||
61 |