sfValidatorAnd.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 13126 2008-11-18 15:35:02Z 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. * @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 A 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. $clean = $value;
  86. $errors = array();
  87. foreach ($this->validators as $validator)
  88. {
  89. try
  90. {
  91. $clean = $validator->clean($clean);
  92. }
  93. catch (sfValidatorError $e)
  94. {
  95. $errors[] = $e;
  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. }