Permalink
Browse files

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.
  • Loading branch information...
2 parents 172300e + e6e3da5 commit e056480ab285e424675c428d1593f98ce256b2ba @fabpot fabpot committed Jan 16, 2012
@@ -0,0 +1,27 @@
+<?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\Collection;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ */
+class Optional extends Constraint
+{
+ public $constraints = array();
+
+ public function getDefaultOption()
+ {
+ return 'constraints';
+ }
+}
@@ -0,0 +1,27 @@
+<?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\Collection;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ */
+class Required extends Constraint
+{
+ public $constraints = array();
+
+ public function getDefaultOption()
+ {
+ return 'constraints';
+ }
+}
@@ -14,6 +14,8 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
+use Symfony\Component\Validator\Constraints\Collection\Optional;
+use Symfony\Component\Validator\Constraints\Collection\Required;
/**
* @api
@@ -51,12 +53,18 @@ public function isValid($value, Constraint $constraint)
$extraFields[$field] = $fieldValue;
}
- foreach ($constraint->fields as $field => $constraints) {
+ foreach ($constraint->fields as $field => $fieldConstraint) {
if (
// bug fix issue #2779
(is_array($value) && array_key_exists($field, $value)) ||
($value instanceof \ArrayAccess && $value->offsetExists($field))
) {
+ if ($fieldConstraint instanceof Required || $fieldConstraint instanceof Optional) {
+ $constraints = $fieldConstraint->constraints;
+ } else {
+ $constraints = $fieldConstraint;
+ }
+
// cannot simply cast to array, because then the object is converted to an
// array instead of wrapped inside
$constraints = is_array($constraints) ? $constraints : array($constraints);
@@ -66,7 +74,7 @@ public function isValid($value, Constraint $constraint)
}
unset($extraFields[$field]);
- } else {
+ } else if (!$fieldConstraint instanceof Optional) {
$missingFields[] = $field;
}
}
@@ -0,0 +1,20 @@
+<?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\Tests\Component\Validator\Constraints;
+
+class CollectionValidatorArrayObjectTest extends CollectionValidatorTest
+{
+ public function prepareTestData(array $contents)
+ {
+ return new \ArrayObject($contents);
+ }
+}
@@ -0,0 +1,20 @@
+<?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\Tests\Component\Validator\Constraints;
+
+class CollectionValidatorArrayTest extends CollectionValidatorTest
+{
+ public function prepareTestData(array $contents)
+ {
+ return $contents;
+ }
+}
@@ -0,0 +1,80 @@
+<?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\Tests\Component\Validator\Constraints;
+
+/**
+* This class is a hand written simplified version of PHP native `ArrayObject`
+* class, to show that it behaves different than PHP native implementation.
+*/
+class CustomArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
+{
+ private $array;
+
+ public function __construct(array $array = null)
+ {
+ $this->array = (array) ($array ?: array());
+ }
+
+ public function offsetExists($offset)
+ {
+ return array_key_exists($offset, $this->array);
+ }
+
+ public function offsetGet($offset)
+ {
+ return $this->array[$offset];
+ }
+
+ public function offsetSet($offset, $value)
+ {
+ if (null === $offset) {
+ $this->array[] = $value;
+ } else {
+ $this->array[$offset] = $value;
+ }
+ }
+
+ public function offsetUnset($offset)
+ {
+ if (array_key_exists($offset, $this->array)) {
+ unset($this->array[$offset]);
+ }
+ }
+
+ public function getIterator()
+ {
+ return new \ArrayIterator($this->array);
+ }
+
+ public function count()
+ {
+ return count($this->array);
+ }
+
+ public function serialize()
+ {
+ return serialize($this->array);
+ }
+
+ public function unserialize($serialized)
+ {
+ $this->array = (array) unserialize((string) $serialized);
+ }
+}
+
+class CollectionValidatorCustomArrayObjectTest extends CollectionValidatorTest
+{
+ public function prepareTestData(array $contents)
+ {
+ return new CustomArrayObject($contents);
+ }
+}
Oops, something went wrong.

0 comments on commit e056480

Please sign in to comment.