Issues (113)

lib/Elements/Html/AbstractHtmlElement.php (6 issues)

Severity
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
    {
62
      if (\is_string($choice))
0 ignored issues
show
The condition is_string($choice) is always true.
Loading history...
63
      {
64
        $this->setContent($choice);
65
      }
66
      else
67
      {
68
        $this->addTag($choice);
69
      }
70
    }
71
  }
72
73
  /**
74
   * @param $choice
75
   *
76
   * @return self
77
   */
78
  public function addTag ($choice): self
79
  {
80
    if ($this->canAddTag($choice))
81
    {
82
      $this->tags[] = $choice;
83
    }
84
    return $this;
85
  }
86
87
  /**
88
   * Checks to see if the choice can be added to the tags array of this class.
89
   *
90
   * @param $choice
91
   *
92
   * @return bool
93
   */
94
  abstract protected function canAddTag ($choice): bool;
95
96
  /**
97
   * @inheritDoc
98
   */
99
  public function toDOMElement (\DOMDocument $doc): \DOMElement
100
  {
101
    $el = $this->createElement($doc, $this->tag_attributes);
102
    if ($this->hasContent())
103
    {
104
      $el->appendChild($doc->createTextNode($this->getContent()));
105
    }
106
    else
107
    {
108
      foreach ($this->getTags() as $tag)
109
      {
110
        $el->appendChild($tag->toDOMElement($doc));
111
      }
112
    }
113
    return $el;
114
  }
115
116
  /**
117
   * @return bool
118
   */
119
  public function hasContent (): bool
120
  {
121
    return empty($this->content) === FALSE;
122
  }
123
124
  /**
125
   * @return string
126
   */
127
  public function getContent (): string
128
  {
129
    return $this->content;
130
  }
131
132
  /**
133
   * @param string $content
134
   *
135
   * @return self
136
   */
137
  public function setContent (string $content): self
138
  {
139
    $this->content = $content;
140
    return $this;
141
  }
142
143
  /**
144
   * @return array
145
   */
146
  public function getTags (): array
147
  {
148
    return $this->tags;
149
  }
150
151
  /**
152
   * @param array $tags
153
   *
154
   * @return self
155
   */
156
  public function setTags (array $tags): self
157
  {
158
    $this->tags = $tags;
159
    return $this;
160
  }
161
162
  /**
163
   * @return Br
164
   */
165
  public function returnBr (): Br
166
  {
167
    if ($this instanceof Br)
168
    {
169
      return $this;
170
    }
171
    throw new \RuntimeException('The element is not an instance of Class Br');
172
  }
173
174
  /**
175
   * @return Caption
176
   */
177
  public function returnCaption (): Caption
178
  {
179
    if ($this instanceof Caption)
180
    {
181
      return $this;
182
    }
183
    throw new \RuntimeException('The element is not an instance of Class Caption');
184
  }
185
186
  /**
187
   * @return FootNote
188
   */
189
  public function returnFootNote (): FootNote
190
  {
191
    if ($this instanceof FootNote)
192
    {
193
      return $this;
194
    }
195
    throw new \RuntimeException('The element is not an instance of Class FootNote');
196
  }
197
198
  /**
199
   * @return FootNoteRef
200
   */
201
  public function returnFootNoteRef (): FootNoteRef
202
  {
203
    if ($this instanceof FootNoteRef)
204
    {
205
      return $this;
206
    }
207
    throw new \RuntimeException('The element is not an instance of Class FootNoteRef');
208
  }
209
210
  /**
211
   * @return Item
212
   */
213
  public function returnItem (): Item
214
  {
215
    if ($this instanceof Item)
216
    {
217
      return $this;
218
    }
219
    throw new \RuntimeException('The element is not an instance of Class Item');
220
  }
221
222
  /**
223
   * @return LinkHtml
224
   */
225
  public function returnLinkHtml (): LinkHtml
226
  {
227
    if ($this instanceof LinkHtml)
228
    {
229
      return $this;
230
    }
231
    throw new \RuntimeException('The element is not an instance of Class LinkHtml');
232
  }
233
234
  /**
235
   * @return ListElement
236
   */
237
  public function returnListElement (): ListElement
