sfValidatorSchemaCompare.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorSchemaCompare compares several values from an array.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorSchemaCompare.class.php 10802 2008-08-12 07:10:53Z fabien $
  16. */
  17. class sfValidatorSchemaCompare extends sfValidatorSchema
  18. {
  19. const EQUAL = '==';
  20. const NOT_EQUAL = '!=';
  21. const LESS_THAN = '<';
  22. const LESS_THAN_EQUAL = '<=';
  23. const GREATER_THAN = '>';
  24. const GREATER_THAN_EQUAL = '>=';
  25. /**
  26. * Constructor.
  27. *
  28. * Available options:
  29. *
  30. * * left_field: The left field name
  31. * * operator: The comparison operator
  32. * * self::EQUAL
  33. * * self::NOT_EQUAL
  34. * * self::LESS_THAN
  35. * * self::LESS_THAN_EQUAL
  36. * * self::GREATER_THAN
  37. * * self::GREATER_THAN_EQUAL
  38. * * right_field: The right field name
  39. * * throw_global_error: Whether to throw a global error (false by default) or an error tied to the left field
  40. *
  41. * @param string $leftField The left field name
  42. * @param string $operator The operator to apply
  43. * @param string $rightField The right field name
  44. * @param array $options An array of options
  45. * @param array $messages An array of error messages
  46. *
  47. * @see sfValidatorBase
  48. */
  49. public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array())
  50. {
  51. $this->addOption('left_field', $leftField);
  52. $this->addOption('operator', $operator);
  53. $this->addOption('right_field', $rightField);
  54. $this->addOption('throw_global_error', false);
  55. parent::__construct(null, $options, $messages);
  56. }
  57. /**
  58. * @see sfValidatorBase
  59. */
  60. protected function doClean($values)
  61. {
  62. if (is_null($values))
  63. {
  64. $values = array();
  65. }
  66. if (!is_array($values))
  67. {
  68. throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
  69. }
  70. $leftValue = isset($values[$this->getOption('left_field')]) ? $values[$this->getOption('left_field')] : null;
  71. $rightValue = isset($values[$this->getOption('right_field')]) ? $values[$this->getOption('right_field')] : null;
  72. switch ($this->getOption('operator'))
  73. {
  74. case self::GREATER_THAN:
  75. $valid = $leftValue > $rightValue;
  76. break;
  77. case self::GREATER_THAN_EQUAL:
  78. $valid = $leftValue >= $rightValue;
  79. break;
  80. case self::LESS_THAN:
  81. $valid = $leftValue < $rightValue;
  82. break;
  83. case self::LESS_THAN_EQUAL:
  84. $valid = $leftValue <= $rightValue;
  85. break;
  86. case self::NOT_EQUAL:
  87. $valid = $leftValue != $rightValue;
  88. break;
  89. case self::EQUAL:
  90. $valid = $leftValue == $rightValue;
  91. break;
  92. default:
  93. throw new InvalidArgumentException(sprintf('The operator "%s" does not exist.', $this->getOption('operator')));
  94. }
  95. if (!$valid)
  96. {
  97. $error = new sfValidatorError($this, 'invalid', array(
  98. 'left_field' => $leftValue,
  99. 'right_field' => $rightValue,
  100. 'operator' => $this->getOption('operator'),
  101. ));
  102. if ($this->getOption('throw_global_error'))
  103. {
  104. throw $error;
  105. }
  106. throw new sfValidatorErrorSchema($this, array($this->getOption('left_field') => $error));
  107. }
  108. return $values;
  109. }
  110. /**
  111. * @see sfValidatorBase
  112. */
  113. public function asString($indent = 0)
  114. {
  115. $options = $this->getOptionsWithoutDefaults();
  116. $messages = $this->getMessagesWithoutDefaults();
  117. unset($options['left_field'], $options['operator'], $options['right_field']);
  118. $arguments = '';
  119. if ($options || $messages)
  120. {
  121. $arguments = sprintf('(%s%s)',
  122. $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
  123. $messages ? ', '.sfYamlInline::dump($messages) : ''
  124. );
  125. }
  126. return sprintf('%s%s %s%s %s',
  127. str_repeat(' ', $indent),
  128. $this->getOption('left_field'),
  129. $this->getOption('operator'),
  130. $arguments,
  131. $this->getOption('right_field')
  132. );
  133. }
  134. }