sfValidatorSchemaCompare.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 10902 2008-08-14 19:23:45Z fabien $
  16. */
  17. class sfValidatorSchemaCompare extends sfValidatorSchema
  18. {
  19. const EQUAL = 'equal';
  20. const NOT_EQUAL = 'not_equal';
  21. const LESS_THAN = 'less_than';
  22. const LESS_THAN_EQUAL = 'less_than_equal';
  23. const GREATER_THAN = 'greater_than';
  24. const GREATER_THAN_EQUAL = '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. default:
  91. $valid = $leftValue == $rightValue;
  92. }
  93. if (!$valid)
  94. {
  95. $error = new sfValidatorError($this, 'invalid', array(
  96. 'left_field' => $leftValue,
  97. 'right_field' => $rightValue,
  98. 'operator' => $this->getOption('operator'),
  99. ));
  100. if ($this->getOption('throw_global_error'))
  101. {
  102. throw $error;
  103. }
  104. throw new sfValidatorErrorSchema($this, array($this->getOption('left_field') => $error));
  105. }
  106. return $values;
  107. }
  108. /**
  109. * @see sfValidatorBase
  110. */
  111. public function asString($indent = 0)
  112. {
  113. $options = $this->getOptionsWithoutDefaults();
  114. $messages = $this->getMessagesWithoutDefaults();
  115. unset($options['left_field'], $options['operator'], $options['right_field']);
  116. $arguments = '';
  117. if ($options || $messages)
  118. {
  119. $arguments = sprintf('(%s%s)',
  120. $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
  121. $messages ? ', '.sfYamlInline::dump($messages) : ''
  122. );
  123. }
  124. return sprintf('%s%s %s%s %s',
  125. str_repeat(' ', $indent),
  126. $this->getOption('left_field'),
  127. $this->getOption('operator'),
  128. $arguments,
  129. $this->getOption('right_field')
  130. );
  131. }
  132. }