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
|
97 |
|
public function __construct($name) |
30
|
|
|
{ |
31
|
97 |
|
$this->name = $name; |
32
|
97 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Adds a validator to the parser. |
36
|
|
|
* |
37
|
|
|
* @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator |
38
|
|
|
*/ |
39
|
95 |
|
public function addValidator(ValidatorInterface $validator) |
40
|
|
|
{ |
41
|
95 |
|
$this->validators[] = $validator; |
42
|
95 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Adds a filter to the parser. |
47
|
|
|
* |
48
|
|
|
* @param \Mcustiel\SimpleRequest\Interfaces\FilterInterface $filter |
49
|
|
|
*/ |
50
|
16 |
|
public function addFilter(FilterInterface $filter) |
51
|
|
|
{ |
52
|
16 |
|
$this->filters[] = $filter; |
53
|
16 |
|
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
|
97 |
|
public function build(RequestBuilder $requestBuilder) |
71
|
|
|
{ |
72
|
97 |
|
return $this->createPropertyParser($requestBuilder) |
73
|
97 |
|
->setFilters($this->filters) |
74
|
97 |
|
->setValidators($this->validators); |
75
|
|
|
} |
76
|
|
|
|
77
|
97 |
|
private function createPropertyParser(RequestBuilder $requestBuilder) |
78
|
|
|
{ |
79
|
97 |
|
if ($this->type) { |
80
|
2 |
|
return new PropertyParserToObject($this->name, $this->type, $requestBuilder); |
81
|
|
|
} |
82
|
97 |
|
return new SimplePropertyParser($this->name); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|