1. sfValidatorOr.class.php
  2. /** * sfValidatorOr validates an input value if at least one validator passes. * * @package symfony * @subpackage validator * @author Fabien Potencier * @version SVN: $Id: sfValidatorOr.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfValidatorOr 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('sfValidatorOr constructor takes a sfValidatorBase object, or a sfValidatorBase array.');
  38. }
  39. parent::__construct($options, $messages);
  40. }
  41. /**
  42. * @see sfValidatorBase
  43. */
  44. protected function configure($options = array(), $messages = array())
  45. {
  46. $this->setMessage('invalid', null);
  47. }
  48. /**
  49. * Adds a validator.
  50. *
  51. * @param sfValidatorBase $validator An sfValidatorBase instance
  52. */
  53. public function addValidator(sfValidatorBase $validator)
  54. {
  55. $this->validators[] = $validator;
  56. }
  57. /**
  58. * Returns an array of the validators.
  59. *
  60. * @return array An array of sfValidatorBase instances
  61. */
  62. public function getValidators()
  63. {
  64. return $this->validators;
  65. }
  66. /**
  67. * @see sfValidatorBase
  68. */
  69. protected function doClean($value)
  70. {
  71. $errors = array();
  72. foreach ($this->validators as $validator)
  73. {
  74. try
  75. {
  76. return $validator->clean($value);
  77. }
  78. catch (sfValidatorError $e)
  79. {
  80. $errors[] = $e;
  81. }
  82. }
  83. if ($this->getMessage('invalid'))
  84. {
  85. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  86. }
  87. throw new sfValidatorErrorSchema($this, $errors);
  88. }
  89. /**
  90. * @see sfValidatorBase
  91. */
  92. public function asString($indent = 0)
  93. {
  94. $validators = '';
  95. for ($i = 0, $max = count($this->validators); $i < $max; $i++)
  96. {
  97. $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
  98. if ($i < $max - 1)
  99. {
  100. $validators .= str_repeat(' ', $indent + 2).'or';
  101. }
  102. if ($i == $max - 2)
  103. {
  104. $options = $this->getOptionsWithoutDefaults();
  105. $messages = $this->getMessagesWithoutDefaults();
  106. if ($options || $messages)
  107. {
  108. $validators .= sprintf('(%s%s)',
  109. $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
  110. $messages ? ', '.sfYamlInline::dump($messages) : ''
  111. );
  112. }
  113. }
  114. }
  115. return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
  116. }
  117. }

Debug toolbar