Passed
Pull Request — 1.x (#13)
by
unknown
40:20 queued 25:26
created

EntityTable::addColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\MermaidRenderer\Entity;
6
7
use Cycle\Schema\Renderer\MermaidRenderer\Annotation;
8
use Cycle\Schema\Renderer\MermaidRenderer\Method;
9
use Cycle\Schema\Renderer\MermaidRenderer\Column;
10
11
final class EntityTable implements EntityInterface
12
{
13
    private const BLOCK = <<<BLOCK
14
        class %s {
15
            %s
16
        }
17
        BLOCK;
18
19
    public const INDENT = '    ';
20
21
    private string $title;
22
23
    /**
24
     * @var Annotation[]|Column[]|Method[]
25
     */
26
    private array $columns = [];
27
28
    public function __construct(string $title)
29
    {
30
        $this->title = $title;
31
    }
32
33
    public function addColumn(Column $column): void
34
    {
35
        $this->columns[] = $column;
36
    }
37
38
    public function addMethod(Method $method): void
39
    {
40
        $this->columns[] = $method;
41
    }
42
43
    public function addAnnotation(Annotation $annotation): void
44
    {
45
        \array_unshift($this->columns, $annotation);
46
    }
47
48
    public function __toString(): string
49
    {
50
        $columns = \array_map(function (\Stringable $column) {
51
            return (string) $column;
52
        }, $this->columns);
53
54
        $body = \implode("\n" . self::INDENT, $columns);
55
56
        return \sprintf(self::BLOCK, $this->title, $body);
57
    }
58
}
59