Skip to content

Commit 0710c26

Browse files
ISSUE-3: Implemented CollectionValidator
1 parent 064ab53 commit 0710c26

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

src/Collection.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
namespace DarkWebDesign\SymfonyAddon\Constraint;
22+
23+
use Symfony\Component\Validator\Constraint;
24+
use Symfony\Component\Validator\Constraints\Valid;
25+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
26+
27+
/**
28+
* Collection constraint.
29+
*
30+
* @Annotation
31+
*
32+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
33+
*
34+
* @author Raymond Schouten
35+
*
36+
* @since 2.3
37+
*/
38+
class Collection extends Constraint
39+
{
40+
/** @var \Symfony\Component\Validator\Constraint[] */
41+
public $constraints;
42+
43+
/**
44+
* Constructor.
45+
*
46+
* @param mixed $options
47+
*/
48+
public function __construct($options = null)
49+
{
50+
parent::__construct($options);
51+
52+
if (!is_array($this->constraints)) {
53+
throw new ConstraintDefinitionException(sprintf(
54+
'The option "constraints" is expected to be an array in constraint %s',
55+
__CLASS__
56+
));
57+
}
58+
59+
foreach ($this->constraints as $constraint) {
60+
if (!$constraint instanceof Constraint) {
61+
throw new ConstraintDefinitionException(
62+
sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, __CLASS__)
63+
);
64+
}
65+
66+
if ($constraint instanceof Valid) {
67+
throw new ConstraintDefinitionException(
68+
sprintf(
69+
'The constraint Valid cannot be nested inside constraint %s. ' .
70+
'You can only declare the Valid constraint directly on a field or method.',
71+
__CLASS__
72+
)
73+
);
74+
}
75+
}
76+
}
77+
78+
/**
79+
* Returns the name of the default option.
80+
*
81+
* @return string
82+
*/
83+
public function getDefaultOption()
84+
{
85+
return 'constraints';
86+
}
87+
88+
/**
89+
* Returns the name of the required options.
90+
*
91+
* @return array
92+
*/
93+
public function getRequiredOptions()
94+
{
95+
return array('constraints');
96+
}
97+
}

src/CollectionValidator.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
namespace DarkWebDesign\SymfonyAddon\Constraint;
22+
23+
use ArrayAccess;
24+
use DarkWebDesign\SymfonyAddon\Constraint\Collection;
25+
use Symfony\Component\Validator\Constraint;
26+
use Symfony\Component\Validator\ConstraintValidator;
27+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
28+
use Traversable;
29+
30+
/**
31+
* Collection validator.
32+
*
33+
* @author Raymond Schouten
34+
*
35+
* @since 2.3
36+
*/
37+
class CollectionValidator extends ConstraintValidator
38+
{
39+
/**
40+
* Checks if the value is valid.
41+
*
42+
* @param mixed $value
43+
* @param \Symfony\Component\Validator\Constraint $constraint
44+
*
45+
* @throws \Symfony\Component\Validator\Exception\UnexpectedTypeException
46+
*/
47+
public function validate($value, Constraint $constraint)
48+
{
49+
if (!$constraint instanceof Collection) {
50+
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Collection');
51+
}
52+
53+
if (null === $value) {
54+
return;
55+
}
56+
57+
if (!is_array($value) && !$value instanceof Traversable) {
58+
throw new UnexpectedTypeException($value, 'array or Traversable');
59+
}
60+
61+
foreach ($value as $field => $fieldValue) {
62+
foreach ($constraint->constraints as $fieldConstraint) {
63+
$this->context->validateValue($fieldValue, $fieldConstraint, '[' . $field . ']');
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)