Skip to content

[2.3] [Validator] added comparison constraints and validators #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* copied the constraints `Optional` and `Required` to the
`Symfony\Component\Validator\Constraints\` namespace and deprecated the original
classes.
* added comparison validators (EqualTo, NotEqualTo, LessThan, LessThanOrEqualTo, GreaterThan, GreaterThanOrEqualTo, IdenticalTo, NotIdenticalTo)

2.2.0
-----
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/Validator/Constraints/AbstractComparison.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* Used for the comparison of values.
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
abstract class AbstractComparison extends Constraint
{
public $message;
public $value;

/**
* {@inheritDoc}
*/
public function __construct($options = null)
{
if (!isset($options['value'])) {
throw new ConstraintDefinitionException(sprintf(
'The %s constraint requires the "value" option to be set.',
get_class($this)
));
}

parent::__construct($options);
}

/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'value';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* Provides a base class for the validation of property comparisons.
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
abstract class AbstractComparisonValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$this->compareValues($value, $constraint->value, $constraint)) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $this->valueToString($constraint->value),
'{{ compared_value }}' => $this->valueToString($constraint->value),
'{{ compared_value_type }}' => $this->valueToType($constraint->value)
));
}
}

/**
* Returns a string representation of the type of the value.
*
* @param mixed $value
*
* @return string
*/
private function valueToType($value)
{
return is_object($value) ? get_class($value) : gettype($value);
}

/**
* Returns a string representation of the value.
*
* @param mixed $value
*
* @return string
*/
private function valueToString($value)
{
if ($value instanceof \DateTime) {
return $value->format('Y-m-d H:i:s');
}

return var_export($value, true);
}

/**
* Compares the two given values to find if their relationship is valid
*
* @param mixed $value1 The first value to compare
* @param mixed $value2 The second value to compare
*
* @return Boolean true if the relationship is valid, false otherwise
*/
abstract protected function compareValues($value1, $value2);
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/EqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @Annotation
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class EqualTo extends AbstractComparison
{
public $message = 'This value should be equal to {{ compared_value }}';
}
28 changes: 28 additions & 0 deletions src/Symfony/Component/Validator/Constraints/EqualToValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* Validates values are equal (==).
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class EqualToValidator extends AbstractComparisonValidator
{
/**
* @inheritDoc
*/
protected function compareValues($value1, $value2)
{
return $value1 == $value2;
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/GreaterThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @Annotation
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class GreaterThan extends AbstractComparison
{
public $message = 'This value should be greater than {{ compared_value }}';
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @Annotation
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class GreaterThanOrEqual extends AbstractComparison
{
public $message = 'This value should be greater than or equal to {{ compared_value }}';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* Validates values are greater than or equal to the previous (>=).
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class GreaterThanOrEqualValidator extends AbstractComparisonValidator
{
/**
* @inheritDoc
*/
protected function compareValues($value1, $value2)
{
return $value1 >= $value2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* Validates values are greater than the previous (>).
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class GreaterThanValidator extends AbstractComparisonValidator
{
/**
* @inheritDoc
*/
protected function compareValues($value1, $value2)
{
return $value1 > $value2;
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IdenticalTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @Annotation
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class IdenticalTo extends AbstractComparison
{
public $message = 'This value should be identical to {{ compared_value_type }} {{ compared_value }}';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* Validates values are identical (===).
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class IdenticalToValidator extends AbstractComparisonValidator
{
/**
* @inheritDoc
*/
protected function compareValues($value1, $value2)
{
return $value1 === $value2;
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/LessThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @Annotation
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class LessThan extends AbstractComparison
{
public $message = 'This value should be less than {{ compared_value }}';
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/LessThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @Annotation
*
* @author Daniel Holmes <daniel@danielholmes.org>
*/
class LessThanOrEqual extends AbstractComparison
{
public $message = 'This value should be less than or equal to {{ compared_value }}';
}
Loading