Passed
Pull Request — master (#47)
by
unknown
24:31 queued 09:29
created

TsEnumGenerator::generateItemRows()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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