1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the rafrsr/lib-array2object package. |
5
|
|
|
* |
6
|
|
|
* (c) Rafael SR <https://github.com/rafrsr> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Rafrsr\LibArray2Object; |
12
|
|
|
|
13
|
|
|
use Rafrsr\LibArray2Object\Parser\ArrayParser; |
14
|
|
|
use Rafrsr\LibArray2Object\Parser\BooleanParser; |
15
|
|
|
use Rafrsr\LibArray2Object\Parser\DateTimeParser; |
16
|
|
|
use Rafrsr\LibArray2Object\Parser\FloatParser; |
17
|
|
|
use Rafrsr\LibArray2Object\Parser\IntegerParser; |
18
|
|
|
use Rafrsr\LibArray2Object\Parser\ObjectParser; |
19
|
|
|
use Rafrsr\LibArray2Object\Parser\StringParser; |
20
|
|
|
use Rafrsr\LibArray2Object\Parser\ValueParserInterface; |
21
|
|
|
use Rafrsr\LibArray2Object\Traits\ParsersAwareTrait; |
22
|
|
|
|
23
|
|
|
abstract class AbstractBuilder |
24
|
|
|
{ |
25
|
|
|
use ParsersAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AbstractContext |
29
|
|
|
*/ |
30
|
|
|
private $context; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $disabledParsers = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* create builder instance. |
39
|
|
|
* |
40
|
|
|
* @return static |
41
|
|
|
*/ |
42
|
|
|
public static function create() |
43
|
|
|
{ |
44
|
|
|
return new static(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return AbstractContext |
49
|
|
|
*/ |
50
|
|
|
public function getContext() |
51
|
|
|
{ |
52
|
|
|
return $this->context; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param AbstractContext $context |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setContext($context) |
61
|
|
|
{ |
62
|
|
|
$this->context = $context; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ValueParserInterface $parser |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function addParser(ValueParserInterface $parser) |
73
|
|
|
{ |
74
|
|
|
$this->parsers[$parser->getName()] = $parser; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|ValueParserInterface $parser |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function hasParser($parser) |
85
|
|
|
{ |
86
|
|
|
if ($parser instanceof ValueParserInterface) { |
87
|
|
|
$parser = $parser->getName(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return array_key_exists($parser, $this->parsers); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* disableParser. |
95
|
|
|
* |
96
|
|
|
* @param string|ValueParserInterface $parser |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function disableParser($parser) |
101
|
|
|
{ |
102
|
|
|
if ($parser instanceof ValueParserInterface) { |
103
|
|
|
$parser = $parser->getName(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_string($parser)) { |
107
|
|
|
$this->disabledParsers[] = $parser; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getDisabledParsers() |
117
|
|
|
{ |
118
|
|
|
return $this->disabledParsers; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string|ValueParserInterface $parser |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function removeParser($parser) |
127
|
|
|
{ |
128
|
|
|
if ($parser instanceof ValueParserInterface) { |
129
|
|
|
$parser = $parser->getName(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (is_string($parser) && $this->hasParser($parser)) { |
133
|
|
|
unset($this->parsers[$parser]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param AbstractContext $context |
141
|
|
|
*/ |
142
|
|
|
protected function prepareContext(AbstractContext $context) |
143
|
|
|
{ |
144
|
|
|
//defaults |
145
|
|
|
$context->setParsers( |
146
|
|
|
[ |
147
|
|
|
new StringParser(), |
148
|
|
|
new BooleanParser(), |
149
|
|
|
new IntegerParser(), |
150
|
|
|
new FloatParser(), |
151
|
|
|
new DateTimeParser(), |
152
|
|
|
new ArrayParser(), |
153
|
|
|
new ObjectParser($context), |
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
//add custom parsers |
158
|
|
|
foreach ($this->getParsers() as $parser) { |
159
|
|
|
if ($parser instanceof ValueParserInterface) { |
160
|
|
|
$context->prependParser($parser); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
//disable parsers |
165
|
|
|
foreach ($this->getDisabledParsers() as $disabledParser) { |
166
|
|
|
$actualParsers = $context->getParsers(); |
167
|
|
|
if (array_key_exists($disabledParser, $actualParsers)) { |
168
|
|
|
unset($actualParsers[$disabledParser]); |
169
|
|
|
} |
170
|
|
|
$context->setParsers($actualParsers); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
abstract public function build(); |
175
|
|
|
} |
176
|
|
|
|