You are here

abstract class AbstractOptions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-stdlib/src/AbstractOptions.php \Zend\Stdlib\AbstractOptions

Hierarchy

Expanded class hierarchy of AbstractOptions

File

vendor/zendframework/zend-stdlib/src/AbstractOptions.php, line 14

Namespace

Zend\Stdlib
View source
abstract class AbstractOptions implements ParameterObjectInterface {

  /**
   * We use the __ prefix to avoid collisions with properties in
   * user-implementations.
   *
   * @var bool
   */
  protected $__strictMode__ = true;

  /**
   * Constructor
   *
   * @param  array|Traversable|null $options
   */
  public function __construct($options = null) {
    if (null !== $options) {
      $this
        ->setFromArray($options);
    }
  }

  /**
   * Set one or more configuration properties
   *
   * @param  array|Traversable|AbstractOptions $options
   * @throws Exception\InvalidArgumentException
   * @return AbstractOptions Provides fluent interface
   */
  public function setFromArray($options) {
    if ($options instanceof self) {
      $options = $options
        ->toArray();
    }
    if (!is_array($options) && !$options instanceof Traversable) {
      throw new Exception\InvalidArgumentException(sprintf('Parameter provided to %s must be an %s, %s or %s', __METHOD__, 'array', 'Traversable', 'Zend\\Stdlib\\AbstractOptions'));
    }
    foreach ($options as $key => $value) {
      $this
        ->__set($key, $value);
    }
    return $this;
  }

  /**
   * Cast to array
   *
   * @return array
   */
  public function toArray() {
    $array = [];
    $transform = function ($letters) {
      $letter = array_shift($letters);
      return '_' . strtolower($letter);
    };
    foreach ($this as $key => $value) {
      if ($key === '__strictMode__') {
        continue;
      }
      $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
      $array[$normalizedKey] = $value;
    }
    return $array;
  }

  /**
   * Set a configuration property
   *
   * @see ParameterObject::__set()
   * @param string $key
   * @param mixed $value
   * @throws Exception\BadMethodCallException
   * @return void
   */
  public function __set($key, $value) {
    $setter = 'set' . str_replace('_', '', $key);
    if (is_callable([
      $this,
      $setter,
    ])) {
      $this
        ->{$setter}($value);
      return;
    }
    if ($this->__strictMode__) {
      throw new Exception\BadMethodCallException(sprintf('The option "%s" does not have a callable "%s" ("%s") setter method which must be defined', $key, 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))), $setter));
    }
  }

  /**
   * Get a configuration property
   *
   * @see ParameterObject::__get()
   * @param string $key
   * @throws Exception\BadMethodCallException
   * @return mixed
   */
  public function __get($key) {
    $getter = 'get' . str_replace('_', '', $key);
    if (is_callable([
      $this,
      $getter,
    ])) {
      return $this
        ->{$getter}();
    }
    throw new Exception\BadMethodCallException(sprintf('The option "%s" does not have a callable "%s" getter method which must be defined', $key, 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))));
  }

  /**
   * Test if a configuration property is null
   * @see ParameterObject::__isset()
   * @param string $key
   * @return bool
   */
  public function __isset($key) {
    $getter = 'get' . str_replace('_', '', $key);
    return method_exists($this, $getter) && null !== $this
      ->__get($key);
  }

  /**
   * Set a configuration property to NULL
   *
   * @see ParameterObject::__unset()
   * @param string $key
   * @throws Exception\InvalidArgumentException
   * @return void
   */
  public function __unset($key) {
    try {
      $this
        ->__set($key, null);
    } catch (Exception\BadMethodCallException $e) {
      throw new Exception\InvalidArgumentException('The class property $' . $key . ' cannot be unset as' . ' NULL is an invalid value for it', 0, $e);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractOptions::$__strictMode__ protected property We use the __ prefix to avoid collisions with properties in user-implementations.
AbstractOptions::setFromArray public function Set one or more configuration properties
AbstractOptions::toArray public function Cast to array
AbstractOptions::__construct public function Constructor
AbstractOptions::__get public function Get a configuration property Overrides ParameterObjectInterface::__get
AbstractOptions::__isset public function Test if a configuration property is null Overrides ParameterObjectInterface::__isset
AbstractOptions::__set public function Set a configuration property Overrides ParameterObjectInterface::__set
AbstractOptions::__unset public function Set a configuration property to NULL Overrides ParameterObjectInterface::__unset