MakeEnumCommand::interact()   B
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
c 4
b 0
f 0
nc 24
nop 2
dl 0
loc 34
rs 8.0555
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
85
        $this->setName('make:enum')
86
               ->setDescription('Command to generate new enumeration class');
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function interact(Reader $reader, Writer $writer): void
93
    {
94
        parent::interact($reader, $writer);
95
96
        $properties = [];
97
98
        $io = $this->io();
99
        $writer->boldYellow('Enter the enumeration list (empty value to finish):', true);
100
        $value = '';
101
        while ($value !== null) {
102
            $value = $io->prompt('Enum name', null, null, false);
103
104
            if (!empty($value)) {
105
                $value = trim($value);
106
                $name = preg_replace('#([^a-z0-9]+)#i', '_', $value);
107
                if ($name !== null && substr($name, -1) === '_') {
108
                    $name = substr($name, 0, -1);
109
                }
110
                if (is_string($name)) {
111
                    $properties[] = Str::upper($name);
112
                }
113
            }
114
        }
115
116
        if (count($properties) > 0) {
117
            foreach ($properties as $name) {
118
                $value = $io->prompt(
119
                    sprintf('Enumeration value for [%s]', $name),
120
                    null,
121
                    null,
122
                    false
123
                );
124
                if (!empty($value)) {
125
                    $this->enumerations[$name] = $value;
126
                }
127
            }
128
        }
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getClassTemplate(): string
135
    {
136
        return <<<EOF
137
        <?php
138
        
139
        declare(strict_types=1);
140
        
141
        namespace %namespace%;
142
        
143
        %uses%
144
145
        /**
146
        * @class %classname%
147
        * @package %namespace%
148
        */
149
        class %classname%
150
        {
151
            %enumerations%
152
        }
153
        
154
        EOF;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    protected function createClass(): string
161
    {
162
        $content = parent::createClass();
163
164
        return $this->getEnumerationBody($content);
165
    }
166
167
    /**
168
     * Return the enumerations body
169
     * @param string $content
170
     * @return string
171
     */
172
    protected function getEnumerationBody(string $content): string
173
    {
174
        $result = '';
175
        foreach ($this->enumerations as $name => $value) {
176
            $result .= $this->getEnumerationTemplate($name, $value);
177
        }
178
179
        return str_replace('%enumerations%', $result, $content);
180
    }
181
182
    /**
183
     * Return the enumeration template
184
     * @param string $name
185
     * @param string $value
186
     * @return string
187
     */
188
    protected function getEnumerationTemplate(string $name, string $value): string
189
    {
190
        return <<<EOF
191
        public const $name = '$value';
192
            
193
        EOF;
194
    }
195
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    protected function getUsesContent(): string
201
    {
202
        return '';
203
    }
204
}
205