PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Validator/Explode.php

https://bitbucket.org/raulgracia/zendframework2
PHP | 170 lines | 79 code | 23 blank | 68 comment | 6 complexity | a77f674dcb1a3e1f979a6daa057aff68 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2013 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
  13. {
  14. const INVALID = 'explodeInvalid';
  15. /**
  16. * @var array
  17. */
  18. protected $messageTemplates = array(
  19. self::INVALID => "Invalid type given.",
  20. );
  21. /**
  22. * @var array
  23. */
  24. protected $messageVariables = array();
  25. /**
  26. * @var string
  27. */
  28. protected $valueDelimiter = ',';
  29. /**
  30. * @var ValidatorInterface
  31. */
  32. protected $validator;
  33. /**
  34. * @var bool
  35. */
  36. protected $breakOnFirstFailure = false;
  37. /**
  38. * Sets the delimiter string that the values will be split upon
  39. *
  40. * @param string $delimiter
  41. * @return Explode
  42. */
  43. public function setValueDelimiter($delimiter)
  44. {
  45. $this->valueDelimiter = $delimiter;
  46. return $this;
  47. }
  48. /**
  49. * Returns the delimiter string that the values will be split upon
  50. *
  51. * @return string
  52. */
  53. public function getValueDelimiter()
  54. {
  55. return $this->valueDelimiter;
  56. }
  57. /**
  58. * Sets the Validator for validating each value
  59. *
  60. * @param ValidatorInterface $validator
  61. * @return Explode
  62. */
  63. public function setValidator(ValidatorInterface $validator)
  64. {
  65. $this->validator = $validator;
  66. return $this;
  67. }
  68. /**
  69. * Gets the Validator for validating each value
  70. *
  71. * @return ValidatorInterface
  72. */
  73. public function getValidator()
  74. {
  75. return $this->validator;
  76. }
  77. /**
  78. * Set break on first failure setting
  79. *
  80. * @param bool $break
  81. * @return Explode
  82. */
  83. public function setBreakOnFirstFailure($break)
  84. {
  85. $this->breakOnFirstFailure = (bool) $break;
  86. return $this;
  87. }
  88. /**
  89. * Get break on first failure setting
  90. *
  91. * @return bool
  92. */
  93. public function isBreakOnFirstFailure()
  94. {
  95. return $this->breakOnFirstFailure;
  96. }
  97. /**
  98. * Defined by Zend\Validator\ValidatorInterface
  99. *
  100. * Returns true if all values validate true
  101. *
  102. * @param mixed $value
  103. * @return bool
  104. * @throws Exception\RuntimeException
  105. */
  106. public function isValid($value)
  107. {
  108. $this->setValue($value);
  109. if ($value instanceof Traversable) {
  110. $value = ArrayUtils::iteratorToArray($value);
  111. }
  112. if (is_array($value)) {
  113. $values = $value;
  114. } elseif (is_string($value)) {
  115. $delimiter = $this->getValueDelimiter();
  116. // Skip explode if delimiter is null,
  117. // used when value is expected to be either an
  118. // array when multiple values and a string for
  119. // single values (ie. MultiCheckbox form behavior)
  120. $values = (null !== $delimiter)
  121. ? explode($this->valueDelimiter, $value)
  122. : array($value);
  123. } else {
  124. $values = array($value);
  125. }
  126. $retval = true;
  127. $messages = array();
  128. $validator = $this->getValidator();
  129. if (!$validator) {
  130. throw new Exception\RuntimeException(sprintf(
  131. '%s expects a validator to be set; none given',
  132. __METHOD__
  133. ));
  134. }
  135. foreach ($values as $value) {
  136. if (!$validator->isValid($value)) {
  137. $messages[] = $validator->getMessages();
  138. $retval = false;
  139. if ($this->isBreakOnFirstFailure()) {
  140. break;
  141. }
  142. }
  143. }
  144. $this->abstractOptions['messages'] = $messages;
  145. return $retval;
  146. }
  147. }