Completed
Branch develop (22789e)
by Mariano
08:33
created

PropertyParserBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
    public function __construct($name)
30
    {
31
        $this->name = $name;
32
    }
33
34
    /**
35
     * Adds a validator to the parser.
36
     *
37
     * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator
38
     */
39
    public function addValidator(ValidatorInterface $validator)
40
    {
41
        $this->validators[] = $validator;
42
        return $this;
43
    }
44
45
    /**
46
     * Adds a filter to the parser.
47
     *
48
     * @param \Mcustiel\SimpleRequest\Interfaces\FilterInterface $filter
49
     */
50
    public function addFilter(FilterInterface $filter)
51
    {
52
        $this->filters[] = $filter;
53
        return $this;
54
    }
55
56
    /**
57
     * Sets the type to parse the object to.
58
     *
59
     * @param string $type
60
     */
61
    public function parseAs($type)
62
    {
63
        $this->type = $type;
64
        return $this;
65
    }
66
67
    /**
68
     * @param \Mcustiel\SimpleRequest\RequestBuilder $requestBuilder
69
     */
70
    public function build(RequestBuilder $requestBuilder)
71
    {
72
        return $this->createPropertyParser($requestBuilder)
73
            ->setFilters($this->filters)
74
            ->setValidators($this->validators);
75
    }
76
77
    private function createPropertyParser(RequestBuilder $requestBuilder)
78
    {
79
        if ($this->type) {
80
            return new PropertyParserToObject($this->name, $this->type, $requestBuilder);
81
        }
82
        return new SimplePropertyParser($this->name);
83
    }
84
}
85