Completed
Branch develop (62f2fb)
by Mariano
03:42
created

setSpecification()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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()
0 ignored issues
show
Bug Compatibility introduced by
The expression \Mcustiel\SimpleRequest\...iatedClass())->build(); of type Mcustiel\SimpleRequest\I...aces\ValidatorInterface adds the type Mcustiel\SimpleRequest\Interfaces\FilterInterface to the return on line 63 which is incompatible with the return type documented by Mcustiel\SimpleRequest\V...rInstanceFromAnnotation of type Mcustiel\SimpleRequest\I...aces\ValidatorInterface.
Loading history...
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