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