CondensedXmlFormatter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A toSimpleXML() 0 24 6
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\RestResource;
4
5
use SimpleXMLElement;
6
use function is_array;
7
use function is_numeric;
8
use function str_replace;
9
10
final class CondensedXmlFormatter extends XmlFormatter
11
{
12
    protected function toSimpleXML(
13
        array $input,
14
        SimpleXMLElement $parent,
15
        bool $alreadySingularized = false
16
    ): void {
17
        foreach ($input as $key => $value) {
18
19
            if (is_numeric($key)) {
20
                $name = $alreadySingularized ?
21
                    'item' : $this->singularizer->convert($parent->getName());
22
                $singularized = true;
23
            } else {
24
                $name = str_replace(['<', '>'], '', (string) $key);
25
                $singularized = false;
26
            }
27
28
            if (is_array($value)) {
29
                $node = $parent->addChild($name);
30
                $this->toSimpleXML($value, $node, $singularized);
31
            } elseif ($singularized) {
32
                $child = $parent->addChild($name);
33
                $child->addAttribute('value', (string) $value);
34
            } else {
35
                $parent->addAttribute($name, (string) $value);
36
            }
37
38
        }
39
    }
40
}
41