Passed
Pull Request — 1.x (#13)
by
unknown
12:35
created

EntityTable::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 0
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\Row;
10
11
final class EntityTable implements EntityInterface
12
{
13
    private string $title;
14
    private array $rows = [];
15
16
    private const BLOCK = <<<BLOCK
17
        class %s {
18
            %s
19
        }
20
        BLOCK;
21
22
    public const INDENT = '    ';
23
24
    public function __construct(string $title)
25
    {
26
        $this->title = $title;
27
    }
28
29
    public function addRow(Row $row): void
30
    {
31
        $this->rows[] = $row;
32
    }
33
34
    public function addMethod(Method $method): void
35
    {
36
        $this->rows[] = $method;
37
    }
38
39
    public function addAnnotation(Annotation $annotation): void
40
    {
41
        \array_unshift($this->rows, $annotation);
42
    }
43
44
    public function __toString(): string
45
    {
46
        $rows = [];
47
48
        foreach ($this->rows as $row) {
49
            $rows[] = (string)$row;
50
        }
51
52
        $body = \implode("\n" . self::INDENT, $rows);
53
54
        return \sprintf(self::BLOCK, $this->title, $body);
55
    }
56
}
57