ColumnsRenderer::render()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 45
rs 8.5546
cc 7
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
6
7
use Cycle\ORM\SchemaInterface;
8
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
9
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
10
11
class ColumnsRenderer implements Renderer
12
{
13
    public function render(Formatter $formatter, array $schema, string $role): ?string
14
    {
15
        $rows = [];
16
17
        $columns = $schema[SchemaInterface::COLUMNS] ?? [];
18
        $title = \sprintf('%s:', $formatter->title('Fields'));
19
20
        if (! \is_array($columns) || \count($columns) === 0) {
21
            return $title . ' ' . $formatter->error('not defined');
22
        }
23
24
        $padding = $formatter->title('') . '  ';
25
26
        $rows[] = $title;
27
28
        $rows[] = \sprintf(
29
            '%s(%s -> %s -> %s)',
30
            $padding,
31
            $formatter->property('property'),
32
            $formatter->column('db.field'),
33
            $formatter->typecast('typecast')
34
        );
35
36
        $types = $schema[SchemaInterface::TYPECAST] ?? [];
37
38
        foreach ($columns as $property => $field) {
39
            $typecast = $types[$property] ?? $types[$field] ?? null;
40
            $row = \sprintf(
41
                '%s%s -> %s',
42
                $padding,
43
                $formatter->property((string)$property),
44
                $formatter->column($field)
45
            );
46
47
            if ($typecast !== null && $typecast !== [] && $typecast !== '') {
48
                $row .= \sprintf(
49
                    ' -> %s',
50
                    $formatter->typecast(\implode('::', (array)$typecast))
51
                );
52
            }
53
54
            $rows[] = $row;
55
        }
56
57
        return \implode($formatter::LINE_SEPARATOR, $rows);
58
    }
59
}
60