CustomPropertiesRenderer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 22
c 0
b 0
f 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A printValue() 0 10 1
A __construct() 0 3 1
A render() 0 24 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
6
7
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
8
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
9
10
class CustomPropertiesRenderer implements Renderer
11
{
12
    private array $exclude;
13
14
    /**
15
     * @param  array<int, int>  $exclude  Values list that should be excluded
16
     */
17
    public function __construct(array $exclude)
18
    {
19
        $this->exclude = $exclude;
20
    }
21
22
    public function render(Formatter $formatter, array $schema, string $role): ?string
23
    {
24
        $customProperties = \array_diff(\array_keys($schema), $this->exclude);
25
26
        if ($customProperties === []) {
27
            return null;
28
        }
29
30
        $rows = [
31
            \sprintf('%s:', $formatter->title('Custom props')),
32
        ];
33
34
        foreach ($customProperties as $property) {
35
            $data = $schema[$property] ?? null;
36
37
            $rows[] = \sprintf(
38
                '%s%s: %s',
39
                $formatter->title(' '),
40
                $property,
41
                $formatter->typecast($this->printValue($data, $formatter))
42
            );
43
        }
44
45
        return \implode($formatter::LINE_SEPARATOR, $rows);
46
    }
47
48
    /**
49
     * @param mixed $value
50
     */
51
    private function printValue($value, Formatter $formatter): string
52
    {
53
        $data = \trim(\var_export($value, true), '\'');
54
        $data = \array_map(
55
            static fn (string $row): string => $formatter->title(' ') . $row,
56
            \explode("\n", $data)
57
        );
58
59
        return \ltrim(
60
            \implode("\n", $data)
61
        );
62
    }
63
}
64