3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib
;
14 abstract class AbstractOptions
implements ParameterObjectInterface
17 * We use the __ prefix to avoid collisions with properties in
18 * user-implementations.
22 protected $__strictMode__ = true;
27 * @param array|Traversable|null $options
29 public function __construct($options = null)
31 if (null !== $options) {
32 $this->setFromArray($options);
37 * Set one or more configuration properties
39 * @param array|Traversable|AbstractOptions $options
40 * @throws Exception\InvalidArgumentException
41 * @return AbstractOptions Provides fluent interface
43 public function setFromArray($options)
45 if ($options instanceof self
) {
46 $options = $options->toArray();
49 if (!is_array($options) && !$options instanceof Traversable
) {
50 throw new Exception\
InvalidArgumentException(sprintf(
51 'Parameter provided to %s must be an %s, %s or %s',
52 __METHOD__
, 'array', 'Traversable', 'Zend\Stdlib\AbstractOptions'
56 foreach ($options as $key => $value) {
57 $this->__set($key, $value);
68 public function toArray()
71 $transform = function ($letters) {
72 $letter = array_shift($letters);
73 return '_' . strtolower($letter);
75 foreach ($this as $key => $value) {
76 if ($key === '__strictMode__') continue;
77 $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
78 $array[$normalizedKey] = $value;
84 * Set a configuration property
86 * @see ParameterObject::__set()
89 * @throws Exception\BadMethodCallException
92 public function __set($key, $value)
94 $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
95 if ($this->__strictMode__
&& !method_exists($this, $setter)) {
96 throw new Exception\
BadMethodCallException(
97 'The option "' . $key . '" does not '
98 . 'have a matching ' . $setter . ' setter method '
99 . 'which must be defined'
101 } elseif (!$this->__strictMode__
&& !method_exists($this, $setter)) {
104 $this->{$setter}($value);
108 * Get a configuration property
110 * @see ParameterObject::__get()
112 * @throws Exception\BadMethodCallException
115 public function __get($key)
117 $getter = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
118 if (!method_exists($this, $getter)) {
119 throw new Exception\
BadMethodCallException(
120 'The option "' . $key . '" does not '
121 . 'have a matching ' . $getter . ' getter method '
122 . 'which must be defined'
126 return $this->{$getter}();
130 * Test if a configuration property is null
131 * @see ParameterObject::__isset()
135 public function __isset($key)
137 return null !== $this->__get($key);
141 * Set a configuration property to NULL
143 * @see ParameterObject::__unset()
145 * @throws Exception\InvalidArgumentException
148 public function __unset($key)
151 $this->__set($key, null);
152 } catch (Exception\BadMethodCallException
$e) {
153 throw new Exception\
InvalidArgumentException(
154 'The class property $' . $key . ' cannot be unset as'
155 . ' NULL is an invalid value for it',