Test Failed
Push — master ( f2bdc0...e2cac0 )
by Chris
29:08
created

HtmlMap::toHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace WebTheory\Html;
4
5
use JsonSerializable;
6
7
/**
8
 * @deprecated version 0.2.0
9
 */
10
class HtmlMap implements JsonSerializable
11
{
12
    /**
13
     *
14
     */
15
    public $map;
16
17
    /**
18
     *
19
     */
20
    public function __construct(array $map)
21
    {
22
        $this->map = $map;
23
    }
24
25
    /**
26
     * Generates an html string from array of element definitions
27
     *
28
     * 'tag' => string
29
     * 'attributes' => array || string
30
     * 'content' => string
31
     * 'children' => array
32
     *
33
     * @param array $html_map
34
     * @param bool $recall
35
     *
36
     * @return string
37
     */
38
    protected function constructHtml($map = null, $recall = false)
39
    {
40
        static $markedUp;
41
42
        $html = '';
43
44
        if (!$recall) {
45
            $markedUp = [];
46
        }
47
48
        foreach ($map ?? $this->map as $currentElement => $definition) {
49
50
            if (in_array($currentElement, $markedUp)) {
51
                continue;
52
            }
53
54
            // add values already existing as strings to $html as they may already exist as markup
55
            if (is_object($definition) && method_exists($definition, '__toString') || is_string($definition)) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (is_object($definition) ... is_string($definition), Probably Intended Meaning: is_object($definition) &...is_string($definition))
Loading history...
56
                $html .= $definition;
57
                $markedUp[] = $currentElement;
58
                continue;
59
            }
60
61
            $html .= Html::open($definition['tag'], $definition['attributes'] ?? '');
0 ignored issues
show
Bug introduced by
It seems like $definition['attributes'] ?? '' can also be of type string; however, parameter $attributes of WebTheory\Html\Html::open() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            $html .= Html::open($definition['tag'], /** @scrutinizer ignore-type */ $definition['attributes'] ?? '');
Loading history...
62
            $html .= $definition['content'] ?? '';
63
64
            // store children in array to be passed as $html_map argument in recursive call
65
            if (!empty($children = $definition['children'] ?? null)) {
66
                foreach ($children as $child) {
67
                    $childMap[$child] = $this->map[$child];
68
                }
69
70
                $html .= $this->constructHtml($childMap, true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $childMap does not seem to be defined for all execution paths leading up to this point.
Loading history...
71
            }
72
73
            $html .= Html::maybeClose($definition['tag']);
74
            $markedUp[] = $currentElement;
75
        }
76
77
        // reset static variables if in initial call stack
78
        if (!$recall) {
79
            $markedUp = null;
80
        }
81
82
        return $html;
83
    }
84
85
    /**
86
     *
87
     */
88
    public function toHtml()
89
    {
90
        return $this->constructHtml();
91
    }
92
93
    /**
94
     *
95
     */
96
    public function toJson()
97
    {
98
        return json_encode($this);
99
    }
100
101
    /**
102
     *
103
     */
104
    public function jsonSerialize()
105
    {
106
        return $this->map;
107
    }
108
109
    /**
110
     *
111
     */
112
    public function __toString()
113
    {
114
        return $this->toHtml();
115
    }
116
}
117