1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AMQPAL; |
4
|
|
|
|
5
|
|
|
use Traversable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AbstractOptions |
9
|
|
|
* |
10
|
|
|
* @package AMQPAL\Adapter |
11
|
|
|
*/ |
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) |
22
|
|
|
{ |
23
|
25 |
|
if (null !== $options) { |
24
|
20 |
|
$this->setFromArray($options); |
25
|
|
|
} |
26
|
25 |
|
} |
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() |
67
|
|
|
{ |
68
|
|
|
$array = []; |
69
|
|
|
$transform = function ($letters) { |
70
|
|
|
$letter = array_shift($letters); |
71
|
|
|
return '_' . strtolower($letter); |
72
|
|
|
}; |
73
|
|
|
foreach ($this as $key => $value) { |
|
|
|
|
74
|
|
|
$normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key); |
75
|
|
|
$array[$normalizedKey] = $value; |
76
|
|
|
} |
77
|
|
|
return $array; |
78
|
|
|
} |
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) |
90
|
|
|
{ |
91
|
24 |
|
$setter = 'set' . str_replace('_', '', $key); |
92
|
|
|
|
93
|
24 |
|
if (is_callable([$this, $setter])) { |
94
|
24 |
|
$this->{$setter}($value); |
95
|
|
|
|
96
|
24 |
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new Exception\BadMethodCallException(sprintf( |
100
|
|
|
'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined', |
101
|
|
|
$key, |
102
|
|
|
'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))), |
103
|
|
|
$setter |
104
|
|
|
)); |
105
|
|
|
} |
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) |
116
|
|
|
{ |
117
|
|
|
$getter = 'get' . str_replace('_', '', $key); |
118
|
|
|
|
119
|
|
|
if (is_callable([$this, $getter])) { |
120
|
|
|
return $this->{$getter}(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw new Exception\BadMethodCallException(sprintf( |
124
|
|
|
'The option "%s" does not have a callable "%s" getter method which must be defined', |
125
|
|
|
$key, |
126
|
|
|
'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))) |
127
|
|
|
)); |
128
|
|
|
} |
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) |
138
|
|
|
{ |
139
|
|
|
$getter = 'get' . str_replace('_', '', $key); |
140
|
|
|
|
141
|
|
|
return method_exists($this, $getter) && null !== $this->__get($key); |
142
|
|
|
} |
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) |
153
|
|
|
{ |
154
|
|
|
try { |
155
|
|
|
$this->__set($key, null); |
156
|
|
|
} catch (Exception\BadMethodCallException $e) { |
157
|
|
|
throw new Exception\InvalidArgumentException( |
158
|
|
|
'The class property $' . $key . ' cannot be unset as' |
159
|
|
|
. ' NULL is an invalid value for it', |
160
|
|
|
0, |
161
|
|
|
$e |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|