1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2018 Peter Gee <https://github.com/pgee70>. |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @package i3Soft\CDA |
30
|
|
|
* @author Peter Gee <https://github.com/pgee70> |
31
|
|
|
* @link https://github.com/pgee70/cda |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
namespace i3Soft\CDA\Elements\Html; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
use i3Soft\CDA\Elements\AbstractElement; |
40
|
|
|
|
41
|
|
|
abstract class AbstractHtmlElement extends AbstractElement |
42
|
|
|
{ |
43
|
|
|
/** @var string */ |
44
|
|
|
protected $content; |
45
|
|
|
|
46
|
|
|
/** @var array */ |
47
|
|
|
protected $tags; |
48
|
|
|
|
49
|
|
|
protected $tag_attributes; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* AbstractHtmlElement constructor. |
53
|
|
|
* |
54
|
|
|
* @param string $choice |
55
|
|
|
*/ |
56
|
|
|
public function __construct($choice = '') |
57
|
|
|
{ |
58
|
|
|
$this->tags = array(); |
59
|
|
|
$this->tag_attributes = array(); |
60
|
|
|
if ($choice) { |
61
|
|
|
if (\is_string($choice)) { |
|
|
|
|
62
|
|
|
$this->setContent($choice); |
63
|
|
|
} else { |
64
|
|
|
$this->addTag($choice); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $choice |
71
|
|
|
* |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
|
|
public function addTag($choice): self |
75
|
|
|
{ |
76
|
|
|
if ($this->canAddTag($choice)) { |
77
|
|
|
$this->tags[] = $choice; |
78
|
|
|
} |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks to see if the choice can be added to the tags array of this class. |
84
|
|
|
* |
85
|
|
|
* @param $choice |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
abstract protected function canAddTag($choice): bool; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function toDOMElement(\DOMDocument $doc): \DOMElement |
95
|
|
|
{ |
96
|
|
|
$el = $this->createElement($doc, $this->tag_attributes); |
97
|
|
|
if ($this->hasContent()) { |
98
|
|
|
$el->appendChild($doc->createTextNode($this->getContent())); |
99
|
|
|
} else { |
100
|
|
|
foreach ($this->getTags() as $tag) { |
101
|
|
|
$el->appendChild($tag->toDOMElement($doc)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return $el; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function hasContent(): bool |
111
|
|
|
{ |
112
|
|
|
return empty($this->content) === false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getContent(): string |
119
|
|
|
{ |
120
|
|
|
return $this->content; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $content |
125
|
|
|
* |
126
|
|
|
* @return self |
127
|
|
|
*/ |
128
|
|
|
public function setContent(string $content): self |
129
|
|
|
{ |
130
|
|
|
$this->content = $content; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function getTags(): array |
138
|
|
|
{ |
139
|
|
|
return $this->tags; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $tags |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
|
|
public function setTags(array $tags): self |
148
|
|
|
{ |
149
|
|
|
$this->tags = $tags; |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return Br |
155
|
|
|
*/ |
156
|
|
|
public function returnBr(): Br |
157
|
|
|
{ |
158
|
|
|
if ($this instanceof Br) { |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
throw new \RuntimeException('The element is not an instance of Class Br'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return Caption |
166
|
|
|
*/ |
167
|
|
|
public function returnCaption(): Caption |
168
|
|
|
{ |
169
|
|
|
if ($this instanceof Caption) { |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
throw new \RuntimeException('The element is not an instance of Class Caption'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return FootNote |
177
|
|
|
*/ |
178
|
|
|
public function returnFootNote(): FootNote |
179
|
|
|
{ |
180
|
|
|
if ($this instanceof FootNote) { |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
throw new \RuntimeException('The element is not an instance of Class FootNote'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return FootNoteRef |
188
|
|
|
*/ |
189
|
|
|
public function returnFootNoteRef(): FootNoteRef |
190
|
|
|
{ |
191
|
|
|
if ($this instanceof FootNoteRef) { |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
throw new \RuntimeException('The element is not an instance of Class FootNoteRef'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return Item |
199
|
|
|
*/ |
200
|
|
|
public function returnItem(): Item |
201
|
|
|
{ |
202
|
|
|
if ($this instanceof Item) { |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
throw new \RuntimeException('The element is not an instance of Class Item'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return LinkHtml |
210
|
|
|
*/ |
211
|
|
|
public function returnLinkHtml(): LinkHtml |
212
|
|
|
{ |
213
|
|
|
if ($this instanceof LinkHtml) { |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
throw new \RuntimeException('The element is not an instance of Class LinkHtml'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return ListElement |
221
|
|
|
*/ |
222
|
|
|
public function returnListElement(): ListElement |
223
|
|
|
{ |
224
|
|
|
if ($this instanceof ListElement) { |
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
throw new \RuntimeException('The element is not an instance of Class ListElement'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return Paragraph |
232
|
|
|
*/ |
233
|
|
|
public function returnParagraph(): Paragraph |
234
|
|
|
{ |
235
|
|
|
if ($this instanceof Paragraph) { |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
throw new \RuntimeException('The element is not an instance of Class Paragraph'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return ReferenceElement |
243
|
|
|
*/ |
244
|
|
|
public function returnReferenceElement(): ReferenceElement |
245
|
|
|
{ |
246
|
|
|
if ($this instanceof ReferenceElement) { |
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
throw new \RuntimeException('The element is not an instance of Class ReferenceElement'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return RenderMultiMedia |
254
|
|
|
*/ |
255
|
|
|
public function returnRenderMultiMedia(): RenderMultiMedia |
256
|
|
|
{ |
257
|
|
|
if ($this instanceof RenderMultiMedia) { |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
throw new \RuntimeException('The element is not an instance of Class RenderMultiMedia'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return Sub |
265
|
|
|
*/ |
266
|
|
|
public function returnSub(): Sub |
267
|
|
|
{ |
268
|
|
|
if ($this instanceof Sub) { |
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
throw new \RuntimeException('The element is not an instance of Class Sub'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return Sup |
276
|
|
|
*/ |
277
|
|
|
public function returnSup(): Sup |
278
|
|
|
{ |
279
|
|
|
if ($this instanceof Sup) { |
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
throw new \RuntimeException('The element is not an instance of Class Sup'); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return Table |
287
|
|
|
*/ |
288
|
|
|
public function returnTable(): Table |
289
|
|
|
{ |
290
|
|
|
if ($this instanceof Table) { |
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
throw new \RuntimeException('The element is not an instance of Class Table'); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return TableBody |
298
|
|
|
*/ |
299
|
|
|
public function returnTableBody(): TableBody |
300
|
|
|
{ |
301
|
|
|
if ($this instanceof TableBody) { |
|
|
|
|
302
|
|
|
return $this; |
303
|
|
|
} |
304
|
|
|
throw new \RuntimeException('The element is not an instance of Class TableBody'); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return TableCell |
309
|
|
|
*/ |
310
|
|
|
public function returnTableCell(): TableCell |
311
|
|
|
{ |
312
|
|
|
if ($this instanceof TableCell) { |
|
|
|
|
313
|
|
|
return $this; |
314
|
|
|
} |
315
|
|
|
throw new \RuntimeException('The element is not an instance of Class TableCell'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return TableHead |
320
|
|
|
*/ |
321
|
|
|
public function returnTableHead(): TableHead |
322
|
|
|
{ |
323
|
|
|
if ($this instanceof TableHead) { |
|
|
|
|
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
throw new \RuntimeException('The element is not an instance of Class TableHead'); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return TableRow |
331
|
|
|
*/ |
332
|
|
|
public function returnTableRow(): TableRow |
333
|
|
|
{ |
334
|
|
|
if ($this instanceof TableRow) { |
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
throw new \RuntimeException('The element is not an instance of Class TableRow'); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @return Text |
342
|
|
|
*/ |
343
|
|
|
public function returnText(): Text |
344
|
|
|
{ |
345
|
|
|
if ($this instanceof Text) { |
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
throw new \RuntimeException('The element is not an instance of Class Text'); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return Title |
353
|
|
|
*/ |
354
|
|
|
public function returnTitle(): Title |
355
|
|
|
{ |
356
|
|
|
if ($this instanceof Title) { |
357
|
|
|
return $this; |
358
|
|
|
} |
359
|
|
|
throw new \RuntimeException('The element is not an instance of Class Title'); |
360
|
|
|
} |
361
|
|
|
} |