Style::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace WebTheory\Html\Attributes;
4
5
use WebTheory\Html\Contracts\HtmlAttributeInterface;
6
7
class Style extends AbstractHtmlAttribute implements HtmlAttributeInterface
8
{
9
    protected string $name = 'style';
10
11
    protected array $value = [];
12
13
    /**
14
     * @return $this
15
     */
16
    public function set($style, $value): Style
17
    {
18
        $this->value[$style] = $value;
19
20
        return $this;
21
    }
22
23
    public function get($style)
24
    {
25
        return $this->value[$style];
26
    }
27
28
    /**
29
     * @return $this
30
     */
31
    public function remove($style): Style
32
    {
33
        unset($this->value[$style]);
34
35
        return $this;
36
    }
37
38
    public function contains($style): bool
39
    {
40
        return isset($this->value[$style]);
41
    }
42
43
    public function parse(): string
44
    {
45
        $str = '';
46
47
        foreach ($this->value as $style => $value) {
48
            $str .= "{$style}: {$value}; ";
49
        }
50
51
        return rtrim($str, ' ');
52
    }
53
54
    public function tokenize(string $styles): array
55
    {
56
        $styles = explode(';', $styles);
57
        $value = [];
58
59
        foreach ($styles as $style) {
60
            $style = explode(':', $style);
61
62
            $value[ltrim($style[0])] = ltrim($style[1]);
63
        }
64
65
        return $value;
66
    }
67
}
68