Passed
Pull Request — 1.x (#13)
by
unknown
41:33 queued 26:35
created

EntityTable::addAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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