AbstractOptions.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib;
  10. use Traversable;
  11. abstract class AbstractOptions implements ParameterObjectInterface
  12. {
  13. /**
  14. * We use the __ prefix to avoid collisions with properties in
  15. * user-implementations.
  16. *
  17. * @var bool
  18. */
  19. protected $__strictMode__ = true;
  20. /**
  21. * Constructor
  22. *
  23. * @param array|Traversable|null $options
  24. */
  25. public function __construct($options = null)
  26. {
  27. if (null !== $options) {
  28. $this->setFromArray($options);
  29. }
  30. }
  31. /**
  32. * Set one or more configuration properties
  33. *
  34. * @param array|Traversable|AbstractOptions $options
  35. * @throws Exception\InvalidArgumentException
  36. * @return AbstractOptions Provides fluent interface
  37. */
  38. public function setFromArray($options)
  39. {
  40. if ($options instanceof self) {
  41. $options = $options->toArray();
  42. }
  43. if (!is_array($options) && !$options instanceof Traversable) {
  44. throw new Exception\InvalidArgumentException(sprintf(
  45. 'Parameter provided to %s must be an %s, %s or %s',
  46. __METHOD__, 'array', 'Traversable', 'Zend\Stdlib\AbstractOptions'
  47. ));
  48. }
  49. foreach ($options as $key => $value) {
  50. $this->__set($key, $value);
  51. }
  52. return $this;
  53. }
  54. /**
  55. * Cast to array
  56. *
  57. * @return array
  58. */
  59. public function toArray()
  60. {
  61. $array = array();
  62. $transform = function ($letters) {
  63. $letter = array_shift($letters);
  64. return '_' . strtolower($letter);
  65. };
  66. foreach ($this as $key => $value) {
  67. if ($key === '__strictMode__') continue;
  68. $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
  69. $array[$normalizedKey] = $value;
  70. }
  71. return $array;
  72. }
  73. /**
  74. * Set a configuration property
  75. *
  76. * @see ParameterObject::__set()
  77. * @param string $key
  78. * @param mixed $value
  79. * @throws Exception\BadMethodCallException
  80. * @return void
  81. */
  82. public function __set($key, $value)
  83. {
  84. $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
  85. if ($this->__strictMode__ && !method_exists($this, $setter)) {
  86. throw new Exception\BadMethodCallException(
  87. 'The option "' . $key . '" does not '
  88. . 'have a matching ' . $setter . ' setter method '
  89. . 'which must be defined'
  90. );
  91. } elseif (!$this->__strictMode__ && !method_exists($this, $setter)) {
  92. return;
  93. }
  94. $this->{$setter}($value);
  95. }
  96. /**
  97. * Get a configuration property
  98. *
  99. * @see ParameterObject::__get()
  100. * @param string $key
  101. * @throws Exception\BadMethodCallException
  102. * @return mixed
  103. */
  104. public function __get($key)
  105. {
  106. $getter = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
  107. if (!method_exists($this, $getter)) {
  108. throw new Exception\BadMethodCallException(
  109. 'The option "' . $key . '" does not '
  110. . 'have a matching ' . $getter . ' getter method '
  111. . 'which must be defined'
  112. );
  113. }
  114. return $this->{$getter}();
  115. }
  116. /**
  117. * Test if a configuration property is null
  118. * @see ParameterObject::__isset()
  119. * @param string $key
  120. * @return bool
  121. */
  122. public function __isset($key)
  123. {
  124. return null !== $this->__get($key);
  125. }
  126. /**
  127. * Set a configuration property to NULL
  128. *
  129. * @see ParameterObject::__unset()
  130. * @param string $key
  131. * @throws Exception\InvalidArgumentException
  132. * @return void
  133. */
  134. public function __unset($key)
  135. {
  136. try {
  137. $this->__set($key, null);
  138. } catch (Exception\BadMethodCallException $e) {
  139. throw new Exception\InvalidArgumentException(
  140. 'The class property $' . $key . ' cannot be unset as'
  141. . ' NULL is an invalid value for it',
  142. 0,
  143. $e
  144. );
  145. }
  146. }
  147. }