AnnotationToImplementationBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 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\Util;
19
20
/**
21
 * Trait with the needed methods to build an object from an annotation. The class
22
 * that use this trait shouls implement the method getClassForType. It's used by
23
 * the classes that builds validators or filters.
24
 *
25
 * @author mcustiel
26
 */
27
abstract class AnnotationToImplementationBuilder
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $type;
33
    /**
34
     * @var mixed
35
     */
36
    protected $specification;
37
38
    /**
39
     * Creator method. Creates an instance of this object.
40
     * return $this
41
     */
42 132
    public static function builder()
43
    {
44 132
        return new static;
45
    }
46
47
    /**
48
     * Sets the class name.
49
     *
50
     * @param string $type Name of the class given by the annotation.
51
     *
52
     * @return $this For fluent interface
53
     */
54 132
    public function withClass($type)
55
    {
56 132
        $this->type = $type;
57 132
        return $this;
58
    }
59
60
    /**
61
     * Sets the specification of the validation or filter.
62
     *
63
     * @param mixed $specification Specification set in the annotation.
64
     *
65
     * @return $this For fluent interface
66
     */
67 128
    public function withSpecification($specification)
68
    {
69 128
        $this->specification = $specification;
70 128
        return $this;
71
    }
72
73
    /**
74
     * Builds the object from the given specification.
75
     *
76
     * @return \Mcustiel\SimpleRequest\Interfaces\FilterInterface|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface
77
     */
78 132
    public function build()
79
    {
80 132
        $class = $this->getClassForType($this->type);
81 128
        $validator = new $class;
82 128
        $validator->setSpecification($this->specification);
83
84 128
        return $validator;
85
    }
86
87
    /**
88
     * @param string $type The type to instantiate.
89
     *
90
     * @return \Mcustiel\SimpleRequest\Interfaces\FilterInterface|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface
91
     *
92
     * @throws \Mcustiel\SimpleRequest\Exception\FilterDoesNotExistException
93
     * @throws \Mcustiel\SimpleRequest\Exception\ValidatorDoesNotExistException
94
     */
95
    abstract protected function getClassForType($type);
96
}
97