Chris@0: setFromArray($options); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set one or more configuration properties Chris@0: * Chris@0: * @param array|Traversable|AbstractOptions $options Chris@0: * @throws Exception\InvalidArgumentException Chris@0: * @return AbstractOptions Provides fluent interface Chris@0: */ Chris@0: public function setFromArray($options) Chris@0: { Chris@0: if ($options instanceof self) { Chris@0: $options = $options->toArray(); Chris@0: } Chris@0: Chris@12: if (! is_array($options) && ! $options instanceof Traversable) { Chris@0: throw new Exception\InvalidArgumentException( Chris@0: sprintf( Chris@0: 'Parameter provided to %s must be an %s, %s or %s', Chris@0: __METHOD__, Chris@0: 'array', Chris@0: 'Traversable', Chris@0: 'Zend\Stdlib\AbstractOptions' Chris@0: ) Chris@0: ); Chris@0: } Chris@0: Chris@0: foreach ($options as $key => $value) { Chris@0: $this->__set($key, $value); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Cast to array Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function toArray() Chris@0: { Chris@0: $array = []; Chris@0: $transform = function ($letters) { Chris@0: $letter = array_shift($letters); Chris@0: return '_' . strtolower($letter); Chris@0: }; Chris@0: foreach ($this as $key => $value) { Chris@0: if ($key === '__strictMode__') { Chris@0: continue; Chris@0: } Chris@0: $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key); Chris@0: $array[$normalizedKey] = $value; Chris@0: } Chris@0: return $array; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set a configuration property Chris@0: * Chris@0: * @see ParameterObject::__set() Chris@0: * @param string $key Chris@0: * @param mixed $value Chris@0: * @throws Exception\BadMethodCallException Chris@0: * @return void Chris@0: */ Chris@0: public function __set($key, $value) Chris@0: { Chris@0: $setter = 'set' . str_replace('_', '', $key); Chris@0: Chris@0: if (is_callable([$this, $setter])) { Chris@0: $this->{$setter}($value); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($this->__strictMode__) { Chris@0: throw new Exception\BadMethodCallException(sprintf( Chris@0: 'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined', Chris@0: $key, Chris@0: 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))), Chris@0: $setter Chris@0: )); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a configuration property Chris@0: * Chris@0: * @see ParameterObject::__get() Chris@0: * @param string $key Chris@0: * @throws Exception\BadMethodCallException Chris@0: * @return mixed Chris@0: */ Chris@0: public function __get($key) Chris@0: { Chris@0: $getter = 'get' . str_replace('_', '', $key); Chris@0: Chris@0: if (is_callable([$this, $getter])) { Chris@0: return $this->{$getter}(); Chris@0: } Chris@0: Chris@0: throw new Exception\BadMethodCallException(sprintf( Chris@0: 'The option "%s" does not have a callable "%s" getter method which must be defined', Chris@0: $key, Chris@0: 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))) Chris@0: )); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test if a configuration property is null Chris@0: * @see ParameterObject::__isset() Chris@0: * @param string $key Chris@0: * @return bool Chris@0: */ Chris@0: public function __isset($key) Chris@0: { Chris@0: $getter = 'get' . str_replace('_', '', $key); Chris@0: Chris@0: return method_exists($this, $getter) && null !== $this->__get($key); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set a configuration property to NULL Chris@0: * Chris@0: * @see ParameterObject::__unset() Chris@0: * @param string $key Chris@0: * @throws Exception\InvalidArgumentException Chris@0: * @return void Chris@0: */ Chris@0: public function __unset($key) Chris@0: { Chris@0: try { Chris@0: $this->__set($key, null); Chris@0: } catch (Exception\BadMethodCallException $e) { Chris@0: throw new Exception\InvalidArgumentException( Chris@0: 'The class property $' . $key . ' cannot be unset as' Chris@0: . ' NULL is an invalid value for it', Chris@0: 0, Chris@0: $e Chris@0: ); Chris@0: } Chris@0: } Chris@0: }