|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Validator\Mapping\ElementMetadata; |
|
|
15 |
use Symfony\Component\Validator\Mapping\ClassMetadata; |
|
|
16 |
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* The default implementation of the ValidatorInterface. |
|
|
20 |
* |
|
|
21 |
* This service can be used to validate objects, properties and raw values |
|
|
22 |
* against constraints. |
|
|
23 |
* |
|
|
24 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
25 |
* @author Bernhard Schussek <bernhard.schussek@symfony.com> |
|
|
26 |
* |
|
|
27 |
* @api |
|
|
28 |
*/ |
|
|
29 |
class Validator implements ValidatorInterface |
|
|
30 |
{ |
|
|
31 |
protected $metadataFactory; |
|
|
32 |
protected $validatorFactory; |
|
|
33 |
protected $validatorInitializers; |
|
|
34 |
|
|
|
35 |
public function __construct( |
|
|
36 |
ClassMetadataFactoryInterface $metadataFactory, |
|
|
37 |
ConstraintValidatorFactoryInterface $validatorFactory, |
|
|
38 |
array $validatorInitializers = array() |
|
|
39 |
) |
|
|
40 |
{ |
|
|
41 |
$this->metadataFactory = $metadataFactory; |
|
|
42 |
$this->validatorFactory = $validatorFactory; |
|
|
43 |
$this->validatorInitializers = $validatorInitializers; |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* {@inheritDoc} |
|
|
48 |
*/ |
|
|
49 |
public function getMetadataFactory() |
|
|
50 |
{ |
|
|
51 |
return $this->metadataFactory; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
/** |
|
|
55 |
* {@inheritDoc} |
|
|
56 |
* |
|
|
57 |
* @api |
|
|
58 |
*/ |
|
|
59 |
public function validate($object, $groups = null) |
|
|
60 |
{ |
|
|
61 |
$metadata = $this->metadataFactory->getClassMetadata(get_class($object)); |
|
|
62 |
|
|
|
63 |
$walk = function(GraphWalker $walker, $group) use ($metadata, $object) { |
|
|
64 |
return $walker->walkObject($metadata, $object, $group, ''); |
|
|
65 |
}; |
|
|
66 |
|
|
|
67 |
return $this->validateGraph($object, $walk, $groups); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
/** |
|
|
71 |
* {@inheritDoc} |
|
|
72 |
* |
|
|
73 |
* @api |
|
|
74 |
*/ |
|
|
75 |
public function validateProperty($object, $property, $groups = null) |
|
|
76 |
{ |
|
|
77 |
$metadata = $this->metadataFactory->getClassMetadata(get_class($object)); |
|
|
78 |
|
|
|
79 |
$walk = function(GraphWalker $walker, $group) use ($metadata, $property, $object) { |
|
|
80 |
return $walker->walkProperty($metadata, $property, $object, $group, ''); |
|
|
81 |
}; |
|
|
82 |
|
|
|
83 |
return $this->validateGraph($object, $walk, $groups); |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
/** |
|
|
87 |
* {@inheritDoc} |
|
|
88 |
* |
|
|
89 |
* @api |
|
|
90 |
*/ |
|
|
91 |
public function validatePropertyValue($class, $property, $value, $groups = null) |
|
|
92 |
{ |
|
|
93 |
$metadata = $this->metadataFactory->getClassMetadata($class); |
|
|
94 |
|
|
|
95 |
$walk = function(GraphWalker $walker, $group) use ($metadata, $property, $value) { |
|
|
96 |
return $walker->walkPropertyValue($metadata, $property, $value, $group, ''); |
|
|
97 |
}; |
|
|
98 |
|
|
|
99 |
return $this->validateGraph($class, $walk, $groups); |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* {@inheritDoc} |
|
|
104 |
* |
|
|
105 |
* @api |
|
|
106 |
*/ |
|
|
107 |
public function validateValue($value, Constraint $constraint, $groups = null) |
|
|
108 |
{ |
|
|
109 |
$walk = function(GraphWalker $walker, $group) use ($constraint, $value) { |
|
|
110 |
return $walker->walkConstraint($constraint, $value, $group, ''); |
|
|
111 |
}; |
|
|
112 |
|
|
|
113 |
return $this->validateGraph($value, $walk, $groups); |
|
|
114 |
} |
|
|
115 |
|
|
|
116 |
protected function validateGraph($root, \Closure $walk, $groups = null) |
|
|
117 |
{ |
|
|
118 |
$walker = new GraphWalker($root, $this->metadataFactory, $this->validatorFactory, $this->validatorInitializers); |
|
|
119 |
$groups = $groups ? (array) $groups : array(Constraint::DEFAULT_GROUP); |
|
|
120 |
|
|
|
121 |
foreach ($groups as $group) { |
|
|
122 |
$walk($walker, $group); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
return $walker->getViolations(); |
|
|
126 |
} |
|
|
127 |
} |