238
  {
239
    if ($this instanceof ListElement)
240
    {
241
      return $this;
242
    }
243
    throw new \RuntimeException('The element is not an instance of Class ListElement');
244
  }
245
246
  /**
247
   * @return Paragraph
248
   */
249
  public function returnParagraph (): Paragraph
250
  {
251
    if ($this instanceof Paragraph)
252
    {
253
      return $this;
254
    }
255
    throw new \RuntimeException('The element is not an instance of Class Paragraph');
256
  }
257
258
  /**
259
   * @return ReferenceElement
260
   */
261
  public function returnReferenceElement (): ReferenceElement
262
  {
263
    if ($this instanceof ReferenceElement)
264
    {
265
      return $this;
266
    }
267
    throw new \RuntimeException('The element is not an instance of Class ReferenceElement');
268
  }
269
270
  /**
271
   * @return RenderMultiMedia
272
   */
273
  public function returnRenderMultiMedia (): RenderMultiMedia
274
  {
275
    if ($this instanceof RenderMultiMedia)
276
    {
277
      return $this;
278
    }
279
    throw new \RuntimeException('The element is not an instance of Class RenderMultiMedia');
280
  }
281
282
  /**
283
   * @return Sub
284
   */
285
  public function returnSub (): Sub
286
  {
287
    if ($this instanceof Sub)
288
    {
289
      return $this;
290
    }
291
    throw new \RuntimeException('The element is not an instance of Class Sub');
292
  }
293
294
  /**
295
   * @return Sup
296
   */
297
  public function returnSup (): Sup
298
  {
299
    if ($this instanceof Sup)
300
    {
301
      return $this;
302
    }
303
    throw new \RuntimeException('The element is not an instance of Class Sup');
304
  }
305
306
  /**
307
   * @return Table
308
   */
309
  public function returnTable (): Table
310
  {
311
    if ($this instanceof Table)
0 ignored issues
show
$this is never a sub-type of i3Soft\CDA\Elements\Html\Table.
Loading history...
312
    {
313
      return $this;
314
    }
315
    throw new \RuntimeException('The element is not an instance of Class Table');
316
  }
317
318
  /**
319
   * @return TableBody
320
   */
321
  public function returnTableBody (): TableBody
322
  {
323
    if ($this instanceof TableBody)
0 ignored issues
show
$this is never a sub-type of i3Soft\CDA\Elements\Html\TableBody.
Loading history...
324
    {
325
      return $this;
326
    }
327
    throw new \RuntimeException('The element is not an instance of Class TableBody');
328
  }
329
330
  /**
331
   * @return TableCell
332
   */
333
  public function returnTableCell (): TableCell
334
  {
335
    if ($this instanceof TableCell)
0 ignored issues
show
$this is never a sub-type of i3Soft\CDA\Elements\Html\TableCell.
Loading history...
336
    {
337
      return $this;
338
    }
339
    throw new \RuntimeException('The element is not an instance of Class TableCell');
340
  }
341
342
  /**
343
   * @return TableHead
344
   */
345
  public function returnTableHead (): TableHead
346
  {
347
    if ($this instanceof TableHead)
0 ignored issues
show
$this is never a sub-type of i3Soft\CDA\Elements\Html\TableHead.
Loading history...
348
    {
349
      return $this;
350
    }
351
    throw new \RuntimeException('The element is not an instance of Class TableHead');
352
  }
353
354
  /**
355
   * @return TableRow
356
   */
357
  public function returnTableRow (): TableRow
358
  {
359
    if ($this instanceof TableRow)
0 ignored issues
show
$this is never a sub-type of i3Soft\CDA\Elements\Html\TableRow.
Loading history...
360
    {
361
      return $this;
362
    }
363
    throw new \RuntimeException('The element is not an instance of Class TableRow');
364
  }
365
366
  /**
367
   * @return Text
368
   */
369
  public function returnText (): Text
370
  {
371
    if ($this instanceof Text)
372
    {
373
      return $this;
374
    }
375
    throw new \RuntimeException('The element is not an instance of Class Text');
376
  }
377
378
  /**
379
   * @return Title
380
   */
381
  public function returnTitle (): Title
382
  {
383
    if ($this instanceof Title)
384
    {
385
      return $this;
386
    }
387
    throw new \RuntimeException('The element is not an instance of Class Title');
388
  }
389
}