1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Accessible\Reader; |
4
|
|
|
|
5
|
|
|
use \Accessible\Configuration; |
6
|
|
|
|
7
|
|
|
class ConstraintsReader extends Reader |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name of the annotation class that enable the constraints validation for a class. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private static $enableConstraintsValidationAnnotationClass = "Accessible\\Annotation\\EnableConstraintsValidation"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name of the annotation class that disable the constraints validation for a class. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private static $disableConstraintsValidationAnnotationClass = "Accessible\\Annotation\\DisableConstraintsValidation"; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Indicates wether the constraints validation is enabled or not for the given object. |
25
|
|
|
* |
26
|
|
|
* @param array $objectClasses The classes of the object to read. |
27
|
|
|
* @param Reader $objectClasses The annotation reader to use. |
28
|
|
|
* |
29
|
|
|
* @return boolean True if the validation is enabled, else false. |
30
|
|
|
*/ |
31
|
6 |
|
public static function isConstraintsValidationEnabled($objectClasses, $annotationReader) |
32
|
|
|
{ |
33
|
6 |
|
$enabled = true; |
34
|
|
|
|
35
|
6 |
|
foreach ($objectClasses as $class) { |
36
|
6 |
|
if ($annotationReader->getClassAnnotation($class, self::$disableConstraintsValidationAnnotationClass) !== null) { |
37
|
1 |
|
$enabled = false; |
38
|
1 |
|
break; |
39
|
|
|
} |
40
|
5 |
|
if ($annotationReader->getClassAnnotation($class, self::$enableConstraintsValidationAnnotationClass) !== null) { |
41
|
1 |
|
$enabled = true; |
42
|
1 |
|
break; |
43
|
|
|
} |
44
|
6 |
|
} |
45
|
|
|
|
46
|
6 |
|
return $enabled; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Validates the given value compared to given property constraints. |
51
|
|
|
* If the value is valid, a call to `count` to the object returned |
52
|
|
|
* by this method should give 0. |
53
|
|
|
* |
54
|
|
|
* @param object $object The object to compare. |
55
|
|
|
* @param string $property The name of the reference property. |
56
|
|
|
* @param mixed $value The value to check. |
57
|
|
|
* |
58
|
|
|
* @return Symfony\Component\Validator\ConstraintViolationList |
59
|
|
|
* The list of constraints violations the check returns. |
60
|
|
|
*/ |
61
|
28 |
|
public static function validatePropertyValue($object, $property, $value) |
62
|
|
|
{ |
63
|
28 |
|
return Configuration::getConstraintsValidator()->validatePropertyValue($object, $property, $value); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|