sfValidatorAnd.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorAnd validates an input value if all validators passes.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorAnd.class.php 13127 2008-11-18 15:35:59Z nicolas $
  16. */
  17. class sfValidatorAnd extends sfValidatorBase
  18. {
  19. protected
  20. $validators = array();
  21. /**
  22. * Constructor.
  23. *
  24. * The first argument can be:
  25. *
  26. * * null
  27. * * a sfValidatorBase instance
  28. * * an array of sfValidatorBase instances
  29. *
  30. * @param mixed $validators Initial validators
  31. * @param array $options An array of options
  32. * @param array $messages An array of error messages
  33. *
  34. * @see sfValidatorBase
  35. */
  36. public function __construct($validators = null, $options = array(), $messages = array())
  37. {
  38. if ($validators instanceof sfValidatorBase)
  39. {
  40. $this->addValidator($validators);
  41. }
  42. else if (is_array($validators))
  43. {
  44. foreach ($validators as $validator)
  45. {
  46. $this->addValidator($validator);
  47. }
  48. }
  49. else if (!is_null($validators))
  50. {
  51. throw new InvalidArgumentException('sfValidatorAnd constructor takes a sfValidatorBase object, or a sfValidatorBase array.');
  52. }
  53. parent::__construct($options, $messages);
  54. }
  55. /**
  56. * Configures the current validator.
  57. *
  58. * Available options:
  59. *
  60. * * halt_on_error: Whether to halt on the first error or not (false by default)
  61. *
  62. * @param array $options An array of options
  63. * @param array $messages An array of error messages
  64. *
  65. * @see sfValidatorBase
  66. */
  67. protected function configure($options = array(), $messages = array())
  68. {
  69. $this->addOption('halt_on_error', false);
  70. $this->setMessage('invalid', null);
  71. }
  72. /**
  73. * Adds a validator.
  74. *
  75. * @param sfValidatorBase $validator A sfValidatorBase instance
  76. */
  77. public function addValidator(sfValidatorBase $validator)
  78. {
  79. $this->validators[] = $validator;
  80. }
  81. /**
  82. * Returns an array of the validators.
  83. *
  84. * @return array An array of sfValidatorBase instances
  85. */
  86. public function getValidators()
  87. {
  88. return $this->validators;
  89. }
  90. /**
  91. * @see sfValidatorBase
  92. */
  93. protected function doClean($value)
  94. {
  95. $clean = $value;
  96. $errors = array();
  97. foreach ($this->validators as $validator)
  98. {
  99. try
  100. {
  101. $clean = $validator->clean($clean);
  102. }
  103. catch (sfValidatorError $e)
  104. {
  105. $errors[] = $e;
  106. if ($this->getOption('halt_on_error'))
  107. {
  108. break;
  109. }
  110. }
  111. }
  112. if (count($errors))
  113. {
  114. if ($this->getMessage('invalid'))
  115. {
  116. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  117. }
  118. throw new sfValidatorErrorSchema($this, $errors);
  119. }
  120. return $clean;
  121. }
  122. /**
  123. * @see sfValidatorBase
  124. */
  125. public function asString($indent = 0)
  126. {
  127. $validators = '';
  128. for ($i = 0, $max = count($this->validators); $i < $max; $i++)
  129. {
  130. $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
  131. if ($i < $max - 1)
  132. {
  133. $validators .= str_repeat(' ', $indent + 2).'and';
  134. }
  135. if ($i == $max - 2)
  136. {
  137. $options = $this->getOptionsWithoutDefaults();
  138. $messages = $this->getMessagesWithoutDefaults();
  139. if ($options || $messages)
  140. {
  141. $validators .= sprintf('(%s%s)',
  142. $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
  143. $messages ? ', '.sfYamlInline::dump($messages) : ''
  144. );
  145. }
  146. }
  147. }
  148. return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
  149. }
  150. }