Passed
Pull Request — 1.x (#13)
by
unknown
43:32 queued 28:34
created

EntityTable::addMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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