3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Stdlib;
14 abstract class AbstractOptions implements ParameterObjectInterface
16 // @codingStandardsIgnoreStart
18 * We use the __ prefix to avoid collisions with properties in
19 * user-implementations.
23 protected $__strictMode__ = true;
24 // @codingStandardsIgnoreEnd
29 * @param array|Traversable|null $options
31 public function __construct($options = null)
33 if (null !== $options) {
34 $this->setFromArray($options);
39 * Set one or more configuration properties
41 * @param array|Traversable|AbstractOptions $options
42 * @throws Exception\InvalidArgumentException
43 * @return AbstractOptions Provides fluent interface
45 public function setFromArray($options)
47 if ($options instanceof self) {
48 $options = $options->toArray();
51 if (! is_array($options) && ! $options instanceof Traversable) {
52 throw new Exception\InvalidArgumentException(
54 'Parameter provided to %s must be an %s, %s or %s',
58 'Zend\Stdlib\AbstractOptions'
63 foreach ($options as $key => $value) {
64 $this->__set($key, $value);
75 public function toArray()
78 $transform = function ($letters) {
79 $letter = array_shift($letters);
80 return '_' . strtolower($letter);
82 foreach ($this as $key => $value) {
83 if ($key === '__strictMode__') {
86 $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
87 $array[$normalizedKey] = $value;
93 * Set a configuration property
95 * @see ParameterObject::__set()
98 * @throws Exception\BadMethodCallException
101 public function __set($key, $value)
103 $setter = 'set' . str_replace('_', '', $key);
105 if (is_callable([$this, $setter])) {
106 $this->{$setter}($value);
111 if ($this->__strictMode__) {
112 throw new Exception\BadMethodCallException(sprintf(
113 'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined',
115 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))),
122 * Get a configuration property
124 * @see ParameterObject::__get()
126 * @throws Exception\BadMethodCallException
129 public function __get($key)
131 $getter = 'get' . str_replace('_', '', $key);
133 if (is_callable([$this, $getter])) {
134 return $this->{$getter}();
137 throw new Exception\BadMethodCallException(sprintf(
138 'The option "%s" does not have a callable "%s" getter method which must be defined',
140 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))
145 * Test if a configuration property is null
146 * @see ParameterObject::__isset()
150 public function __isset($key)
152 $getter = 'get' . str_replace('_', '', $key);
154 return method_exists($this, $getter) && null !== $this->__get($key);
158 * Set a configuration property to NULL
160 * @see ParameterObject::__unset()
162 * @throws Exception\InvalidArgumentException
165 public function __unset($key)
168 $this->__set($key, null);
169 } catch (Exception\BadMethodCallException $e) {
170 throw new Exception\InvalidArgumentException(
171 'The class property $' . $key . ' cannot be unset as'
172 . ' NULL is an invalid value for it',