Completed
Push — master ( ec5659...28f2f4 )
by Pol
01:27
created

Tag   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
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 158
ccs 52
cts 52
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
B renderContent() 0 24 6
A attr() 0 12 3
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 8
    public function __construct(AttributesInterface $attributes, $name, $content = false)
44
    {
45 8
        $this->tag = $name;
46 8
        $this->attributes = $attributes;
47 8
        $this->content($content);
48 8
    }
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 8
    public function content(...$content)
89
    {
90 8
        if ([] !== $content) {
91 8
            if (\reset($content) === false) {
92 8
                $content = null;
93
            }
94
95 8
            $this->content = $content;
96
        }
97
98 8
        return $this->renderContent();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 6
    public function render()
105
    {
106 6
        $output = sprintf('<%s%s', $this->tag, $this->attributes);
107
108 6
        $output .= null === $this->content ?
109 5
            '/>':
110 6
            sprintf('>%s</%s>', $this->renderContent(), $this->tag);
111
112 6
        return $output;
113
    }
114
115
    /**
116
     * Render the tag content.
117
     *
118
     * @return string
119
     */
120 8
    private function renderContent()
121
    {
122 8
        return \implode(
123 8
            '',
124 8
            \array_filter(
125 8
                \array_map(
126
                    function ($content_item) {
127 8
                        $output = '';
128
129
                        // Make sure we can 'stringify' the item.
130 8
                        if (!\is_array($content_item) &&
131 8
                            ((!\is_object($content_item) && \settype($content_item, 'string') !== false) ||
132 8
                                (\is_object($content_item) && \method_exists($content_item, '__toString')))) {
133 8
                            $output = (string) $content_item;
134
                        }
135
136 8
                        return $output;
137 8
                    },
138 8
                    $this->ensureFlatArray($this->ensureArray($this->content))
139
                ),
140 8
                '\strlen'
141
            )
142
        );
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 1
    public function serialize()
149
    {
150 1
        return serialize([
151 1
            'tag' => $this->tag,
152 1
            'attributes' => $this->attributes->getValuesAsArray(),
153 1
            'content' => $this->renderContent(),
154
        ]);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1
    public function unserialize($serialized)
161
    {
162 1
        $unserialize = unserialize($serialized);
163
164 1
        $this->tag = $unserialize['tag'];
165 1
        $this->attributes = $this->attributes->import($unserialize['attributes']);
166 1
        $this->content = $unserialize['content'];
167 1
    }
168
}
169