AbstractOptions.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-2015 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(
  45. sprintf(
  46. 'Parameter provided to %s must be an %s, %s or %s',
  47. __METHOD__,
  48. 'array',
  49. 'Traversable',
  50. 'Zend\Stdlib\AbstractOptions'
  51. )
  52. );
  53. }
  54. foreach ($options as $key => $value) {
  55. $this->__set($key, $value);
  56. }
  57. return $this;
  58. }
  59. /**
  60. * Cast to array
  61. *
  62. * @return array
  63. */
  64. public function toArray()
  65. {
  66. $array = [];
  67. $transform = function ($letters) {
  68. $letter = array_shift($letters);
  69. return '_' . strtolower($letter);
  70. };
  71. foreach ($this as $key => $value) {
  72. if ($key === '__strictMode__') {
  73. continue;
  74. }
  75. $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
  76. $array[$normalizedKey] = $value;
  77. }
  78. return $array;
  79. }
  80. /**
  81. * Set a configuration property
  82. *
  83. * @see ParameterObject::__set()
  84. * @param string $key
  85. * @param mixed $value
  86. * @throws Exception\BadMethodCallException
  87. * @return void
  88. */
  89. public function __set($key, $value)
  90. {
  91. $setter = 'set' . str_replace('_', '', $key);
  92. if (is_callable([$this, $setter])) {
  93. $this->{$setter}($value);
  94. return;
  95. }
  96. if ($this->__strictMode__) {
  97. throw new Exception\BadMethodCallException(sprintf(
  98. 'The option "%s" does not have a callable "%s" ("%s") setter method which must be defined',
  99. $key,
  100. 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))),
  101. $setter
  102. ));
  103. }
  104. }
  105. /**
  106. * Get a configuration property
  107. *
  108. * @see ParameterObject::__get()
  109. * @param string $key
  110. * @throws Exception\BadMethodCallException
  111. * @return mixed
  112. */
  113. public function __get($key)
  114. {
  115. $getter = 'get' . str_replace('_', '', $key);
  116. if (is_callable([$this, $getter])) {
  117. return $this->{$getter}();
  118. }
  119. throw new Exception\BadMethodCallException(sprintf(
  120. 'The option "%s" does not have a callable "%s" getter method which must be defined',
  121. $key,
  122. 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))
  123. ));
  124. }
  125. /**
  126. * Test if a configuration property is null
  127. * @see ParameterObject::__isset()
  128. * @param string $key
  129. * @return bool
  130. */
  131. public function __isset($key)
  132. {
  133. $getter = 'get' . str_replace('_', '', $key);
  134. return method_exists($this, $getter) && null !== $this->__get($key);
  135. }
  136. /**
  137. * Set a configuration property to NULL
  138. *
  139. * @see ParameterObject::__unset()
  140. * @param string $key
  141. * @throws Exception\InvalidArgumentException
  142. * @return void
  143. */
  144. public function __unset($key)
  145. {
  146. try {
  147. $this->__set($key, null);
  148. } catch (Exception\BadMethodCallException $e) {
  149. throw new Exception\InvalidArgumentException(
  150. 'The class property $' . $key . ' cannot be unset as'
  151. . ' NULL is an invalid value for it',
  152. 0,
  153. $e
  154. );
  155. }
  156. }
  157. }