PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/common/third_party/Zend/Validator/Explode.php

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