Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-stdlib/src/AbstractOptions.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 5311817fb629 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 /** | |
3 * Zend Framework (http://framework.zend.com/) | |
4 * | |
5 * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 */ | |
9 | |
10 namespace Zend\Stdlib; | |
11 | |
12 use Traversable; | |
13 | |
14 abstract class AbstractOptions implements ParameterObjectInterface | |
15 { | |
16 /** | |
17 * We use the __ prefix to avoid collisions with properties in | |
18 * user-implementations. | |
19 * | |
20 * @var bool | |
21 */ | |
22 protected $__strictMode__ = true; | |
23 | |
24 /** | |
25 * Constructor | |
26 * | |
27 * @param array|Traversable|null $options | |
28 */ | |
29 public function __construct($options = null) | |
30 { | |
31 if (null !== $options) { | |
32 $this->setFromArray($options); | |
33 } | |
34 } | |
35 | |
36 /** | |
37 * Set one or more configuration properties | |
38 * | |
39 * @param array|Traversable|AbstractOptions $options | |
40 * @throws Exception\InvalidArgumentException | |
41 * @return AbstractOptions Provides fluent interface | |
42 */ | |
43 public function setFromArray($options) | |
44 { | |
45 if ($options instanceof self) { | |
46 $options = $options->toArray(); | |
47 } | |
48 | |
49 if (!is_array($options) && !$options instanceof Traversable) { | |
50 throw new Exception\InvalidArgumentException( | |
51 sprintf( | |
52 'Parameter provided to %s must be an %s, %s or %s', | |
53 __METHOD__, | |
54 'array', | |
55 'Traversable', | |
56 'Zend\Stdlib\AbstractOptions' | |
57 ) | |
58 ); | |
59 } | |
60 | |
61 foreach ($options as $key => $value) { | |
62 $this->__set($key, $value); | |
63 } | |
64 | |
65 return $this; | |
66 } | |
67 | |
68 /** | |
69 * Cast to array | |
70 * | |
71 * @return array | |
72 */ | |
73 public function toArray() | |
74 { | |
75 $array = []; | |
76 $transform = function ($letters) { | |
77 $letter = array_shift($letters); | |
78 return '_' . strtolower($letter); | |
79 }; | |
80 foreach ($this as $key => $value) { | |
81 if ($key === '__strictMode__') { | |
82 continue; | |
83 } | |
84 $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key); | |
85 $array[$normalizedKey] = $value; | |
86 } | |
87 return $array; | |
88 } | |
89 | |
90 /** | |
91 * Set a configuration property | |
92 * | |
93 * @see ParameterObject::__set() | |
94 * @param string $key | |
95 * @param mixed $value | |
96 * @throws Exception\BadMethodCallException | |
97 * @return void | |
98 */ | |
99 public function __set($key, $value) | |
100 { | |
101 $setter = 'set' . str_replace('_', '', $key); | |
102 | |
103 if (is_callable([$this, $setter])) { | |
104 $this->{$setter}($value); | |
105 | |
106 return; | |
107 } | |
108 | |
109 if ($this->__strictMode__) { | |
110 throw new Exception\BadMethodCallException(sprintf( | |
111 'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined', | |
112 $key, | |
113 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))), | |
114 $setter | |
115 )); | |
116 } | |
117 } | |
118 | |
119 /** | |
120 * Get a configuration property | |
121 * | |
122 * @see ParameterObject::__get() | |
123 * @param string $key | |
124 * @throws Exception\BadMethodCallException | |
125 * @return mixed | |
126 */ | |
127 public function __get($key) | |
128 { | |
129 $getter = 'get' . str_replace('_', '', $key); | |
130 | |
131 if (is_callable([$this, $getter])) { | |
132 return $this->{$getter}(); | |
133 } | |
134 | |
135 throw new Exception\BadMethodCallException(sprintf( | |
136 'The option "%s" does not have a callable "%s" getter method which must be defined', | |
137 $key, | |
138 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))) | |
139 )); | |
140 } | |
141 | |
142 /** | |
143 * Test if a configuration property is null | |
144 * @see ParameterObject::__isset() | |
145 * @param string $key | |
146 * @return bool | |
147 */ | |
148 public function __isset($key) | |
149 { | |
150 $getter = 'get' . str_replace('_', '', $key); | |
151 | |
152 return method_exists($this, $getter) && null !== $this->__get($key); | |
153 } | |
154 | |
155 /** | |
156 * Set a configuration property to NULL | |
157 * | |
158 * @see ParameterObject::__unset() | |
159 * @param string $key | |
160 * @throws Exception\InvalidArgumentException | |
161 * @return void | |
162 */ | |
163 public function __unset($key) | |
164 { | |
165 try { | |
166 $this->__set($key, null); | |
167 } catch (Exception\BadMethodCallException $e) { | |
168 throw new Exception\InvalidArgumentException( | |
169 'The class property $' . $key . ' cannot be unset as' | |
170 . ' NULL is an invalid value for it', | |
171 0, | |
172 $e | |
173 ); | |
174 } | |
175 } | |
176 } |