sfValidatorOr.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * sfValidatorOr validates an input value if at least one validator passes.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorOr.class.php 13127 2008-11-18 15:35:59Z nicolas $
  16. */
  17. class sfValidatorOr 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('sfValidatorOr constructor takes a sfValidatorBase object, or a sfValidatorBase array.');
  52. }
  53. parent::__construct($options, $messages);
  54. }
  55. /**
  56. * @see sfValidatorBase
  57. */
  58. protected function configure($options = array(), $messages = array())
  59. {
  60. $this->setMessage('invalid', null);
  61. }
  62. /**
  63. * Adds a validator.
  64. *
  65. * @param sfValidatorBase $validator An sfValidatorBase instance
  66. */
  67. public function addValidator(sfValidatorBase $validator)
  68. {
  69. $this->validators[] = $validator;
  70. }
  71. /**
  72. * Returns an array of the validators.
  73. *
  74. * @return array An array of sfValidatorBase instances
  75. */
  76. public function getValidators()
  77. {
  78. return $this->validators;
  79. }
  80. /**
  81. * @see sfValidatorBase
  82. */
  83. protected function doClean($value)
  84. {
  85. $errors = array();
  86. foreach ($this->validators as $validator)
  87. {
  88. try
  89. {
  90. return $validator->clean($value);
  91. }
  92. catch (sfValidatorError $e)
  93. {
  94. $errors[] = $e;
  95. }
  96. }
  97. if ($this->getMessage('invalid'))
  98. {
  99. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  100. }
  101. throw new sfValidatorErrorSchema($this, $errors);
  102. }
  103. /**
  104. * @see sfValidatorBase
  105. */
  106. public function asString($indent = 0)
  107. {
  108. $validators = '';
  109. for ($i = 0, $max = count($this->validators); $i < $max; $i++)
  110. {
  111. $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
  112. if ($i < $max - 1)
  113. {
  114. $validators .= str_repeat(' ', $indent + 2).'or';
  115. }
  116. if ($i == $max - 2)
  117. {
  118. $options = $this->getOptionsWithoutDefaults();
  119. $messages = $this->getMessagesWithoutDefaults();
  120. if ($options || $messages)
  121. {
  122. $validators .= sprintf('(%s%s)',
  123. $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
  124. $messages ? ', '.sfYamlInline::dump($messages) : ''
  125. );
  126. }
  127. }
  128. }
  129. return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
  130. }
  131. }