AbstractTag::escape()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\htmltag\Tag;
6
7
use drupol\htmltag\AbstractBaseHtmlTagObject;
8
use drupol\htmltag\Attributes\AttributesInterface;
9
use drupol\htmltag\StringableInterface;
10
11
abstract class AbstractTag extends AbstractBaseHtmlTagObject implements TagInterface
12
{
13
    /**
14
     * The tag attributes.
15
     *
16
     * @var \drupol\htmltag\Attributes\AttributesInterface
17
     */
18
    private $attributes;
19
20
    /**
21
     * The tag content.
22
     *
23
     * @var mixed[]|null
24
     */
25
    private $content;
26
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 14
    public function __construct(AttributesInterface $attributes, ?string $name = null, $content = null)
45
    {
46 14
        $this->tag = $name;
47 14
        $this->attributes = $attributes;
48 14
        $this->content($content);
49 14
    }
50
51
    /**
52
     * @param array<string> $arguments
53
     *
54
     * @return \drupol\htmltag\Tag\TagInterface
55
     */
56
    public static function __callStatic(string $name, array $arguments = [])
57 1
    {
58
        return new static($arguments[0], $name);
59 1
    }
60
61
    public function __toString(): string
62
    {
63
        return $this->render();
64
    }
65 2
66
    public function alter(callable ...$closures): TagInterface
67 2
    {
68
        foreach ($closures as $closure) {
69
            $this->content = $closure(
70
                $this->ensureFlatArray((array) $this->content)
71
            );
72
        }
73 1
74
        return $this;
75 1
    }
76 1
77 1
    public function attr(?string $name = null, ...$value)
78
    {
79
        if (null === $name) {
80
            return $this->attributes->render();
81 1
        }
82
83
        if ([] === $value) {
84
            return $this->attributes[$name];
85
        }
86
87 3
        return $this->attributes[$name]->set($value);
88
    }
89 3
90 1
    public function content(...$data): ?string
91
    {
92
        if ([] !== $data) {
93 3
            if (null === reset($data)) {
94 2
                $data = null;
95
            }
96
97 1
            $this->content = $data;
98
        }
99
100
        return $this->renderContent();
101
    }
102
103 14
    public function escape($value): ?string
104
    {
105 14
        $return = $this->ensureString($value);
106 14
107 11
        if ($value instanceof StringableInterface) {
108
            return $return;
109
        }
110 14
111
        return null === $return ?
112
            $return :
113 14
            htmlentities($return);
114
    }
115
116
    /**
117
     * @return array<int, string>
118
     */
119 10
    public function getContentAsArray(): array
120
    {
121 10
        return $this->preprocess(
122
            $this->ensureFlatArray((array) $this->content)
123 10
        );
124 3
    }
125
126
    public function preprocess(array $values, array $context = []): array
127 10
    {
128 1
        return $values;
129 10
    }
130
131
    public function render(): string
132
    {
133
        return null === ($content = $this->renderContent()) ?
134
            sprintf('<%s%s/>', $this->tag, $this->attributes->render()) :
135 14
            sprintf('<%s%s>%s</%s>', $this->tag, $this->attributes->render(), $content, $this->tag);
136
    }
137 14
138 14
    public function serialize()
139
    {
140
        return serialize([
141
            'tag' => $this->tag,
142
            'attributes' => $this->attributes->getValuesAsArray(),
143
            'content' => $this->renderContent(),
144
        ]);
145 14
    }
146
147 14
    public function unserialize($serialized)
148
    {
149
        $unserialize = unserialize($serialized);
150
151
        $this->tag = $unserialize['tag'];
152
        $this->attributes = $this->attributes->import($unserialize['attributes']);
153 10
        $this->content = $unserialize['content'];
154
    }
155 10
156 6
    /**
157 10
     * Render the tag content.
158
     */
159
    protected function renderContent(): ?string
160
    {
161
        return [] === ($items = array_map([$this, 'escape'], $this->getContentAsArray())) ?
162
            null :
163 1
            implode('', $items);
164
    }
165
}
166