/library/Zend/Validator/Explode.php

http://github.com/zendframework/zf2 · PHP · 210 lines · 102 code · 28 blank · 80 comment · 11 complexity · f713a93b402f6e6eed53fe62753918dd MD5 · raw file

  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\Validator;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. class Explode extends AbstractValidator implements ValidatorPluginManagerAwareInterface
  13. {
  14. const INVALID = 'explodeInvalid';
  15. protected $pluginManager;
  16. /**
  17. * @var array
  18. */
  19. protected $messageTemplates = array(
  20. self::INVALID => "Invalid type given",
  21. );
  22. /**
  23. * @var array
  24. */
  25. protected $messageVariables = array();
  26. /**
  27. * @var string
  28. */
  29. protected $valueDelimiter = ',';
  30. /**
  31. * @var ValidatorInterface
  32. */
  33. protected $validator;
  34. /**
  35. * @var bool
  36. */
  37. protected $breakOnFirstFailure = false;
  38. /**
  39. * Sets the delimiter string that the values will be split upon
  40. *
  41. * @param string $delimiter
  42. * @return Explode
  43. */
  44. public function setValueDelimiter($delimiter)
  45. {
  46. $this->valueDelimiter = $delimiter;
  47. return $this;
  48. }
  49. /**
  50. * Returns the delimiter string that the values will be split upon
  51. *
  52. * @return string
  53. */
  54. public function getValueDelimiter()
  55. {
  56. return $this->valueDelimiter;
  57. }
  58. /**
  59. * Set validator plugin manager
  60. *
  61. * @param ValidatorPluginManager $pluginManager
  62. */
  63. public function setValidatorPluginManager(ValidatorPluginManager $pluginManager)
  64. {
  65. $this->pluginManager = $pluginManager;
  66. }
  67. /**
  68. * Get validator plugin manager
  69. *
  70. * @return ValidatorPluginManager
  71. */
  72. public function getValidatorPluginManager()
  73. {
  74. if (!$this->pluginManager) {
  75. $this->setValidatorPluginManager(new ValidatorPluginManager());
  76. }
  77. return $this->pluginManager;
  78. }
  79. /**
  80. * Sets the Validator for validating each value
  81. *
  82. * @param ValidatorInterface|array $validator
  83. * @throws Exception\RuntimeException
  84. * @return Explode
  85. */
  86. public function setValidator($validator)
  87. {
  88. if (is_array($validator)) {
  89. if (!isset($validator['name'])) {
  90. throw new Exception\RuntimeException(
  91. 'Invalid validator specification provided; does not include "name" key'
  92. );
  93. }
  94. $name = $validator['name'];
  95. $options = isset($validator['options']) ? $validator['options'] : array();
  96. $validator = $this->getValidatorPluginManager()->get($name, $options);
  97. }
  98. if (!$validator instanceof ValidatorInterface) {
  99. throw new Exception\RuntimeException(
  100. 'Invalid validator given'
  101. );
  102. }
  103. $this->validator = $validator;
  104. return $this;
  105. }
  106. /**
  107. * Gets the Validator for validating each value
  108. *
  109. * @return ValidatorInterface
  110. */
  111. public function getValidator()
  112. {
  113. return $this->validator;
  114. }
  115. /**
  116. * Set break on first failure setting
  117. *
  118. * @param bool $break
  119. * @return Explode
  120. */
  121. public function setBreakOnFirstFailure($break)
  122. {
  123. $this->breakOnFirstFailure = (bool) $break;
  124. return $this;
  125. }
  126. /**
  127. * Get break on first failure setting
  128. *
  129. * @return bool
  130. */
  131. public function isBreakOnFirstFailure()
  132. {
  133. return $this->breakOnFirstFailure;
  134. }
  135. /**
  136. * Defined by Zend\Validator\ValidatorInterface
  137. *
  138. * Returns true if all values validate true
  139. *
  140. * @param mixed $value
  141. * @param mixed $context Extra "context" to provide the composed validator
  142. * @return bool
  143. * @throws Exception\RuntimeException
  144. */
  145. public function isValid($value, $context = null)
  146. {
  147. $this->setValue($value);
  148. if ($value instanceof Traversable) {
  149. $value = ArrayUtils::iteratorToArray($value);
  150. }
  151. if (is_array($value)) {
  152. $values = $value;
  153. } elseif (is_string($value)) {
  154. $delimiter = $this->getValueDelimiter();
  155. // Skip explode if delimiter is null,
  156. // used when value is expected to be either an
  157. // array when multiple values and a string for
  158. // single values (ie. MultiCheckbox form behavior)
  159. $values = (null !== $delimiter)
  160. ? explode($this->valueDelimiter, $value)
  161. : array($value);
  162. } else {
  163. $values = array($value);
  164. }
  165. $validator = $this->getValidator();
  166. if (!$validator) {
  167. throw new Exception\RuntimeException(sprintf(
  168. '%s expects a validator to be set; none given',
  169. __METHOD__
  170. ));
  171. }
  172. foreach ($values as $value) {
  173. if (!$validator->isValid($value, $context)) {
  174. $this->abstractOptions['messages'][] = $validator->getMessages();
  175. if ($this->isBreakOnFirstFailure()) {
  176. return false;
  177. }
  178. }
  179. }
  180. return count($this->abstractOptions['messages']) == 0;
  181. }
  182. }