PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/Explode.php

https://bitbucket.org/aboozar/zf2
PHP | 162 lines | 69 code | 21 blank | 72 comment | 4 complexity | 8a3a889a578d011d9268d2d2d80d3d27 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-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_Validate
  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_Validate_Interface
  102. *
  103. * Returns true if and only if $value is a valid list of email addresses
  104. * (separated by comma) according to RFC2822
  105. *
  106. * @link http://www.ietf.org/rfc/rfc2822.txt RFC2822
  107. * @link http://www.columbia.edu/kermit/ascii.html US-ASCII characters
  108. * @param string $value
  109. * @return boolean
  110. * @throws Exception\RuntimeException
  111. */
  112. public function isValid($value)
  113. {
  114. if (!is_string($value)) {
  115. $this->error(self::INVALID);
  116. return false;
  117. }
  118. $this->setValue($value);
  119. $values = explode($this->valueDelimiter, $value);
  120. $retval = true;
  121. $messages = array();
  122. $validator = $this->getValidator();
  123. if (!$validator) {
  124. throw new Exception\RuntimeException(sprintf(
  125. '%s expects a validator to be set; none given',
  126. __METHOD__
  127. ));
  128. }
  129. foreach ($values as $value) {
  130. if (!$validator->isValid($value)) {
  131. $messages[] = $validator->getMessages();
  132. $retval = false;
  133. if ($this->isBreakOnFirstFailure()) {
  134. break;
  135. }
  136. }
  137. }
  138. $this->abstractOptions['messages'] = $messages;
  139. return $retval;
  140. }
  141. }