Passed
Push — develop ( ac3052...955933 )
by nguereza
04:41
created

MakeFormParamCommand::getSetterTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.9332
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 MakeFormParamCommand.php
34
 *
35
 *  The Make form parameter 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   http://www.iacademy.cf
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 MakeFormParamCommand
59
 * @package Platine\Framework\Console\Command
60
 */
61
class MakeFormParamCommand extends MakeCommand
62
{
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected string $type = 'form parameter';
67
68
    /**
69
     * Whether to create instance from entity
70
     * @var bool
71
     */
72
    protected bool $createInstanceFormEntity = false;
73
74
    /**
75
     * The list of properties entity field maps
76
     * @var array<string, string>
77
     */
78
    protected array $fromEntityMaps = [];
79
80
    /**
81
     * Create new instance
82
     * @param Application $application
83
     * @param Filesystem $filesystem
84
     */
85
    public function __construct(
86
        Application $application,
87
        Filesystem $filesystem
88
    ) {
89
        parent::__construct($application, $filesystem);
90
        $this->setName('make:param')
91
               ->setDescription('Command to generate new form parameter class');
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function interact(Reader $reader, Writer $writer): void
98
    {
99
        parent::interact($reader, $writer);
100
101
        $properties = [];
102
103
        $io = $this->io();
104
        $writer->boldYellow('Enter the properties list (empty value to finish):', true);
105
        $value = '';
106
        while ($value !== null) {
107
            $value = $io->prompt('Property name', null, null, false);
108
109
            if (!empty($value)) {
110
                $value = trim($value);
111
                $name = Str::camel($value, true);
112
113
                $properties[$name] = [
114
                    'name' => $name,
115
                    'short' => 'string',
116
                ];
117
            }
118
        }
119
120
        $this->properties = $properties;
121
122
        if (!empty($this->properties)) {
123
            $this->createInstanceFormEntity = $io->confirm('Create instance from entity ?', 'y');
124
125
            if ($this->createInstanceFormEntity) {
126
                $list = [];
127
                foreach ($this->properties as $name => $info) {
128
                    $value = $io->prompt(
129
                        sprintf('Entity field name for [%s] (just enter to ignore)', $name),
130
                        null,
131
                        null,
132
                        false
133
                    );
134
                    if (!empty($value)) {
135
                        $list[$name] = $value;
136
                    }
137
                }
138
139
                if (!empty($list)) {
140
                    $this->fromEntityMaps = $list;
141
                }
142
            }
143
        }
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getClassTemplate(): string
150
    {
151
        return <<<EOF
152
        <?php
153
        
154
        declare(strict_types=1);
155
        
156
        namespace %namespace%;
157
        
158
        use Platine\Framework\Form\Param\BaseParam;
159
        %uses%
160
161
        /**
162
        * @class %classname%
163
        * @package %namespace%
164
        */
165
        class %classname% extends BaseParam
166
        {
167
            %properties%
168
            %from_entity%
169
            %getters%
170
            %setters%
171
        }
172
        
173
        EOF;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    protected function createClass(): string
180
    {
181
        $content = parent::createClass();
182
183
        $fromEntity = $this->getFromEntityBody($content);
184
        $setters = $this->getSettersBody($fromEntity);
185
186
        return $this->getGettersBody($setters);
187
    }
188
189
    /**
190
     * Return the setters body
191
     * @param string $content
192
     * @return string
193
     */
194
    protected function getSettersBody(string $content): string
195
    {
196
        $result = '';
197
        foreach ($this->properties as $info) {
198
            $result .= $this->getSetterTemplate($info);
199
        }
200
201
        return str_replace('%setters%', $result, $content);
202
    }
203
204
    /**
205
     * Return the getter body
206
     * @param string $content
207
     * @return string
208
     */
209
    protected function getGettersBody(string $content): string
210
    {
211
        $result = '';
212
213
        foreach ($this->properties as $info) {
214
            $result .= $this->getGetterTemplate($info);
215
        }
216
217
        return str_replace('%getters%', $result, $content);
218
    }
219
220
    /**
221
     * Return the setter template
222
     * @param array<string, string> $info
223
     * @return string
224
     */
225
    protected function getSetterTemplate(array $info): string
226
    {
227
        $name = $info['name'];
228
        $cleanName = Str::snake($name, ' ');
229
        $setterName = 'set' . Str::ucfirst($name);
230
231
        return <<<EOF
232
        /**
233
            * Set the $cleanName value 
234
            * @param string \$$name 
235
            * @return self
236
            */
237
           public function $setterName(string \$$name): self
238
           {
239
               \$this->$name = \$$name;
240
                
241
               return \$this;
242
           }
243
           
244
           
245
        EOF;
246
    }
247
248
    /**
249
     * Return the getter template
250
     * @param array<string, string> $info
251
     * @return string
252
     */
253
    protected function getGetterTemplate(array $info): string
254
    {
255
        $name = $info['name'];
256
        $cleanName = Str::snake($name, ' ');
257
        $getterName = 'get' . Str::ucfirst($name);
258
259
        return <<<EOF
260
        /**
261
            * Return the $cleanName value 
262
            * @return string
263
            */
264
           public function $getterName(): string
265
           {
266
               return \$this->$name;
267
           }
268
           
269
           
270
        EOF;
271
    }
272
273
    /**
274
     * Return the from entity body
275
     * @param string $content
276
     * @return string
277
     */
278
    protected function getFromEntityBody(string $content): string
279
    {
280
        $result = '';
281
        if ($this->createInstanceFormEntity && !empty($this->fromEntityMaps)) {
282
            $result = <<<EOF
283
            /**
284
                * {@inheritdoc}
285
                */
286
               public function fromEntity(Entity \$entity): self
287
               {
288
                
289
            EOF;
290
            foreach ($this->fromEntityMaps as $property => $map) {
291
                $result .= <<<EOF
292
                \$this->$property = \$entity->$map;
293
                
294
            EOF;
295
            }
296
297
            $result .= <<<EOF
298
                
299
                   return \$this;
300
               }
301
            
302
            EOF;
303
        }
304
305
        return str_replace('%from_entity%', $result, $content);
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    protected function getPropertyTemplate(string $className, array $info): string
312
    {
313
        $shortClass = $info['short'];
314
        $name = $info['name'];
315
        $cleanName = Str::snake($name, ' ');
316
317
        return <<<EOF
318
        /**
319
            * The $cleanName field
320
            * @var $shortClass
321
            */
322
            protected $shortClass \$$name = '';
323
        
324
            
325
        EOF;
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331
    protected function getUsesContent(): string
332
    {
333
        if (!$this->createInstanceFormEntity) {
334
            return '';
335
        }
336
337
        return <<<EOF
338
        use Platine\Orm\Entity; 
339
        
340
        EOF;
341
    }
342
}
343