|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of php-simple-request. |
|
4
|
|
|
* |
|
5
|
|
|
* php-simple-request is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* php-simple-request is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with php-simple-request. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace Mcustiel\SimpleRequest\Validator; |
|
19
|
|
|
|
|
20
|
|
|
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface; |
|
21
|
|
|
use Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException; |
|
22
|
|
|
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation; |
|
23
|
|
|
use Mcustiel\SimpleRequest\Util\ValidatorBuilder; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Base class for Validators that are specified with other validator annotations. |
|
27
|
|
|
* |
|
28
|
|
|
* @author mcustiel |
|
29
|
|
|
*/ |
|
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
|
|
|
|