Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
 * sfValidatorOr validates an input value if at least one validator passes.
13
 *
14
 * @package    symfony
15
 * @subpackage validator
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfValidatorOr.class.php 21908 2009-09-11 12:06:21Z fabien $
18
 */
19
class sfValidatorOr 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 (null !== $validators)
53
    {
54
      throw new InvalidArgumentException('sfValidatorOr 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  An 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
    $errors = array();
94
    foreach ($this->validators as $validator)
95
    {
96
      try
97
      {
98
        return $validator->clean($value);
99
      }
100
      catch (sfValidatorError $e)
101
      {
102
        $errors[] = $e;
103
      }
104
    }
105
 
106
    if ($this->getMessage('invalid'))
107
    {
108
      throw new sfValidatorError($this, 'invalid', array('value' => $value));
109
    }
110
 
111
    throw new sfValidatorErrorSchema($this, $errors);
112
  }
113
 
114
  /**
115
   * @see sfValidatorBase
116
   */
117
  public function asString($indent = 0)
118
  {
119
    $validators = '';
120
    for ($i = 0, $max = count($this->validators); $i < $max; $i++)
121
    {
122
      $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
123
 
124
      if ($i < $max - 1)
125
      {
126
        $validators .= str_repeat(' ', $indent + 2).'or';
127
      }
128
 
129
      if ($i == $max - 2)
130
      {
131
        $options = $this->getOptionsWithoutDefaults();
132
        $messages = $this->getMessagesWithoutDefaults();
133
 
134
        if ($options || $messages)
135
        {
136
          $validators .= sprintf('(%s%s)',
137
            $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
138
            $messages ? ', '.sfYamlInline::dump($messages) : ''
139
          );
140
        }
141
      }
142
    }
143
 
144
    return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
145
  }
146
}