1 | <?php |
||
30 | abstract class AbstractAnnotationSpecifiedValidator implements ValidatorInterface |
||
31 | { |
||
32 | /** |
||
33 | * This method checks if the given variable is a validator annotation. |
||
34 | * |
||
35 | * @param mixed $variable This is the variable to check. It should be of type ValidatorAnnotation. |
||
36 | * |
||
37 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
38 | * If variable is not a ValidatorAnnotation. |
||
39 | * |
||
40 | * @return \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface |
||
41 | * Created validator object. |
||
42 | */ |
||
43 | 84 | protected function checkIfAnnotationAndReturnObject($variable) |
|
44 | { |
||
45 | 84 | if (!($variable instanceof ValidatorAnnotation)) { |
|
46 | 1 | throw new UnspecifiedValidatorException( |
|
47 | 'The validator is being initialized without a valid validator Annotation' |
||
48 | 1 | ); |
|
49 | } |
||
50 | |||
51 | 83 | return $this->createValidatorInstanceFromAnnotation($variable); |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Constructs a Validator object from a Validator annotation. |
||
56 | * |
||
57 | * @param \Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation $validatorAnnotation |
||
58 | * |
||
59 | * @return \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface Created validator object. |
||
60 | */ |
||
61 | 83 | protected function createValidatorInstanceFromAnnotation($validatorAnnotation) |
|
62 | { |
||
63 | 83 | return ValidatorBuilder::builder() |
|
|
|||
64 | 83 | ->withSpecification($validatorAnnotation->getValue()) |
|
65 | 83 | ->withClass($validatorAnnotation->getAssociatedClass()) |
|
66 | 83 | ->build(); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | * |
||
72 | * @see \Mcustiel\SimpleRequest\Interfaces\Specificable::setSpecification() |
||
73 | */ |
||
74 | abstract public function setSpecification($specification = null); |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | * |
||
79 | * @see \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface::validate() |
||
80 | */ |
||
81 | abstract public function validate($value); |
||
82 | } |
||
83 |