Passed
Push — master ( a1297c...2fe29a )
by Dmitry
13:33
created

TsEnumGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace hipanel\rbac\console\converter;
5
6
use hipanel\rbac\console\converter\formatter\DefaultEnumRowItemBuilder;
7
use hipanel\rbac\console\converter\formatter\EnumRowItemBuilderInterface;
8
use Laminas\Code\Generator\AbstractGenerator;
9
10
final class TsEnumGenerator extends AbstractGenerator
11
{
12
    private string $name;
13
    private array $items;
14
15
    private EnumRowItemBuilderInterface $rowFormatter;
16
17
    public function __construct(string $name, array $items)
18
    {
19
        $this->name = $name;
20
        $this->items = $items;
21
22
        $this->rowFormatter = new DefaultEnumRowItemBuilder();
23
    }
24
25
    public function withRowFormatter(EnumRowItemBuilderInterface $formatter): self
26
    {
27
        $this->rowFormatter = $formatter;
28
29
        return $this;
30
    }
31
32
    public function generate(): string
33
    {
34
        return sprintf(<<<JS
35
export enum %s%s{
36
%s
37
};
38
JS
39
        , $this->name, $this->indentation, $this->generateItemRows());
40
    }
41
42
    private function generateItemRows(): string
43
    {
44
        $res = [];
45
        foreach ($this->items as $item) {
46
            $res[] = $this->rowFormatter->build($item);
47
        }
48
49
        return implode(self::LINE_FEED, $res);
50
    }
51
}
52