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

ElementRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 16
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B renderElement() 0 23 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