Development

/branches/1.2/lib/validator/sfValidatorSchemaCompare.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/validator/sfValidatorSchemaCompare.class.php

Revision 10802, 4.3 kB (checked in by fabien, 6 years ago)

[1.1, 1.2] added an exception when using a non existant operator with sfValidatorSchemaCompare

  • 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  * sfValidatorSchemaCompare compares several values from an array.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorSchemaCompare extends sfValidatorSchema
20 {
21   const EQUAL              = '==';
22   const NOT_EQUAL          = '!=';
23   const LESS_THAN          = '<';
24   const LESS_THAN_EQUAL    = '<=';
25   const GREATER_THAN       = '>';
26   const GREATER_THAN_EQUAL = '>=';
27
28   /**
29    * Constructor.
30    *
31    * Available options:
32    *
33    *  * left_field:         The left field name
34    *  * operator:           The comparison operator
35    *                          * self::EQUAL
36    *                          * self::NOT_EQUAL
37    *                          * self::LESS_THAN
38    *                          * self::LESS_THAN_EQUAL
39    *                          * self::GREATER_THAN
40    *                          * self::GREATER_THAN_EQUAL
41    *  * right_field:        The right field name
42    *  * throw_global_error: Whether to throw a global error (false by default) or an error tied to the left field
43    *
44    * @param string $leftField   The left field name
45    * @param string $operator    The operator to apply
46    * @param string $rightField  The right field name
47    * @param array  $options     An array of options
48    * @param array  $messages    An array of error messages
49    *
50    * @see sfValidatorBase
51    */
52   public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array())
53   {
54     $this->addOption('left_field', $leftField);
55     $this->addOption('operator', $operator);
56     $this->addOption('right_field', $rightField);
57
58     $this->addOption('throw_global_error', false);
59
60     parent::__construct(null, $options, $messages);
61   }
62
63   /**
64    * @see sfValidatorBase
65    */
66   protected function doClean($values)
67   {
68     if (is_null($values))
69     {
70       $values = array();
71     }
72
73     if (!is_array($values))
74     {
75       throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
76     }
77
78     $leftValue  = isset($values[$this->getOption('left_field')]) ? $values[$this->getOption('left_field')] : null;
79     $rightValue = isset($values[$this->getOption('right_field')]) ? $values[$this->getOption('right_field')] : null;
80
81     switch ($this->getOption('operator'))
82     {
83       case self::GREATER_THAN:
84         $valid = $leftValue > $rightValue;
85         break;
86       case self::GREATER_THAN_EQUAL:
87         $valid = $leftValue >= $rightValue;
88         break;
89       case self::LESS_THAN:
90         $valid = $leftValue < $rightValue;
91         break;
92       case self::LESS_THAN_EQUAL:
93         $valid = $leftValue <= $rightValue;
94         break;
95       case self::NOT_EQUAL:
96         $valid = $leftValue != $rightValue;
97         break;
98       case self::EQUAL:
99         $valid = $leftValue == $rightValue;
100         break;
101       default:
102         throw new InvalidArgumentException(sprintf('The operator "%s" does not exist.', $this->getOption('operator')));
103     }
104
105     if (!$valid)
106     {
107       $error = new sfValidatorError($this, 'invalid', array(
108         'left_field'  => $leftValue,
109         'right_field' => $rightValue,
110         'operator'    => $this->getOption('operator'),
111       ));
112       if ($this->getOption('throw_global_error'))
113       {
114         throw $error;
115       }
116
117       throw new sfValidatorErrorSchema($this, array($this->getOption('left_field') => $error));
118     }
119
120     return $values;
121   }
122
123   /**
124    * @see sfValidatorBase
125    */
126   public function asString($indent = 0)
127   {
128     $options = $this->getOptionsWithoutDefaults();
129     $messages = $this->getMessagesWithoutDefaults();
130     unset($options['left_field'], $options['operator'], $options['right_field']);
131
132     $arguments = '';
133     if ($options || $messages)
134     {
135       $arguments = sprintf('(%s%s)',
136         $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
137         $messages ? ', '.sfYamlInline::dump($messages) : ''
138       );
139     }
140
141     return sprintf('%s%s %s%s %s',
142       str_repeat(' ', $indent),
143       $this->getOption('left_field'),
144       $this->getOption('operator'),
145       $arguments,
146       $this->getOption('right_field')
147     );
148   }
149 }
150
Note: See TracBrowser for help on using the browser.