1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Equip\Command; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A general purpose value object for command options. |
7
|
|
|
* |
8
|
|
|
* When constructed, all required options must be passed. |
9
|
|
|
* |
10
|
|
|
* @since 1.3.0 |
11
|
|
|
*/ |
12
|
|
|
abstract class Options |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Check that all required options are defined, then hydrate. |
16
|
|
|
* |
17
|
|
|
* @param array $values |
18
|
|
|
*/ |
19
|
5 |
|
final public function __construct(array $values) |
20
|
|
|
{ |
21
|
|
|
// Remove any values that are invalid for this set of options |
22
|
5 |
|
$values = array_intersect_key( |
23
|
5 |
|
$values, |
24
|
5 |
|
array_flip($this->valid()) |
25
|
5 |
|
); |
26
|
|
|
|
27
|
5 |
|
$this->ensureRequired($values); |
28
|
|
|
|
29
|
4 |
|
foreach ($values as $key => $value) { |
30
|
4 |
|
$this->$key = $value; |
31
|
4 |
|
} |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get a list of all required options. |
36
|
|
|
* |
37
|
|
|
* return [ |
38
|
|
|
* 'email', |
39
|
|
|
* 'password', |
40
|
|
|
* ]; |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
abstract public function required(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get a list of all valid options. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
5 |
|
public function valid() |
52
|
|
|
{ |
53
|
5 |
|
return array_keys($this->toArray()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get all options as an array. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
5 |
|
public function toArray() |
62
|
|
|
{ |
63
|
5 |
|
return get_object_vars($this); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Provide read-only access to attributes. |
68
|
|
|
* |
69
|
|
|
* @param string $key |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
2 |
|
final public function __get($key) |
74
|
|
|
{ |
75
|
2 |
|
return $this->{$key}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Prevent modification. |
80
|
|
|
* |
81
|
|
|
* @throws ImmutableException |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
1 |
|
final public function __set($key, $value) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
throw ImmutableException::cannotModify($this); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Prevent modification. |
95
|
|
|
* |
96
|
|
|
* @throws ImmutableException |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
1 |
|
final public function __unset($key) |
|
|
|
|
103
|
|
|
{ |
104
|
1 |
|
throw ImmutableException::cannotModify($this); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Ensure that all required options are included in the given values. |
109
|
|
|
* |
110
|
|
|
* @param array $values |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* |
114
|
|
|
* @throws CommandException |
115
|
|
|
* If any required options have not been defined. |
116
|
|
|
*/ |
117
|
|
|
private function ensureRequired(array $values) |
118
|
|
|
{ |
119
|
5 |
|
$defined = array_filter($values, static function ($value) { |
120
|
4 |
|
return $value !== null; |
121
|
5 |
|
}); |
122
|
|
|
|
123
|
5 |
|
$missing = array_diff($this->required(), array_keys($defined)); |
124
|
|
|
|
125
|
5 |
|
if ($missing) { |
|
|
|
|
126
|
1 |
|
throw CommandException::missingOptions($missing); |
127
|
|
|
} |
128
|
4 |
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.