1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console\Input; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputOption; |
6
|
|
|
|
7
|
|
|
class Option extends Input |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Option shortcut. |
11
|
|
|
* |
12
|
|
|
* @var null|string |
13
|
|
|
*/ |
14
|
|
|
protected $shortcut = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default option value. |
18
|
|
|
* |
19
|
|
|
* @var null|string |
20
|
|
|
*/ |
21
|
|
|
protected $default = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Parse the set input string. |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
public function parse() |
29
|
|
|
{ |
30
|
|
|
return $this->setDescription() |
31
|
|
|
->setArray('VALUE_IS_ARRAY') |
32
|
|
|
->setValue() |
33
|
|
|
->setEmptyMode() |
34
|
|
|
->setShortcut() |
35
|
|
|
->calculateMode(InputOption::class) |
36
|
|
|
->setName(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Parse option value and value default. |
41
|
|
|
* |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
protected function setValue() |
45
|
|
|
{ |
46
|
|
|
if (strpos($this->input, '=') !== false) { |
47
|
|
|
if (substr($this->input, -1) === '=') { |
48
|
|
|
$this->input = str_replace('=', '', $this->input); |
49
|
|
|
|
50
|
|
|
$this->modeArray[] = 'VALUE_REQUIRED'; |
51
|
|
|
} else { |
52
|
|
|
list($this->input, $this->default) = explode('=', $this->input); |
53
|
|
|
|
54
|
|
|
$this->modeArray[] = 'VALUE_OPTIONAL'; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* If mode is empty, set VALUE_NONE. |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
protected function setEmptyMode() |
67
|
|
|
{ |
68
|
|
|
$this->modeArray = empty($this->modeArray) ? ['VALUE_NONE'] : $this->modeArray; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set option shortcut. |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
protected function setShortcut() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if (strpos($this->input, '|') !== false) { |
81
|
|
|
list($this->shortcut, $this->input) = explode('|', $this->input); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the option attributes. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getAttributes() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'name' => $this->name, |
96
|
|
|
'shortcut' => $this->shortcut, |
97
|
|
|
'mode' => $this->mode, |
98
|
|
|
'description' => $this->description, |
99
|
|
|
'default' => $this->default, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.