123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfValidatorSchemaCompare compares several values from an array.
- *
- * @package symfony
- * @subpackage validator
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfValidatorSchemaCompare.class.php 10802 2008-08-12 07:10:53Z fabien $
- */
- class sfValidatorSchemaCompare extends sfValidatorSchema
- {
- const EQUAL = '==';
- const NOT_EQUAL = '!=';
- const LESS_THAN = '<';
- const LESS_THAN_EQUAL = '<=';
- const GREATER_THAN = '>';
- const GREATER_THAN_EQUAL = '>=';
- /**
- * Constructor.
- *
- * Available options:
- *
- * * left_field: The left field name
- * * operator: The comparison operator
- * * self::EQUAL
- * * self::NOT_EQUAL
- * * self::LESS_THAN
- * * self::LESS_THAN_EQUAL
- * * self::GREATER_THAN
- * * self::GREATER_THAN_EQUAL
- * * right_field: The right field name
- * * throw_global_error: Whether to throw a global error (false by default) or an error tied to the left field
- *
- * @param string $leftField The left field name
- * @param string $operator The operator to apply
- * @param string $rightField The right field name
- * @param array $options An array of options
- * @param array $messages An array of error messages
- *
- * @see sfValidatorBase
- */
- public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array())
- {
- $this->addOption('left_field', $leftField);
- $this->addOption('operator', $operator);
- $this->addOption('right_field', $rightField);
- $this->addOption('throw_global_error', false);
- parent::__construct(null, $options, $messages);
- }
- /**
- * @see sfValidatorBase
- */
- protected function doClean($values)
- {
- if (is_null($values))
- {
- $values = array();
- }
- if (!is_array($values))
- {
- throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
- }
- $leftValue = isset($values[$this->getOption('left_field')]) ? $values[$this->getOption('left_field')] : null;
- $rightValue = isset($values[$this->getOption('right_field')]) ? $values[$this->getOption('right_field')] : null;
- switch ($this->getOption('operator'))
- {
- case self::GREATER_THAN:
- $valid = $leftValue > $rightValue;
- break;
- case self::GREATER_THAN_EQUAL:
- $valid = $leftValue >= $rightValue;
- break;
- case self::LESS_THAN:
- $valid = $leftValue < $rightValue;
- break;
- case self::LESS_THAN_EQUAL:
- $valid = $leftValue <= $rightValue;
- break;
- case self::NOT_EQUAL:
- $valid = $leftValue != $rightValue;
- break;
- case self::EQUAL:
- $valid = $leftValue == $rightValue;
- break;
- default:
- throw new InvalidArgumentException(sprintf('The operator "%s" does not exist.', $this->getOption('operator')));
- }
- if (!$valid)
- {
- $error = new sfValidatorError($this, 'invalid', array(
- 'left_field' => $leftValue,
- 'right_field' => $rightValue,
- 'operator' => $this->getOption('operator'),
- ));
- if ($this->getOption('throw_global_error'))
- {
- throw $error;
- }
- throw new sfValidatorErrorSchema($this, array($this->getOption('left_field') => $error));
- }
- return $values;
- }
- /**
- * @see sfValidatorBase
- */
- public function asString($indent = 0)
- {
- $options = $this->getOptionsWithoutDefaults();
- $messages = $this->getMessagesWithoutDefaults();
- unset($options['left_field'], $options['operator'], $options['right_field']);
- $arguments = '';
- if ($options || $messages)
- {
- $arguments = sprintf('(%s%s)',
- $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
- $messages ? ', '.sfYamlInline::dump($messages) : ''
- );
- }
- return sprintf('%s%s %s%s %s',
- str_repeat(' ', $indent),
- $this->getOption('left_field'),
- $this->getOption('operator'),
- $arguments,
- $this->getOption('right_field')
- );
- }
- }
|