Passed
Push — develop ( e64509...b53b24 )
by nguereza
04:26
created

MakeEnumCommand::getPropertyTemplate()   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 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file MakeEnumCommand.php
34
 *
35
 *  The Make Enumeration Command class
36
 *
37
 *  @package    Platine\Framework\Console\Command
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Console\Command;
49
50
use Platine\Console\Input\Reader;
51
use Platine\Console\Output\Writer;
52
use Platine\Filesystem\Filesystem;
53
use Platine\Framework\App\Application;
54
use Platine\Framework\Console\MakeCommand;
55
use Platine\Stdlib\Helper\Str;
56
57
/**
58
 * @class MakeEnumCommand
59
 * @package Platine\Framework\Console\Command
60
 */
61
class MakeEnumCommand extends MakeCommand
62
{
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected string $type = 'enum';
67
68
    /**
69
     * The enumerations values
70
     * @var array<string, mixed>
71
     */
72
    protected array $enumerations = [];
73
74
    /**
75
     * Create new instance
76
     * @param Application $application
77
     * @param Filesystem $filesystem
78
     */
79
    public function __construct(
80
        Application $application,
81
        Filesystem $filesystem
82
    ) {
83
        parent::__construct($application, $filesystem);
84
        $this->setName('make:enum')
85
               ->setDescription('Command to generate new enumeration class');
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function interact(Reader $reader, Writer $writer): void
92
    {
93
        parent::interact($reader, $writer);
94
95
        $properties = [];
96
97
        $io = $this->io();
98
        $writer->boldYellow('Enter the enumeration list (empty value to finish):', true);
99
        $value = '';
100
        while ($value !== null) {
101
            $value = $io->prompt('Enum name', null, null, false);
102
103
            if (!empty($value)) {
104
                $value = trim($value);
105
                $name = preg_replace('#([^a-z0-9]+)#i', '_', $value);
106
                if ($name !== null && substr($name, -1) == '_') {
107
                    $name = substr($name, 0, -1);
108
                }
109
                if (is_string($name)) {
110
                    $properties[] = Str::upper($name);
111
                }
112
            }
113
        }
114
115
        if (!empty($properties)) {
116
            foreach ($properties as $name) {
117
                $value = $io->prompt(
118
                    sprintf('Enumeration value for [%s]', $name),
119
                    null,
120
                    null,
121
                    false
122
                );
123
                if (!empty($value)) {
124
                    $this->enumerations[$name] = $value;
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getClassTemplate(): string
134
    {
135
        return <<<EOF
136
        <?php
137
        
138
        declare(strict_types=1);
139
        
140
        namespace %namespace%;
141
        
142
        %uses%
143
144
        /**
145
        * @class %classname%
146
        * @package %namespace%
147
        */
148
        class %classname%
149
        {
150
            %enumerations%
151
        }
152
        
153
        EOF;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    protected function createClass(): string
160
    {
161
        $content = parent::createClass();
162
163
        return $this->getEnumerationBody($content);
164
    }
165
166
    /**
167
     * Return the enumerations body
168
     * @param string $content
169
     * @return string
170
     */
171
    protected function getEnumerationBody(string $content): string
172
    {
173
        $result = '';
174
        foreach ($this->enumerations as $name => $value) {
175
            $result .= $this->getEnumerationTemplate($name, $value);
176
        }
177
178
        return str_replace('%enumerations%', $result, $content);
179
    }
180
181
    /**
182
     * Return the enumeration template
183
     * @param string $name
184
     * @param string $value
185
     * @return string
186
     */
187
    protected function getEnumerationTemplate(string $name, string $value): string
188
    {
189
        return <<<EOF
190
        public const $name = '$value';
191
            
192
        EOF;
193
    }
194
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    protected function getUsesContent(): string
200
    {
201
        return '';
202
    }
203
}
204