1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console\Input; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
|
7
|
|
|
abstract class Input |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Input. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $input; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Original input string. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $original; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Input name. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Input description. |
32
|
|
|
* |
33
|
|
|
* @var null|string |
34
|
|
|
*/ |
35
|
|
|
protected $description; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Array of input modes to apply. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $modeArray = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Argument mode. |
46
|
|
|
* |
47
|
|
|
* @var integer |
48
|
|
|
*/ |
49
|
|
|
protected $mode = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Construct. |
53
|
|
|
* |
54
|
|
|
* @param string $input |
55
|
|
|
*/ |
56
|
|
|
public function __construct($input) |
57
|
|
|
{ |
58
|
|
|
$this->input = $input; |
59
|
|
|
$this->original = $input; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Parse the set input string. |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
abstract public function parse(); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the input attributes. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
abstract public function getAttributes(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set array modeArray value if value contains *. |
78
|
|
|
* |
79
|
|
|
* @param string $constant |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
protected function setArray($constant) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
if (strpos($this->input, '*') !== false) { |
86
|
|
|
$this->modeArray[] = $constant; |
87
|
|
|
|
88
|
|
|
$this->input = str_replace('*', '', $this->input); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Parse an argument/option description. |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
protected function setDescription() |
100
|
|
|
{ |
101
|
|
|
if (strpos($this->input, ':') !== false) { |
102
|
|
|
$inputArray = array_map('trim', explode(':', $this->input)); |
103
|
|
|
|
104
|
|
|
$this->input = $inputArray[0]; |
105
|
|
|
|
106
|
|
|
$this->description = $inputArray[1]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Calculate the mode score. |
114
|
|
|
* |
115
|
|
|
* @param string $class |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
protected function calculateMode($class) |
120
|
|
|
{ |
121
|
|
|
foreach ($this->modeArray as $constant) { |
122
|
|
|
$this->mode = $this->mode | constant($class.'::'.$constant); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the input name as the input value. |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
protected function setName() |
134
|
|
|
{ |
135
|
|
|
$this->name = $this->input; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.