Test Failed
Pull Request — master (#58)
by Alexander
04:54 queued 02:29
created

ControllerCommand::getViewsPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\Controller;
6
7
use Yiisoft\Strings\Inflector;
8
use Yiisoft\Strings\StringHelper;
9
use Yiisoft\Validator\Rule\Each;
10
use Yiisoft\Validator\Rule\Regex;
11
use Yiisoft\Validator\Rule\Required;
12
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
13
use Yiisoft\Yii\Gii\Validator\NewClassRule;
14
use Yiisoft\Yii\Gii\Validator\TemplateRule;
15
16
class ControllerCommand extends AbstractGeneratorCommand
17
{
18
    public function __construct(
19
        private string $controllerNamespace = 'App\\Controller',
20
        #[Required]
21
        #[Regex(
22
            pattern: '/^[A-Z][\w]*Controller$/',
23
            message: 'Only word characters are allowed, and the class name must start with a capital letter and end with "Controller".'
24
        )]
25
        #[NewClassRule]
26
        /**
27
         * @var string the controller class name
28
         */
29
        private string $controllerClass = '',
30
        /**
31
         * @var string|null the controller's views path
32
         */
33
        private ?string $viewsPath = null,
34
        #[Regex(
35
            pattern: '/^[\w\\\\]*$/',
36
            message: 'Only word characters and backslashes are allowed.',
37
            skipOnEmpty: true,
38
        )]
39
        /**
40
         * @var string|null the base class of the controller or null if no parent class present
41
         */
42
        private ?string $baseClass = null,
43
        #[Each([
44
            new Regex(
45
                pattern: '/^[a-z][a-z0-9\\-,\\s]*$/',
46
                message: 'Only a-z, 0-9, dashes (-), spaces and commas are allowed.'
47
            ),
48
        ])
49
        ]
50
        /**
51
         * @var string[] list of action IDs
52
         */
53
        private array $actions = ['index'],
54
        #[Required(message: 'A code template must be selected.')]
55
        #[TemplateRule]
56
        private string $template = 'default',
57
    ) {
58
        parent::__construct($template);
59
        sort($this->actions);
60
    }
61
62
    /**
63
     * @return string the controller ID
64
     */
65
    public function getControllerID(): string
66
    {
67
        $name = StringHelper::baseName($this->controllerClass);
68
        return (new Inflector())->pascalCaseToId(substr($name, 0, -10));
69
    }
70
71
    public function getControllerClass(): string
72
    {
73
        return $this->controllerClass;
74
    }
75
76
    public function getActions(): array
77
    {
78
        return $this->actions;
79
    }
80
81
    public function getViewsPath(): ?string
82
    {
83
        return $this->viewsPath;
84
    }
85
86
    public function getControllerNamespace(): string
87
    {
88
        return $this->controllerNamespace;
89
    }
90
91
    public function getBaseClass(): ?string
92
    {
93
        return $this->baseClass;
94
    }
95
}
96