123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- class sfValidatorAnd extends sfValidatorBase
- {
- protected
- $validators = array();
-
- public function __construct($validators = null, $options = array(), $messages = array())
- {
- if ($validators instanceof sfValidatorBase)
- {
- $this->addValidator($validators);
- }
- else if (is_array($validators))
- {
- foreach ($validators as $validator)
- {
- $this->addValidator($validator);
- }
- }
- else if (!is_null($validators))
- {
- throw new InvalidArgumentException('sfValidatorAnd constructor takes a sfValidatorBase object, or a sfValidatorBase array.');
- }
-
- parent::__construct($options, $messages);
- }
-
- protected function configure($options = array(), $messages = array())
- {
- $this->addOption('halt_on_error', false);
- $this->setMessage('invalid', null);
- }
-
- public function addValidator(sfValidatorBase $validator)
- {
- $this->validators[] = $validator;
- }
-
- public function getValidators()
- {
- return $this->validators;
- }
-
- protected function doClean($value)
- {
- $clean = $value;
- $errors = array();
- foreach ($this->validators as $validator)
- {
- try
- {
- $clean = $validator->clean($clean);
- }
- catch (sfValidatorError $e)
- {
- $errors[] = $e;
- if ($this->getOption('halt_on_error'))
- {
- break;
- }
- }
- }
- if (count($errors))
- {
- if ($this->getMessage('invalid'))
- {
- throw new sfValidatorError($this, 'invalid', array('value' => $value));
- }
- throw new sfValidatorErrorSchema($this, $errors);
- }
- return $clean;
- }
-
- public function asString($indent = 0)
- {
- $validators = '';
- for ($i = 0, $max = count($this->validators); $i < $max; $i++)
- {
- $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
- if ($i < $max - 1)
- {
- $validators .= str_repeat(' ', $indent + 2).'and';
- }
- if ($i == $max - 2)
- {
- $options = $this->getOptionsWithoutDefaults();
- $messages = $this->getMessagesWithoutDefaults();
- if ($options || $messages)
- {
- $validators .= sprintf('(%s%s)',
- $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
- $messages ? ', '.sfYamlInline::dump($messages) : ''
- );
- }
- }
- }
- return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
- }
- }
|