Development

/branches/1.1/lib/validator/sfValidatorAnd.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/validator/sfValidatorAnd.class.php

Revision 13126, 3.4 kB (checked in by nicolas, 4 years ago)

reverted from r9411: sfValidatorAnd and sfValidatorOr validators are now required by default to ensure a standard behavior - fixes and closes #4877

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfValidatorAnd validates an input value if all validators passes.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorAnd extends sfValidatorBase
20 {
21   protected
22     $validators = array();
23
24   /**
25    * Constructor.
26    *
27    * The first argument can be:
28    *
29    *  * null
30    *  * a sfValidatorBase instance
31    *  * an array of sfValidatorBase instances
32    *
33    * @param mixed $validators Initial validators
34    * @param array $options    An array of options
35    * @param array $messages   An array of error messages
36    *
37    * @see sfValidatorBase
38    */
39   public function __construct($validators = null, $options = array(), $messages = array())
40   {
41     if ($validators instanceof sfValidatorBase)
42     {
43       $this->addValidator($validators);
44     }
45     else if (is_array($validators))
46     {
47       foreach ($validators as $validator)
48       {
49         $this->addValidator($validator);
50       }
51     }
52     else if (!is_null($validators))
53     {
54       throw new InvalidArgumentException('sfValidatorAnd constructor takes a sfValidatorBase object, or a sfValidatorBase array.');
55     }
56     
57     parent::__construct($options, $messages);
58   }
59
60   /**
61    * @see sfValidatorBase
62    */
63   protected function configure($options = array(), $messages = array())
64   {
65     $this->setMessage('invalid', null);
66   }
67
68   /**
69    * Adds a validator.
70    *
71    * @param sfValidatorBase $validator  A sfValidatorBase instance
72    */
73   public function addValidator(sfValidatorBase $validator)
74   {
75     $this->validators[] = $validator;
76   }
77
78   /**
79    * Returns an array of the validators.
80    *
81    * @return array An array of sfValidatorBase instances
82    */
83   public function getValidators()
84   {
85     return $this->validators;
86   }
87
88   /**
89    * @see sfValidatorBase
90    */
91   protected function doClean($value)
92   {
93     $clean = $value;
94     $errors = array();
95     foreach ($this->validators as $validator)
96     {
97       try
98       {
99         $clean = $validator->clean($clean);
100       }
101       catch (sfValidatorError $e)
102       {
103         $errors[] = $e;
104       }
105     }
106
107     if (count($errors))
108     {
109       if ($this->getMessage('invalid'))
110       {
111         throw new sfValidatorError($this, 'invalid', array('value' => $value));
112       }
113
114       throw new sfValidatorErrorSchema($this, $errors);
115     }
116
117     return $clean;
118   }
119
120   /**
121    * @see sfValidatorBase
122    */
123   public function asString($indent = 0)
124   {
125     $validators = '';
126     for ($i = 0, $max = count($this->validators); $i < $max; $i++)
127     {
128       $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
129
130       if ($i < $max - 1)
131       {
132         $validators .= str_repeat(' ', $indent + 2).'and';
133       }
134
135       if ($i == $max - 2)
136       {
137         $options = $this->getOptionsWithoutDefaults();
138         $messages = $this->getMessagesWithoutDefaults();
139
140         if ($options || $messages)
141         {
142           $validators .= sprintf('(%s%s)',
143             $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
144             $messages ? ', '.sfYamlInline::dump($messages) : ''
145           );
146         }
147       }
148     }
149
150     return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
151   }
152 }
153
Note: See TracBrowser for help on using the browser.