Completed
Pull Request — master (#3)
by Mariano
09:11
created

PropertyParserBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace Mcustiel\SimpleRequest\Strategies;
3
4
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface;
5
use Mcustiel\SimpleRequest\Interfaces\FilterInterface;
6
use Mcustiel\SimpleRequest\RequestBuilder;
7
use Mcustiel\SimpleRequest\Strategies\Properties\SimplePropertyParser;
8
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParserToObject;
9
10
class PropertyParserBuilder
11
{
12
    /**
13
     * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[]
14
     */
15
    private $validators = [];
16
    /**
17
     * @var \Mcustiel\SimpleRequest\Interfaces\FilterInterface[]
18
     */
19
    private $filters = [];
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
    /**
25
     * @var string
26
     */
27
    private $type;
28
29 95
    public function __construct($name)
30
    {
31 95
        $this->name = $name;
32 95
    }
33
34
    /**
35
     * Adds a validator to the parser.
36
     *
37
     * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator
38
     */
39 93
    public function addValidator(ValidatorInterface $validator)
40
    {
41 93
        $this->validators[] = $validator;
42 93
        return $this;
43
    }
44
45
    /**
46
     * Adds a filter to the parser.
47
     *
48
     * @param \Mcustiel\SimpleRequest\Interfaces\FilterInterface $filter
49
     */
50 14
    public function addFilter(FilterInterface $filter)
51
    {
52 14
        $this->filters[] = $filter;
53 14
        return $this;
54
    }
55
56
    /**
57
     * Sets the type to parse the object to.
58
     *
59
     * @param string $type
60
     */
61 2
    public function parseAs($type)
62
    {
63 2
        $this->type = $type;
64 2
        return $this;
65
    }
66
67
    /**
68
     * @param \Mcustiel\SimpleRequest\RequestBuilder $requestBuilder
69
     */
70 95
    public function build(RequestBuilder $requestBuilder)
71
    {
72 95
        return $this->createPropertyParser($requestBuilder)
73 95
            ->setFilters($this->filters)
74 95
            ->setValidators($this->validators);
75
    }
76
77 95
    private function createPropertyParser(RequestBuilder $requestBuilder)
78
    {
79 95
        if ($this->type) {
80 2
            return new PropertyParserToObject($this->name, $this->type, $requestBuilder);
81
        }
82 95
        return new SimplePropertyParser($this->name);
83
    }
84
}
85