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

Debug toolbar