1 | <?php |
||
12 | abstract class AbstractOptions |
||
13 | { |
||
14 | /** |
||
15 | * Constructor |
||
16 | * |
||
17 | * @param array|Traversable|null $options |
||
18 | * @throws Exception\InvalidArgumentException |
||
19 | * @throws Exception\BadMethodCallException |
||
20 | */ |
||
21 | 25 | public function __construct($options = null) |
|
27 | |||
28 | /** |
||
29 | * Set one or more configuration properties |
||
30 | * |
||
31 | * @param array|Traversable|AbstractOptions $options |
||
32 | * @throws Exception\InvalidArgumentException |
||
33 | * @return AbstractOptions Provides fluent interface |
||
34 | * @throws Exception\BadMethodCallException |
||
35 | */ |
||
36 | 24 | public function setFromArray($options) |
|
37 | { |
||
38 | 24 | if ($options instanceof self) { |
|
39 | $options = $options->toArray(); |
||
40 | } |
||
41 | |||
42 | 24 | if (!is_array($options) && !$options instanceof Traversable) { |
|
43 | throw new Exception\InvalidArgumentException( |
||
44 | sprintf( |
||
45 | 'Parameter provided to %s must be an %s, %s or %s', |
||
46 | __METHOD__, |
||
47 | 'array', |
||
48 | 'Traversable', |
||
49 | 'Zend\Stdlib\AbstractOptions' |
||
50 | ) |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | 24 | foreach ($options as $key => $value) { |
|
55 | 24 | $this->__set($key, $value); |
|
56 | } |
||
57 | |||
58 | 24 | return $this; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Cast to array |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public function toArray() |
||
79 | |||
80 | /** |
||
81 | * Set a configuration property |
||
82 | * |
||
83 | * @see ParameterObject::__set() |
||
84 | * @param string $key |
||
85 | * @param mixed $value |
||
86 | * @throws Exception\BadMethodCallException |
||
87 | * @return void |
||
88 | */ |
||
89 | 24 | public function __set($key, $value) |
|
106 | |||
107 | /** |
||
108 | * Get a configuration property |
||
109 | * |
||
110 | * @see ParameterObject::__get() |
||
111 | * @param string $key |
||
112 | * @throws Exception\BadMethodCallException |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public function __get($key) |
||
129 | |||
130 | /** |
||
131 | * Test if a configuration property is null |
||
132 | * @see ParameterObject::__isset() |
||
133 | * @param string $key |
||
134 | * @return bool |
||
135 | * @throws Exception\BadMethodCallException |
||
136 | */ |
||
137 | public function __isset($key) |
||
143 | |||
144 | /** |
||
145 | * Set a configuration property to NULL |
||
146 | * |
||
147 | * @see ParameterObject::__unset() |
||
148 | * @param string $key |
||
149 | * @throws Exception\InvalidArgumentException |
||
150 | * @return void |
||
151 | */ |
||
152 | public function __unset($key) |
||
165 | } |
||
166 |