1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PrinsFrank\PhpGeoSVG\Html\Rendering; |
6
|
|
|
|
7
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\InvalidArgumentException; |
8
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\RecursionException; |
9
|
|
|
use PrinsFrank\PhpGeoSVG\Html\Elements\Element; |
10
|
|
|
use PrinsFrank\PhpGeoSVG\Html\Elements\Text\TextContent; |
11
|
|
|
|
12
|
|
|
class ElementRenderer |
13
|
|
|
{ |
14
|
|
|
private const RECURSION_LIMIT = 10; |
15
|
|
|
private const INDENTING_CHAR = ' '; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @throws RecursionException |
19
|
|
|
*/ |
20
|
9 |
|
public static function renderElement(Element $element, int $currentDepth = 0): string |
21
|
|
|
{ |
22
|
9 |
|
if ($currentDepth++ > self::RECURSION_LIMIT) { |
23
|
2 |
|
throw new RecursionException('Recursion limit of ' . self::RECURSION_LIMIT . ' Reached'); |
24
|
|
|
} |
25
|
|
|
|
26
|
9 |
|
$childElementContent = self::renderChildElements($element->getChildElements(), $currentDepth); |
27
|
7 |
|
$textContent = self::renderTextContent($element->getTextContent(), $currentDepth); |
28
|
7 |
|
if (null === $childElementContent && null === $textContent && $element->canSelfClose()) { |
29
|
3 |
|
return self::renderSelfClosingElement($element); |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
$attributeString = AttributeRenderer::renderAttributes($element->getAttributes()); |
33
|
5 |
|
return '<' . $element->getTagName() . (null !== $attributeString ? ' ' . $attributeString : null) . '>' . PHP_EOL . |
34
|
|
|
$childElementContent . $textContent . |
35
|
5 |
|
str_repeat(self::INDENTING_CHAR, $currentDepth - 1) . '</' . $element->getTagName() . '>' . PHP_EOL; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
*/ |
41
|
2 |
|
public static function renderSelfClosingElement(Element $element): string |
42
|
|
|
{ |
43
|
2 |
|
if ($element->canSelfClose() === false) { |
44
|
1 |
|
throw new InvalidArgumentException('Only elements that can self close can be rendered as self closing elements'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$attributeString = AttributeRenderer::renderAttributes($element->getAttributes()); |
48
|
1 |
|
return '<' . $element->getTagName() . (null !== $attributeString ? ' ' . $attributeString : null) . '/>' . PHP_EOL; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Element[] $childElements |
53
|
|
|
* @throws RecursionException |
54
|
|
|
*/ |
55
|
2 |
|
public static function renderChildElements(array $childElements, int $currentDepth): ?string |
56
|
|
|
{ |
57
|
2 |
|
$childElementString = null; |
58
|
2 |
|
foreach ($childElements as $childElement) { |
59
|
1 |
|
$childElementString .= str_repeat(self::INDENTING_CHAR, $currentDepth) . self::renderElement($childElement, $currentDepth); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return $childElementString; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public static function renderTextContent(?TextContent $textContent, int $currentDepth): ?string |
66
|
|
|
{ |
67
|
2 |
|
if (null !== $textContent) { |
68
|
1 |
|
return str_repeat(self::INDENTING_CHAR, $currentDepth) . $textContent->getContent() . PHP_EOL; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return null; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|