Completed
Push — master ( 3a85cf...ea50d3 )
by Pol
01:42
created

Tag   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 169
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __callStatic() 0 4 1
A __toString() 0 4 1
A content() 0 12 3
A render() 0 10 2
A attr() 0 12 3
B renderContent() 0 35 6
A serialize() 0 8 1
A unserialize() 0 8 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_map(
126
                    function ($value) {
127 5
                        return \htmlspecialchars(
128 5
                            $value,
129 5
                            ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5,
130 5
                            'UTF-8'
131
                        );
132 9
                    },
133 9
                    \array_filter(
134 9
                        \array_map(
135
                            function ($content_item) {
136 9
                                $output = '';
137
138
                                // Make sure we can 'stringify' the item.
139 9
                                if (!\is_array($content_item) &&
140 9
                                    ((!\is_object($content_item) && \settype($content_item, 'string') !== false) ||
141 9
                                        (\is_object($content_item) && \method_exists($content_item, '__toString')))) {
142 9
                                    $output = (string) $content_item;
143
                                }
144
145 9
                                return $output;
146 9
                            },
147 9
                            $this->ensureFlatArray($this->ensureArray($this->content))
148
                        ),
149 9
                        '\strlen'
150
                    )
151
                )
152
            )
153
        );
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 1
    public function serialize()
160
    {
161 1
        return \serialize([
162 1
            'tag' => $this->tag,
163 1
            'attributes' => $this->attributes->getValuesAsArray(),
164 1
            'content' => $this->renderContent(),
165
        ]);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 1
    public function unserialize($serialized)
172
    {
173 1
        $unserialize = \unserialize($serialized);
174
175 1
        $this->tag = $unserialize['tag'];
176 1
        $this->attributes = $this->attributes->import($unserialize['attributes']);
177 1
        $this->content = $unserialize['content'];
178 1
    }
179
}
180