1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Filter.php |
4
|
|
|
* |
5
|
|
|
* PHP version 7.3 and up. |
6
|
|
|
* |
7
|
|
|
* @category Core |
8
|
|
|
* @package Redbox_Cli |
9
|
|
|
* @author Johnny Mast <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
11
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
12
|
|
|
* @since 1.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Redbox\Cli\Arguments; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Okay its hard to explain this one you dot not know array_filter. |
19
|
|
|
* What it does (this class) |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Redbox_Cli |
23
|
|
|
* @author Johnny Mast <[email protected]> |
24
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
25
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
|
|
class Filter |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* An array of arguments passed to the program. |
32
|
|
|
* |
33
|
|
|
* @var Argument[] $arguments |
34
|
|
|
*/ |
35
|
|
|
protected $arguments = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the arguments to filter. |
39
|
|
|
* |
40
|
|
|
* @param array $arguments The arguments. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
9 |
|
public function setArguments($arguments = []): void |
45
|
|
|
{ |
46
|
9 |
|
$this->arguments = $arguments; |
47
|
9 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Callback function to check if the argument has a short prefix. |
51
|
|
|
* |
52
|
|
|
* @param \Redbox\Cli\Arguments\Argument $argument The argument to check. |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
9 |
|
public function hasShortPrefix($argument) |
57
|
|
|
{ |
58
|
9 |
|
return ($argument->prefix); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Callback function to check if the argument has a long prefix. |
63
|
|
|
* |
64
|
|
|
* @param \Redbox\Cli\Arguments\Argument $argument The argument to check. |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
9 |
|
public function hasLongPrefix($argument) |
69
|
|
|
{ |
70
|
9 |
|
return ($argument->prefix); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Callback function to check if the argument is required. |
75
|
|
|
* |
76
|
|
|
* @param \Redbox\Cli\Arguments\Argument $argument The argument to check. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
2 |
|
protected function isRequired($argument): bool |
81
|
|
|
{ |
82
|
2 |
|
return ($argument->required == true); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Callback function to check if the argument does not had the required option. |
87
|
|
|
* |
88
|
|
|
* @param \Redbox\Cli\Arguments\Argument $argument The argument to check. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
2 |
|
protected function isOptional($argument): bool |
93
|
|
|
{ |
94
|
2 |
|
return ($argument->required == false); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return all required arguments, these are arguments with required => true, |
99
|
|
|
* |
100
|
|
|
* @return Argument[] arguments with arguments set required to true |
101
|
|
|
*/ |
102
|
2 |
|
public function required(): array |
103
|
|
|
{ |
104
|
2 |
|
return $this->filterArguments(['isRequired']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return all arguments without required => true. |
109
|
|
|
* |
110
|
|
|
* @return Argument[] arguments with arguments set required to false |
111
|
|
|
*/ |
112
|
2 |
|
public function optional(): array |
113
|
|
|
{ |
114
|
2 |
|
return $this->filterArguments(['isOptional']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return an array with short prefixes. |
119
|
|
|
* |
120
|
|
|
* @return Argument[] arguments with a short prefix (e.x -u) |
121
|
|
|
*/ |
122
|
9 |
|
public function withShortPrefix(): array |
123
|
|
|
{ |
124
|
9 |
|
return $this->filterArguments(['hasShortPrefix']); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return an array with long prefixes |
129
|
|
|
* |
130
|
|
|
* @return Argument[] required arguments (e.x --user) |
131
|
|
|
*/ |
132
|
9 |
|
public function withLongPrefix(): array |
133
|
|
|
{ |
134
|
9 |
|
return $this->filterArguments(['hasLongPrefix']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* This function will do the actual filtering. Call backs for array_filter |
139
|
|
|
* will be in this function for example isRequired. |
140
|
|
|
* |
141
|
|
|
* @param array $filters The filters for the arguments array. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
9 |
|
protected function filterArguments($filters = []): array |
146
|
|
|
{ |
147
|
9 |
|
$arguments = $this->arguments; |
148
|
|
|
|
149
|
9 |
|
foreach ($filters as $filter) { |
150
|
9 |
|
$arguments = array_filter($arguments, [$this, $filter]); |
151
|
|
|
} |
152
|
9 |
|
return array_values($arguments); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|