Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] added improve support for collection validation #9988

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function validate($value, Constraint $constraint)
/**
* Returns a string representation of the type of the value.
*
* @param mixed $value
* @param mixed $value
*
* @return string
*/
Expand All @@ -54,7 +54,7 @@ private function valueToType($value)
/**
* Returns a string representation of the value.
*
* @param mixed $value
* @param mixed $value
*
* @return string
*/
Expand All @@ -74,8 +74,8 @@ private function valueToString($value)
/**
* 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
* @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
*/
Expand Down
121 changes: 121 additions & 0 deletions src/Symfony/Component/Validator/Constraints/AbstractComposite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

/**
* @Annotation
*
* @author Marc Morales Valldepérez <[email protected]>
* @author Marc Morera Merino <[email protected]>
*/
abstract class AbstractComposite extends Constraint
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should remove this empty line

/**
* @var array
*
* Set of constraints
*/
public $constraints = array();

/**
* {@inheritDoc}
*/
public function __construct($options = null)
{
parent::__construct($options);

if (!is_array($this->constraints)) {
$this->constraints = array($this->constraints);
}

// We consider explicid groups are defined if are not default one
$areExplicitGroupsDefined = ( $this->groups != array(self::DEFAULT_GROUP));

// Each constraint contained
foreach ($this->constraints as $constraint) {
if (!$constraint instanceof Constraint) {
throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, __CLASS__));
}

if ($constraint instanceof Valid) {
throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint %s. You can only declare the Valid constraint directly on a field or method.', __CLASS__));
}

// If explicid groups are defined
if ($areExplicitGroupsDefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use isset($options['groups']) instead.

/**
* If constraint has explicid groups defined
*
* In that case, the groups of the nested constraint need to be
* a subset of the groups of the outer constraint.
*/
if ($constraint->groups !== array(self::DEFAULT_GROUP)) {

// If are not a subset
if ($constraint->groups != array_intersect($constraint->groups, $this->groups)) {
throw new ConstraintDefinitionException(sprintf('The groups defined in Constraint %s must be a subset of the groups defined in the Constraint %s', $constraint, __CLASS__));
}

// Otherwise, we add all defined groups here
} else {
foreach ($this->groups as $group) {
$constraint->addImplicitGroupName($group);
}
}

/**
* Otherwise, we merge current groups with constraint
*/
} else {
$this->groups = array_unique(array_merge($this->groups, $constraint->groups));
}
}
}

/**
* Adds the given group if this constraint is in the Default group.
*
* Also propagate same method to nested Constraints.
*
* @param string $group
*
* @api
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the @api tag

*/
public function addImplicitGroupName($group)
{
parent::addImplicitGroupName($group);

foreach ($this->constraints as $constraint) {
$constraint->addImplicitGroupName($group);
}
}

/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'constraints';
}

/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
return array('constraints');
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* @author Bernhard Schussek <[email protected]>
*
* @api
*
* @deprecated Deprecated in 2.5, to be removed in 3.0. Use
* {@link \Symfony\Component\Validator\Constraints\Each} instead.
*/
class All extends Constraint
{
Expand Down Expand Up @@ -56,4 +59,5 @@ public function getRequiredOptions()
{
return array('constraints');
}

}
3 changes: 3 additions & 0 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* @author Bernhard Schussek <[email protected]>
*
* @api
*
* @deprecated Deprecated in 2.5, to be removed in 3.0. Use
* {@link \Symfony\Component\Validator\Constraints\EachValidator} instead.
*/
class AllValidator extends ConstraintValidator
{
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Each.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 <[email protected]>
*
* 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 Marc Morera Merino <[email protected]>
* @author Marc Morales Valldepérez <[email protected]> *
*/
class Each extends AbstractComposite
{
}
46 changes: 46 additions & 0 deletions src/Symfony/Component/Validator/Constraints/EachValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Marc Morera Merino <[email protected]>
* @author Marc Morales Valldepérez <[email protected]>
*/
class EachValidator extends ConstraintValidator
{

/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value) {
return;
}

if (!is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
}

$group = $this->context->getGroup();

foreach ($value as $key => $element) {
foreach ($constraint->constraints as $constr) {
$this->context->validateValue($element, $constr, '[' . $key . ']', $group);
}
}
}
}
29 changes: 29 additions & 0 deletions src/Symfony/Component/Validator/Constraints/None.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 Marc Morera Merino <[email protected]>
* @author Marc Morales Valldepérez <[email protected]>
*/
class None extends AbstractComposite
{

/**
* @var string
*
* Message for notice Violation
*/
public $message = 'None of this collection should pass validation.';
}
57 changes: 57 additions & 0 deletions src/Symfony/Component/Validator/Constraints/NoneValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Marc Morera Merino <[email protected]>
* @author Marc Morales Valldepérez <[email protected]>
*/
class NoneValidator extends ConstraintValidator
{

/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value) {
return;
}

if (!is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
}

$group = $this->context->getGroup();

$totalIterations = count($value) * count($constraint->constraints);

foreach ($value as $key => $element) {
foreach ($constraint->constraints as $constr) {
$this->context->validateValue($element, $constr, '[' . $key . ']', $group);
}
}

$constraintsSuccess = $totalIterations - (int) $this->context->getViolations()->count();

//We clear all violations as just current Validator should add real Violations
$this->context->clearViolations();

if ($constraintsSuccess > 0) {
$this->context->addViolation($constraint->message);
}
}
}
Loading