Completed
Push — master ( 226547...612b7b )
by Nathan
02:27
created

Element.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NatePage\EasyHtmlElement;
4
5
use HtmlGenerator\HtmlTag;
6
use HtmlGenerator\Markup;
7
8
class Element implements ElementInterface
9
{
10
    /** @var string  */
11
    protected $type;
12
13
    /** @var string */
14
    protected $text;
15
16
    /** @var array */
17
    protected $attributes;
18
19
    /** @var array */
20
    protected $children;
21
22
    /** @var ElementInterface */
23
    protected $parent;
24
25
    public function __construct($type = null, $text = null, array $attributes = array(), array $children = array())
26
    {
27
        $this->setType($type);
28
        $this->setText($text);
29
        $this->setChildren($children);
30
31
        $this->attributes = $attributes;
32
    }
33
34
    public function __toString()
35
    {
36
        return (string) $this->renderRoot();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function setType($type = null)
43
    {
44
        if(null !== $type){
45
            $this->type = $type;
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getType()
55
    {
56
        return $this->type;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function setText($text = null)
63
    {
64
        if(null != $text){
0 ignored issues
show
It seems like you are loosely comparing $text of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
65
            $this->text = $text;
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getText()
75
    {
76
        return $this->text;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function addAttribute($key, $value)
83
    {
84
        $this->attributes[$key] = $value;
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function addAttributes(array $attributes)
93
    {
94
        foreach($attributes as $key => $value){
95
            $this->addAttribute($key, $value);
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function setAttributes(array $attributes = array())
105
    {
106
        $this->attributes = $attributes;
107
108
        return $this;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getAttributes()
115
    {
116
        return $this->attributes;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function addChild(ElementInterface $child)
123
    {
124
        $this->children[] = $child;
125
126
        $child->setParent($this);
127
128
        return $this;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function addChildren(array $children)
135
    {
136
        foreach($children as $child){
137
            $this->addChild($child);
138
        }
139
140
        return $this;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function setChildren(array $children = array())
147
    {
148
        $this->children = array();
149
150
        $this->addChildren($children);
151
152
        return $this;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getChildren()
159
    {
160
        return $this->children;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function setParent(ElementInterface $parent = null)
167
    {
168
        $this->parent = $parent;
169
170
        return $this;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function getParent()
177
    {
178
        return $this->parent;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function renderRoot()
185
    {
186
        return null !== $this->parent ? $this->parent->renderRoot() : $this->render();
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function render(Markup $root = null)
193
    {
194
        $type = $this->type;
195
        $attributes = $this->attributes;
196
197
        $element = null === $root ? HtmlTag::createElement($type) : $root->addElement($type);
198
        $element->text($this->text);
199
200
        $this->renderAttributes($element, $attributes);
201
        $this->renderChildren($element);
202
203
        return $element;
204
    }
205
206
    /**
207
     * Render element children.
208
     *
209
     * @param Markup $root
210
     */
211
    private function renderChildren(Markup $root)
212
    {
213
        foreach($this->children as $child){
214
            $child->render($root);
215
        }
216
    }
217
218
    /**
219
     * Set Markup element attributes.
220
     *
221
     * @param Markup $element
222
     * @param array $attributes
223
     */
224
    private function renderAttributes(Markup $element, array $attributes)
225
    {
226
        foreach($attributes as $attr => $value){
227
            if(is_array($value)){
228
                $glue = 'style' == $attr ? '; ' : ' ';
229
                $value = implode($glue, $value);
230
            }
231
232
            if(null !== $value){
233
                $element->set($attr, $value);
234
            }
235
        }
236
    }
237
}
238