Passed
Push — main ( 42b4d9...c70814 )
by Frank
01:56
created

ElementRenderer::renderElement()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 17
nop 2
crap 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Html\Rendering;
6
7
use PrinsFrank\PhpGeoSVG\Exception\RecursionException;
8
use PrinsFrank\PhpGeoSVG\Html\Elements\Element;
9
10
class ElementRenderer
11
{
12
    private const RECURSION_LIMIT = 10;
13
    private const INDENTING_CHAR  = ' ';
14
15
    /**
16
     * @throws RecursionException
17
     */
18 9
    public static function renderElement(Element $element, int $currentDepth = 0): string
19
    {
20 9
        if ($currentDepth++ > self::RECURSION_LIMIT) {
21 2
            throw new RecursionException('Recursion limit of ' . self::RECURSION_LIMIT . ' Reached');
22
        }
23
24 9
        $elementContent = null;
25 9
        foreach ($element->getChildElements() as $childElement) {
26 4
            $elementContent .= str_repeat(self::INDENTING_CHAR, $currentDepth) . self::renderElement($childElement, $currentDepth);
27
        }
28
29 7
        if (null !== $element->getTextContent()) {
30 1
            $elementContent .= str_repeat(self::INDENTING_CHAR, $currentDepth) . $element->getTextContent()->getContent() . PHP_EOL;
31
        }
32
33 7
        $attributeString = AttributeRenderer::renderAttributes($element->getAttributes());
34 7
        if (null === $elementContent && $element->canSelfClose()) {
35 3
            return '<' . $element->getTagName() . (null !== $attributeString ? ' ' . $attributeString : null) . '/>' . PHP_EOL;
36
        }
37
38 5
        return '<' . $element->getTagName() . (null !== $attributeString ? ' ' . $attributeString : null) . '>' . PHP_EOL .
39 5
            $elementContent .
40 5
        str_repeat(self::INDENTING_CHAR, $currentDepth - 1) . '</' . $element->getTagName() . '>' . PHP_EOL;
41
    }
42
}
43