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

Debug toolbar