Completed
Push — master ( ed7b9f...3a85cf )
by Pol
01:36
created

Tag::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * @param string $name
39
     *   The tag name.
40
     * @param mixed $content
41
     *   The content.
42
     */
43 9
    public function __construct(AttributesInterface $attributes, $name, $content = false)
44
    {
45 9
        $this->tag = $name;
46 9
        $this->attributes = $attributes;
47 9
        $this->content($content);
48 9
    }
49
50
    /**
51
     * @param string $name
52
     * @param array $arguments
53
     *
54
     * @return \drupol\htmltag\Tag\TagInterface
55
     */
56 1
    public static function __callStatic($name, array $arguments = [])
57
    {
58 1
        return new static($arguments[0], $name);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function __toString()
65
    {
66 1
        return $this->render();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function attr($name = null, ...$value)
73
    {
74 3
        if (null === $name) {
75 1
            return $this->attributes->render();
76
        }
77
78 3
        if ([] === $value) {
79 2
            return $this->attributes[$name];
80
        }
81
82 1
        return $this->attributes[$name]->set($value);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 9
    public function content(...$content)
89
    {
90 9
        if ([] !== $content) {
91 9
            if (\reset($content) === false) {
92 8
                $content = null;
93
            }
94
95 9
            $this->content = $content;
96
        }
97
98 9
        return $this->renderContent();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 7
    public function render()
105
    {
106 7
        $output = sprintf('<%s%s', $this->tag, $this->attributes);
107
108 7
        $output .= null === $this->content ?
109 5
            '/>':
110 7
            sprintf('>%s</%s>', $this->renderContent(), $this->tag);
111
112 7
        return $output;
113
    }
114
115
    /**
116
     * Render the tag content.
117
     *
118
     * @return string
119
     */
120 9
    private function renderContent()
121
    {
122 9
        return \implode(
123 9
            '',
124 9
            $this->preprocess(
125 9
                \array_filter(
126 9
                    \array_map(
127
                        function ($content_item) {
128 9
                            $output = '';
129
130
                            // Make sure we can 'stringify' the item.
131 9
                            if (!\is_array($content_item) &&
132 9
                                ((!\is_object($content_item) && \settype($content_item, 'string') !== false) ||
133 9
                                    (\is_object($content_item) && \method_exists($content_item, '__toString')))) {
134 9
                                $output = (string) $content_item;
135
                            }
136
137 9
                            return $output;
138 9
                        },
139 9
                        $this->ensureFlatArray($this->ensureArray($this->content))
140
                    ),
141 9
                    '\strlen'
142
                )
143
            )
144
        );
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 9
    protected function preprocess(array $values, $name = null)
151
    {
152 9
        return array_map(
153
            function ($value) {
154 5
                return htmlspecialchars(
155 5
                    $value,
156 5
                    ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5,
157 5
                    'UTF-8'
158
                );
159 9
            },
160 9
            $values
161
        );
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 1
    public function serialize()
168
    {
169 1
        return serialize([
170 1
            'tag' => $this->tag,
171 1
            'attributes' => $this->attributes->getValuesAsArray(),
172 1
            'content' => $this->renderContent(),
173
        ]);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 1
    public function unserialize($serialized)
180
    {
181 1
        $unserialize = unserialize($serialized);
182
183 1
        $this->tag = $unserialize['tag'];
184 1
        $this->attributes = $this->attributes->import($unserialize['attributes']);
185 1
        $this->content = $unserialize['content'];
186 1
    }
187
}
188