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