Completed
Push — master ( 56b2a2...4da2f1 )
by Pol
02:28
created

AbstractTag::escape()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace drupol\htmltag\Tag;
4
5
use drupol\htmltag\AbstractBaseHtmlTagObject;
6
use drupol\htmltag\Attributes\AttributesInterface;
7
use drupol\htmltag\StringableInterface;
8
9
/**
10
 * Class AbstractTag.
11
 */
12
abstract class AbstractTag extends AbstractBaseHtmlTagObject implements TagInterface
13
{
14
    /**
15
     * The tag attributes.
16
     *
17
     * @var \drupol\htmltag\Attributes\AttributesInterface
18
     */
19
    private $attributes;
20
21
    /**
22
     * The tag content.
23
     *
24
     * @var null|mixed[]
25
     */
26
    private $content;
27
    /**
28
     * The tag name.
29
     *
30
     * @var string
31
     */
32
    private $tag;
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 15
    public function __construct(AttributesInterface $attributes, $name, $content = null)
45
    {
46 15
        $this->tag = $name;
47 15
        $this->attributes = $attributes;
48 15
        $this->content($content);
49 15
    }
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 2
    public function __toString()
66
    {
67 2
        return $this->render();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function alter(callable ...$closures)
74
    {
75 1
        foreach ($closures as $closure) {
76 1
            $this->content = $closure(
77 1
                $this->ensureFlatArray((array) $this->content)
78
            );
79
        }
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 3
    public function attr($name = null, ...$value)
88
    {
89 3
        if (null === $name) {
90 1
            return $this->attributes->render();
91
        }
92
93 3
        if ([] === $value) {
94 2
            return $this->attributes[$name];
95
        }
96
97 1
        return $this->attributes[$name]->set($value);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 15
    public function content(...$data)
104
    {
105 15
        if ([] !== $data) {
106 15
            if (null === \reset($data)) {
107 12
                $data = null;
108
            }
109
110 15
            $this->content = $data;
111
        }
112
113 15
        return $this->renderContent();
114
    }
115
116
    /**
117
     * @return array|\drupol\htmltag\Attribute\AttributeInterface[]
118
     */
119 15
    public function getContentAsArray()
120
    {
121 15
        return $this->preprocess(
122 15
            $this->ensureFlatArray((array) $this->content)
123
        );
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 11
    public function render()
130
    {
131 11
        return null === ($content = $this->renderContent()) ?
132 6
            \sprintf('<%s%s/>', $this->tag, $this->attributes->render()) :
133 11
            \sprintf('<%s%s>%s</%s>', $this->tag, $this->attributes->render(), $content, $this->tag);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function serialize()
140
    {
141 1
        return \serialize([
142 1
            'tag' => $this->tag,
143 1
            'attributes' => $this->attributes->getValuesAsArray(),
144 1
            'content' => $this->renderContent(),
145
        ]);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 1
    public function unserialize($serialized)
152
    {
153 1
        $unserialize = \unserialize($serialized);
154
155 1
        $this->tag = $unserialize['tag'];
156 1
        $this->attributes = $this->attributes->import($unserialize['attributes']);
157 1
        $this->content = $unserialize['content'];
158 1
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 11
    protected function escape($value)
164
    {
165 11
        $return = $this->ensureString($value);
166
167 11
        if ($value instanceof StringableInterface) {
168 4
            return $return;
169
        }
170
171 11
        return null === $return ?
172 2
            $return :
173 11
            \htmlentities($return);
174
    }
175
176
    /**
177
     * Render the tag content.
178
     *
179
     * @return null|string
180
     */
181 15
    protected function renderContent()
182
    {
183 15
        return ($items = \array_map([$this, 'escape'], $this->getContentAsArray())) === [] ?
184 12
            null :
185 15
            \implode('', $items);
186
    }
187
}
188