Completed
Push — master ( 630c64...f14c7d )
by Pol
18:54 queued 02:19
created

Tag::content()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
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\AttributesFactoryInterface;
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
     * The attributes factory.
36
     *
37
     * @var \drupol\htmltag\Attributes\AttributesFactoryInterface
38
     */
39
    private $attributesFactory;
40
41
    /**
42
     * Tag constructor.
43
     *
44
     * @param \drupol\htmltag\Attributes\AttributesFactoryInterface $attributesFactory
45
     *   The attributes factory.
46
     * @param string $name
47
     *   The tag name.
48
     */
49 6
    public function __construct(AttributesFactoryInterface $attributesFactory, $name)
50
    {
51 6
        $this->tag = $name;
52 6
        $this->attributesFactory = $attributesFactory;
53 6
        $this->attributes = $attributesFactory::build();
54 6
    }
55
56
    /**
57
     * @param string $name
58
     * @param array $arguments
59
     *
60
     * @return \drupol\htmltag\Tag\TagInterface
61
     */
62 1
    public static function __callStatic($name, array $arguments = [])
63
    {
64 1
        return new static($arguments[0], $name);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function __toString()
71
    {
72 1
        return $this->render();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function attr($name = null, ...$value)
79
    {
80 2
        if (null === $name) {
81 1
            return $this->attributes->render();
82
        }
83
84 2
        return $this->attributes[$name]->set($value);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function content(...$content)
91
    {
92 2
        if ([] !== $content) {
93 2
            if (reset($content) === false) {
94 1
                $content = null;
95
            }
96
97 2
            $this->content = $content;
98
        }
99
100 2
        return $this->renderContent();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 5
    public function render()
107
    {
108 5
        $output = sprintf('<%s%s', $this->tag, $this->attributes);
109
110 5
        $output .= null === $this->content ?
111 5
            '/>':
112 5
            sprintf('>%s</%s>', $this->renderContent(), $this->tag);
113
114 5
        return $output;
115
    }
116
117
    /**
118
     * Render the tag content.
119
     *
120
     * @return string
121
     */
122 2
    private function renderContent()
123
    {
124 2
        return implode(
125 2
            '',
126 2
            array_filter(
127 2
                array_map(
128
                    function ($content_item) {
129 2
                        $output = '';
130
131
                        // Make sure we can 'stringify' the item.
132 2
                        if (!is_array($content_item) &&
133 2
                            ((!is_object($content_item) && settype($content_item, 'string') !== false) ||
134 2
                                (is_object($content_item) && method_exists($content_item, '__toString')))) {
135 2
                            $output = (string) $content_item;
136
                        }
137
138 2
                        return $output;
139 2
                    },
140 2
                    $this->ensureFlatArray($this->ensureArray($this->content))
141
                ),
142 2
                'strlen'
143
            )
144
        );
145
    }
146
}
147