-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] Improve support for optional/required fields in Collection constraint #3118
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6641f3e
[Validator] Added constraints Optional and Required for the Collectio…
webmozart bf59018
[Validator] Removed @api-tag from Optional and Required constraint, s…
webmozart 509c7bf
[Validator] Moved Optional and Required constraints to dedicated sub …
webmozart e6e3da5
[Validator] Improved test coverage of CollectionValidator and reduced…
webmozart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Validator/Constraints/Collection/Optional.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Validator/Constraints/Collection/Required.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...ymfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
elseif
without space