Command::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 26
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\ActiveRecord;
6
7
use Yiisoft\ActiveRecord\ActiveRecord;
8
use Yiisoft\Strings\Inflector;
9
use Yiisoft\Validator\Rule\Regex;
10
use Yiisoft\Validator\Rule\Required;
11
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
12
use Yiisoft\Yii\Gii\Validator\ClassExistsRule;
13
use Yiisoft\Yii\Gii\Validator\TableExistsRule;
14
use Yiisoft\Yii\Gii\Validator\TemplateRule;
15
16
final class Command extends AbstractGeneratorCommand
17
{
18 4
    public function __construct(
19
        #[Required]
20
        #[Regex(
21
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
22
            message: 'Invalid namespace'
23
        )]
24
        private readonly string $namespace = 'App\\Model',
25
        #[Required]
26
        #[Regex(
27
            pattern: '/^[a-z_][a-z0-9_]*$/i',
28
            message: 'Invalid table name'
29
        )]
30
        #[TableExistsRule]
31
        private readonly string $tableName = 'user',
32
        #[Regex(
33
            pattern: '/^[a-z\\\\]*$/i',
34
            message: 'Only word characters and backslashes are allowed.',
35
            skipOnEmpty: true,
36
        )]
37
        #[ClassExistsRule]
38
        private readonly string $baseClass = ActiveRecord::class,
39
        #[Required(message: 'A code template must be selected.')]
40
        #[TemplateRule]
41
        protected string $template = 'default',
42
    ) {
43 4
        parent::__construct($template);
44
    }
45
46 2
    public function getNamespace(): string
47
    {
48 2
        return $this->namespace;
49
    }
50
51 2
    public function getBaseClass(): string
52
    {
53 2
        return $this->baseClass;
54
    }
55
56 2
    public function getTableName(): string
57
    {
58 2
        return $this->tableName;
59
    }
60
61 2
    public function getModelName(): string
62
    {
63 2
        return (new Inflector())->tableToClass($this->tableName);
64
    }
65
66
    public static function getAttributeLabels(): array
67
    {
68
        return [
69
            'namespace' => 'Model namespace',
70
            'baseClass' => 'Base class',
71
            'tableName' => 'Table name',
72
            'template' => 'Template',
73
        ];
74
    }
75
76
    public static function getHints(): array
77
    {
78
        return [
79
            'namespace' => 'Namespace for the model class to store it in the related directory.',
80
            'baseClass' => 'Parent class for the new model class.',
81
            'tableName' => 'Corresponded table name for the model class.',
82
        ];
83
    }
84
85
    public static function getAttributes(): array
86
    {
87
        return [
88
            'namespace',
89
            'tableName',
90
            'baseClass',
91
            'template',
92
        ];
93
    }
94
}
95