KeysRenderer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A render() 0 16 5
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 KeysRenderer implements Renderer
12
{
13
    private string $title;
14
    private int $key;
15
    private bool $required;
16
17
    public function __construct(
18
        int $key = SchemaInterface::PRIMARY_KEY,
19
        string $title = 'Primary key',
20
        bool $required = true
21
    ) {
22
        $this->title = $title;
23
        $this->key = $key;
24
        $this->required = $required;
25
    }
26
27
    public function render(Formatter $formatter, array $schema, string $role): ?string
28
    {
29
        $keys = $schema[$this->key] ?? null;
30
31
        $row = \sprintf('%s: ', $formatter->title($this->title));
32
33
        if ($keys === null || $keys === '' || $keys === []) {
34
            return $this->required ? $row . $formatter->error('not defined') : null;
35
        }
36
37
        $keys = \array_map(
38
            static fn (string $key) => $formatter->property($key),
39
            (array)$keys
40
        );
41
42
        return $row . \implode(', ', $keys);
43
    }
44
}
45