Passed
Pull Request — 1.x (#12)
by
unknown
14:23
created

EntityTable::addMethod()   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 2
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 __toString(): string
39
    {
40
        $rows = [];
41
42
        foreach ($this->rows as $row) {
43
            $rows[] = implode(self::SEPARATOR, $row);
44
        }
45
46
        $body = implode("\n" . self::INDENT, $rows);
47
48
        return sprintf(self::BLOCK, $this->title, $body);
49
    }
50
}
51