PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Validator/Explode.php

https://bitbucket.org/gencer/zf2
PHP | 214 lines | 106 code | 29 blank | 79 comment | 10 complexity | 7ff62ca7400f514dbc6bfedab52bfcc1 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-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\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. * @return bool
  142. * @throws Exception\RuntimeException
  143. */
  144. public function isValid($value)
  145. {
  146. $this->setValue($value);
  147. if ($value instanceof Traversable) {
  148. $value = ArrayUtils::iteratorToArray($value);
  149. }
  150. if (is_array($value)) {
  151. $values = $value;
  152. } elseif (is_string($value)) {
  153. $delimiter = $this->getValueDelimiter();
  154. // Skip explode if delimiter is null,
  155. // used when value is expected to be either an
  156. // array when multiple values and a string for
  157. // single values (ie. MultiCheckbox form behavior)
  158. $values = (null !== $delimiter)
  159. ? explode($this->valueDelimiter, $value)
  160. : array($value);
  161. } else {
  162. $values = array($value);
  163. }
  164. $retval = true;
  165. $messages = array();
  166. $validator = $this->getValidator();
  167. if (!$validator) {
  168. throw new Exception\RuntimeException(sprintf(
  169. '%s expects a validator to be set; none given',
  170. __METHOD__
  171. ));
  172. }
  173. foreach ($values as $value) {
  174. if (!$validator->isValid($value)) {
  175. $messages[] = $validator->getMessages();
  176. $retval = false;
  177. if ($this->isBreakOnFirstFailure()) {
  178. break;
  179. }
  180. }
  181. }
  182. $this->abstractOptions['messages'] = $messages;
  183. return $retval;
  184. }
  185. }