Completed
Push — master ( f2437f...d48f0d )
by Pol
12:54
created

Tag::sanitize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace drupol\htmltag\Tag;
4
5
use drupol\htmltag\AbstractBaseHtmlTagObject;
6
use drupol\htmltag\Attributes\AttributesInterface;
7
8
/**
9
 * Class Tag.
10
 */
11
class Tag extends AbstractBaseHtmlTagObject implements TagInterface
12
{
13
    /**
14
     * The tag name.
15
     *
16
     * @var string
17
     */
18
    private $tag;
19
20
    /**
21
     * The tag attributes.
22
     *
23
     * @var \drupol\htmltag\Attributes\AttributesInterface
24
     */
25
    private $attributes;
26
27
    /**
28
     * The tag content.
29
     *
30
     * @var mixed[]|null
31
     */
32
    private $content;
33
34
    /**
35
     * Tag constructor.
36
     *
37
     * @param \drupol\htmltag\Attributes\AttributesInterface $attributes
38
     *   The attributes object.
39
     * @param string $name
40
     *   The tag name.
41
     * @param mixed $content
42
     *   The content.
43
     */
44 10
    public function __construct(AttributesInterface $attributes, $name, $content = false)
45
    {
46 10
        $this->tag = $name;
47 10
        $this->attributes = $attributes;
48 10
        $this->content($content);
49 10
    }
50
51
    /**
52
     * @param string $name
53
     * @param array $arguments
54
     *
55
     * @return \drupol\htmltag\Tag\TagInterface
56
     */
57 1
    public static function __callStatic($name, array $arguments = [])
58
    {
59 1
        return new static($arguments[0], $name);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function __toString()
66
    {
67 1
        return $this->render();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 3
    public function attr($name = null, ...$value)
74
    {
75 3
        if (null === $name) {
76 1
            return $this->attributes->render();
77
        }
78
79 3
        if ([] === $value) {
80 2
            return $this->attributes[$name];
81
        }
82
83 1
        return $this->attributes[$name]->set($value);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 10
    public function content(...$content)
90
    {
91 10
        if ([] !== $content) {
92 10
            if (\reset($content) === false) {
93 9
                $content = null;
94
            }
95
96 10
            $this->content = $content;
97
        }
98
99 10
        return $this->renderContent();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 8
    public function render()
106
    {
107 8
        $output = sprintf('<%s%s', $this->tag, $this->attributes);
108
109 8
        $output .= null === $this->content ?
110 5
            '/>':
111 8
            sprintf('>%s</%s>', $this->renderContent(), $this->tag);
112
113 8
        return $output;
114
    }
115
116
    /**
117
     * Render the tag content.
118
     *
119
     * @return string
120
     */
121 10
    private function renderContent()
122
    {
123 10
        return \implode(
124 10
            '',
125 10
            $this->preprocess(
126 10
                \array_map(
127
                    function ($content_item) {
128 6
                        if (is_object($content_item)) {
129 6
                            if (!$content_item instanceof TagInterface &&
130 6
                                \method_exists($content_item, '__toString')) {
131 6
                                $content_item = (string) $content_item;
132
                            }
133 10
                        } else {
134 10
                            $content_item = $this->sanitize($content_item);
135 10
                        }
136
137 10
                        return $content_item;
138
                    },
139
                    $this->ensureFlatArray($this->ensureArray($this->content))
140 10
                )
141 10
            )
142 10
        );
143 10
    }
144
145
    /**
146 10
     * Sanitize a string.
147 10
     *
148 10
     * @param string $string
149
     *   The string to sanitize.
150 10
     *
151
     * @return string
152
     *   The sanitized string.
153
     */
154
    protected function sanitize($string) {
155
        return \htmlspecialchars(
156
            $string,
157
            ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5,
158
            'UTF-8'
159
        );
160 1
    }
161
162 1
    /**
163 1
     * {@inheritdoc}
164 1
     */
165 1
    public function serialize()
166
    {
167
        return \serialize([
168
            'tag' => $this->tag,
169
            'attributes' => $this->attributes->getValuesAsArray(),
170
            'content' => $this->renderContent(),
171
        ]);
172 1
    }
173
174 1
    /**
175
     * {@inheritdoc}
176 1
     */
177 1
    public function unserialize($serialized)
178 1
    {
179 1
        $unserialize = \unserialize($serialized);
180
181
        $this->tag = $unserialize['tag'];
182
        $this->attributes = $this->attributes->import($unserialize['attributes']);
183
        $this->content = $unserialize['content'];
184
    }
185
186
    /**
187
     * Alter the values of an object.
188
     *
189
     * @param callable ...$closures
190
     *   A closure.
191
     *
192
     * @return object
193 1
     *   The object.
194
     */
195 1
    /**
196 1
     * {@inheritdoc}
197 1
     */
198
    public function alter(callable ...$closures)
199
    {
200
        foreach ($closures as $closure) {
201 1
            $this->content = $closure(
202
                $this->ensureFlatArray($this->ensureArray($this->content))
203
            );
204
        }
205
206
        return $this;
207
    }
208
}
209