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