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

Skip to content

Commit e056480

Browse files
committed
merged branch bschussek/collection-validator (PR #3118)
Commits ------- e6e3da5 [Validator] Improved test coverage of CollectionValidator and reduced test code duplication 509c7bf [Validator] Moved Optional and Required constraints to dedicated sub namespace. bf59018 [Validator] Removed @api-tag from Optional and Required constraint, since these two are new. 6641f3e [Validator] Added constraints Optional and Required for the CollectionValidator Discussion ---------- [Validator] Improve support for optional/required fields in Collection constraint Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: none Todo: none ![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=collection-validator) Improves the `Collection` constraint to test on a more granular level if entries of the collection are optional or required. Before this could only be set using the "allowExtraFields" and "allowMissingFields" options, but these are very general and limited. The former syntax - without Optional or Required - is still supported. Usage: $array = array( 'name' => 'Bernhard', 'birthdate' => '1970-01-01', ); $validator->validate($array, null, new Collection(array( 'name' => new Required(), 'birthdate' => new Optional(), )); // you can also pass additional constraints for the fields $validator->validate($array, null, new Collection(array( 'name' => new Required(array( new Type('string'), new MinLength(3), )), 'birthdate' => new Optional(new Date()), )); --------------------------------------------------------------------------- by canni at 2012-01-15T20:22:17Z @bschussek I've rewritten a lot of test code for Collection validator in 2.0 branch and also had modified validator itself, as it had a bug #3078, consider waiting with this PR till fabpot will merge 2.0 back into master, as there will be code conflicts :) --------------------------------------------------------------------------- by Koc at 2012-01-15T23:13:04Z Does it helps to #2615 ? --------------------------------------------------------------------------- by fabpot at 2012-01-16T06:44:53Z @canni: I've just merged 2.0 into master. --------------------------------------------------------------------------- by bschussek at 2012-01-16T12:05:19Z @fabpot: Rebased. I also fixed the CS issues mentioned by @stof.
2 parents 172300e + e6e3da5 commit e056480

File tree

7 files changed

+359
-186
lines changed

7 files changed

+359
-186
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints\Collection;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
*/
19+
class Optional extends Constraint
20+
{
21+
public $constraints = array();
22+
23+
public function getDefaultOption()
24+
{
25+
return 'constraints';
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints\Collection;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
*/
19+
class Required extends Constraint
20+
{
21+
public $constraints = array();
22+
23+
public function getDefaultOption()
24+
{
25+
return 'constraints';
26+
}
27+
}

src/Symfony/Component/Validator/Constraints/CollectionValidator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Constraints\Collection\Optional;
18+
use Symfony\Component\Validator\Constraints\Collection\Required;
1719

1820
/**
1921
* @api
@@ -51,12 +53,18 @@ public function isValid($value, Constraint $constraint)
5153
$extraFields[$field] = $fieldValue;
5254
}
5355

54-
foreach ($constraint->fields as $field => $constraints) {
56+
foreach ($constraint->fields as $field => $fieldConstraint) {
5557
if (
5658
// bug fix issue #2779
5759
(is_array($value) && array_key_exists($field, $value)) ||
5860
($value instanceof \ArrayAccess && $value->offsetExists($field))
5961
) {
62+
if ($fieldConstraint instanceof Required || $fieldConstraint instanceof Optional) {
63+
$constraints = $fieldConstraint->constraints;
64+
} else {
65+
$constraints = $fieldConstraint;
66+
}
67+
6068
// cannot simply cast to array, because then the object is converted to an
6169
// array instead of wrapped inside
6270
$constraints = is_array($constraints) ? $constraints : array($constraints);
@@ -66,7 +74,7 @@ public function isValid($value, Constraint $constraint)
6674
}
6775

6876
unset($extraFields[$field]);
69-
} else {
77+
} else if (!$fieldConstraint instanceof Optional) {
7078
$missingFields[] = $field;
7179
}
7280
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Validator\Constraints;
13+
14+
class CollectionValidatorArrayObjectTest extends CollectionValidatorTest
15+
{
16+
public function prepareTestData(array $contents)
17+
{
18+
return new \ArrayObject($contents);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Validator\Constraints;
13+
14+
class CollectionValidatorArrayTest extends CollectionValidatorTest
15+
{
16+
public function prepareTestData(array $contents)
17+
{
18+
return $contents;
19+
}
20+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Validator\Constraints;
13+
14+
/**
15+
* This class is a hand written simplified version of PHP native `ArrayObject`
16+
* class, to show that it behaves different than PHP native implementation.
17+
*/
18+
class CustomArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
19+
{
20+
private $array;
21+
22+
public function __construct(array $array = null)
23+
{
24+
$this->array = (array) ($array ?: array());
25+
}
26+
27+
public function offsetExists($offset)
28+
{
29+
return array_key_exists($offset, $this->array);
30+
}
31+
32+
public function offsetGet($offset)
33+
{
34+
return $this->array[$offset];
35+
}
36+
37+
public function offsetSet($offset, $value)
38+
{
39+
if (null === $offset) {
40+
$this->array[] = $value;
41+
} else {
42+
$this->array[$offset] = $value;
43+
}
44+
}
45+
46+
public function offsetUnset($offset)
47+
{
48+
if (array_key_exists($offset, $this->array)) {
49+
unset($this->array[$offset]);
50+
}
51+
}
52+
53+
public function getIterator()
54+
{
55+
return new \ArrayIterator($this->array);
56+
}
57+
58+
public function count()
59+
{
60+
return count($this->array);
61+
}
62+
63+
public function serialize()
64+
{
65+
return serialize($this->array);
66+
}
67+
68+
public function unserialize($serialized)
69+
{
70+
$this->array = (array) unserialize((string) $serialized);
71+
}
72+
}
73+
74+
class CollectionValidatorCustomArrayObjectTest extends CollectionValidatorTest
75+
{
76+
public function prepareTestData(array $contents)
77+
{
78+
return new CustomArrayObject($contents);
79+
}
80+
}

0 commit comments

Comments
 (0)