|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer\Field\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\CiffRenderer\Field\Builder\BuilderInterface; |
|
6
|
|
|
use Graze\CiffRenderer\Field\Renderer\RendererInterface; |
|
7
|
|
|
use Graze\CiffRenderer\Field\Parser\ParserInterface; |
|
8
|
|
|
use Graze\CiffRenderer\Field\Parser\ParserManager; |
|
9
|
|
|
use Intervention\Image\ImageManager; |
|
10
|
|
|
use \SimpleXMLElement; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractBuilder implements BuilderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ParserManager |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $parserManager; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var SimpleXMLElement |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $xmlField; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ImageManager |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $imageManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var SimpleXMLElement |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $xmlHeader; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var callable |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $fontResolver; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var callable |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $graphicResolver; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $tracerColour; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return ParserInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function instantiateParser(); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param ParserInterface $parser |
|
56
|
|
|
* |
|
57
|
|
|
* @return ParserInterface |
|
58
|
|
|
*/ |
|
59
|
6 |
|
protected function buildParser(ParserInterface $parser) |
|
60
|
|
|
{ |
|
61
|
6 |
|
$parser->setXmlField($this->getXmlField()); |
|
62
|
6 |
|
$parser->setParserManager($this->getParserManager()); |
|
63
|
|
|
|
|
64
|
6 |
|
return $parser; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param ParserInterface $parser |
|
69
|
|
|
* |
|
70
|
|
|
* @return RendererInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
abstract protected function instantiateRenderer(ParserInterface $parser); |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param RendererInterface $renderer |
|
76
|
|
|
* @param ParserInterface $parser |
|
77
|
|
|
* |
|
78
|
|
|
* @return RendererInterface |
|
79
|
|
|
*/ |
|
80
|
6 |
|
protected function buildRenderer(RendererInterface $renderer, ParserInterface $parser) |
|
81
|
|
|
{ |
|
82
|
6 |
|
$renderer->setParser($parser); |
|
83
|
6 |
|
$renderer->setImageManager($this->getImageManager()); |
|
84
|
6 |
|
$renderer->setScale($this->getScale()); |
|
85
|
6 |
|
$renderer->setTracerColour($this->getTracerColour()); |
|
86
|
|
|
|
|
87
|
6 |
|
return $renderer; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return RendererInterface |
|
92
|
|
|
*/ |
|
93
|
6 |
|
public function build() |
|
94
|
|
|
{ |
|
95
|
6 |
|
$parser = $this->instantiateParser(); |
|
96
|
|
|
|
|
97
|
6 |
|
$parser = $this->buildParser($parser); |
|
98
|
|
|
|
|
99
|
|
|
// store it in the manager - other parsers may depend on it |
|
100
|
6 |
|
$this->getParserManager()->addParser($parser); |
|
101
|
|
|
|
|
102
|
6 |
|
$renderer = $this->instantiateRenderer($parser); |
|
103
|
|
|
|
|
104
|
6 |
|
return $this->buildRenderer($renderer, $parser); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param ParserManager $parserManager |
|
109
|
|
|
*/ |
|
110
|
6 |
|
public function setParserManager(ParserManager $parserManager) |
|
111
|
|
|
{ |
|
112
|
6 |
|
$this->parserManager = $parserManager; |
|
113
|
6 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return ParserManager |
|
117
|
|
|
*/ |
|
118
|
6 |
|
protected function getParserManager() |
|
119
|
|
|
{ |
|
120
|
6 |
|
return $this->parserManager; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param SimpleXMLElement $xmlField |
|
125
|
|
|
*/ |
|
126
|
6 |
|
public function setXmlField(SimpleXMLElement $xmlField) |
|
127
|
|
|
{ |
|
128
|
6 |
|
$this->xmlField = $xmlField; |
|
129
|
6 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return SimpleXMLElement |
|
133
|
|
|
*/ |
|
134
|
6 |
|
protected function getXmlField() |
|
135
|
|
|
{ |
|
136
|
6 |
|
return $this->xmlField; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param ImageManager $imageManager |
|
141
|
|
|
*/ |
|
142
|
6 |
|
public function setImageManager(ImageManager $imageManager) |
|
143
|
|
|
{ |
|
144
|
6 |
|
$this->imageManager = $imageManager; |
|
145
|
6 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return ImageManager |
|
149
|
|
|
*/ |
|
150
|
6 |
|
protected function getImageManager() |
|
151
|
|
|
{ |
|
152
|
6 |
|
return $this->imageManager; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param float $scale |
|
157
|
|
|
*/ |
|
158
|
6 |
|
public function setScale($scale) |
|
159
|
|
|
{ |
|
160
|
6 |
|
$this->scale = $scale; |
|
|
|
|
|
|
161
|
6 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return float |
|
165
|
|
|
*/ |
|
166
|
6 |
|
protected function getScale() |
|
167
|
|
|
{ |
|
168
|
6 |
|
return $this->scale; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $tracerColour |
|
173
|
|
|
*/ |
|
174
|
6 |
|
public function setTracerColour($tracerColour) |
|
175
|
|
|
{ |
|
176
|
6 |
|
$this->tracerColour = $tracerColour; |
|
177
|
6 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
6 |
|
protected function getTracerColour() |
|
183
|
|
|
{ |
|
184
|
6 |
|
return $this->tracerColour; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param SimpleXMLElement $xmlHeader |
|
189
|
|
|
*/ |
|
190
|
1 |
|
public function setXmlHeader(SimpleXMLElement $xmlHeader) |
|
191
|
|
|
{ |
|
192
|
1 |
|
$this->xmlHeader = $xmlHeader; |
|
193
|
1 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return SimpleXMLElement |
|
197
|
|
|
*/ |
|
198
|
1 |
|
protected function getXmlHeader() |
|
199
|
|
|
{ |
|
200
|
1 |
|
return $this->xmlHeader; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param callable $fontResolver |
|
205
|
|
|
*/ |
|
206
|
4 |
|
public function setFontResolver(callable $fontResolver) |
|
207
|
|
|
{ |
|
208
|
4 |
|
$this->fontResolver = $fontResolver; |
|
209
|
4 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return callable |
|
213
|
|
|
*/ |
|
214
|
4 |
|
protected function getFontResolver() |
|
215
|
|
|
{ |
|
216
|
4 |
|
return $this->fontResolver; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param callable $graphicResolver |
|
221
|
|
|
*/ |
|
222
|
1 |
|
public function setGraphicResolver(callable $graphicResolver) |
|
223
|
|
|
{ |
|
224
|
1 |
|
$this->graphicResolver = $graphicResolver; |
|
225
|
1 |
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return callable |
|
229
|
|
|
*/ |
|
230
|
1 |
|
protected function getGraphicResolver() |
|
231
|
|
|
{ |
|
232
|
1 |
|
return $this->graphicResolver; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.