Passed
Pull Request — 1.x (#13)
by
unknown
25:31 queued 10:30
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
15
    /**
16
     * @var array
17
     */
18
    private array $rows = [];
19
20
    private const BLOCK = <<<BLOCK
21
        class %s {
22
            %s
23
        }
24
        BLOCK;
25
26
    public const INDENT = '    ';
27
28
    public function __construct(string $title)
29
    {
30
        $this->title = $title;
31
    }
32
33
    public function addRow(Row $row): void
34
    {
35
        $this->rows[] = $row;
36
    }
37
38
    public function addMethod(Method $method): void
39
    {
40
        $this->rows[] = $method;
41
    }
42
43
    public function addAnnotation(Annotation $annotation): void
44
    {
45
        \array_unshift($this->rows, $annotation);
46
    }
47
48
    public function __toString(): string
49
    {
50
        $rows = [];
51
52
        foreach ($this->rows as $row) {
53
            $rows[] = (string)$row;
54
        }
55
56
        $body = \implode("\n" . self::INDENT, $rows);
57
58
        return \sprintf(self::BLOCK, $this->title, $body);
59
    }
60
}
61