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
|
|
|
* Provide read-only access to attributes. |
29
|
|
|
* |
30
|
|
|
* @param string $key |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
2 |
|
final public function __get($key) |
35
|
|
|
{ |
36
|
2 |
|
return $this->{$key}; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Prevent modification. |
41
|
|
|
* |
42
|
|
|
* @throws ImmutableException |
43
|
|
|
* |
44
|
|
|
* @param string $key |
45
|
|
|
* @param mixed $value |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
1 |
|
final public function __set($key, $value) |
|
|
|
|
50
|
|
|
{ |
51
|
1 |
|
throw ImmutableException::cannotModify($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Prevent modification. |
56
|
|
|
* |
57
|
|
|
* @throws ImmutableException |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
1 |
|
final public function __unset($key) |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
throw ImmutableException::cannotModify($this); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Ensure that all required options are included in the given values. |
70
|
|
|
* |
71
|
|
|
* @param array $values |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
* |
75
|
|
|
* @throws CommandException |
76
|
|
|
* If any required options have not been defined. |
77
|
|
|
*/ |
78
|
|
|
private function ensureRequired(array $values) |
79
|
|
|
{ |
80
|
5 |
|
$defined = array_filter($values, static function ($value) { |
81
|
4 |
|
return $value !== null; |
82
|
5 |
|
}); |
83
|
|
|
|
84
|
5 |
|
$missing = array_diff($this->required(), array_keys($defined)); |
|
|
|
|
85
|
|
|
|
86
|
5 |
|
if ($missing) { |
|
|
|
|
87
|
1 |
|
throw CommandException::missingOptions($missing); |
88
|
|
|
} |
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
5 |
|
public function valid() |
95
|
|
|
{ |
96
|
5 |
|
return array_keys($this->toArray()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
5 |
|
public function toArray() |
103
|
|
|
{ |
104
|
5 |
|
return get_object_vars($this); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.