Passed
Push — master ( 689ec5...45f08c )
by Sebastian
05:26
created

StylesRenderer::resolveValueSpace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\StyleCollection;
6
7
use AppUtils\StyleCollection;
8
9
class StylesRenderer
10
{
11
    /**
12
     * @var StyleCollection
13
     */
14
    private $collection;
15
16
    /**
17
     * @var StyleOptions
18
     */
19
    private $options;
20
21
    public function __construct(StyleCollection $collection)
22
    {
23
        $this->collection = $collection;
24
        $this->options = $collection->getOptions();
25
    }
26
27
    public function render() : string
28
    {
29
        if($this->collection->hasStyles())
30
        {
31
            return implode(
32
                $this->resolveSeparator(),
33
                $this->renderLines()
34
            ).$this->resolveSuffix();
35
        }
36
37
        return '';
38
    }
39
40
    private function resolveSuffix() : string
41
    {
42
        if($this->options->isTrailingSemicolonEnabled())
43
        {
44
            return ';';
45
        }
46
47
        return '';
48
    }
49
50
    private function resolveSeparator() : string
51
    {
52
        if($this->options->isNewlineEnabled())
53
        {
54
            return ';'.PHP_EOL;
55
        }
56
57
        return ';';
58
    }
59
60
    private function resolveIndent() : string
61
    {
62
        $indentLevel = $this->options->getIndentLevel();
63
64
        if($indentLevel > 0)
65
        {
66
            return str_repeat($this->options->getIndentChar(), $indentLevel);
67
        }
68
69
        return '';
70
    }
71
72
    private function resolveValueSpace() : string
73
    {
74
        if($this->options->isSpaceBeforeValueEnabled())
75
        {
76
            return ' ';
77
        }
78
79
        return '';
80
    }
81
82
    private function renderLines() : array
83
    {
84
        $lines = array();
85
        $styles = $this->collection->getStyles();
86
        $indent = $this->resolveIndent();
87
        $valueSpace = $this->resolveValueSpace();
88
89
        if($this->options->isSortingEnabled())
90
        {
91
            ksort($styles);
92
        }
93
94
        foreach($styles as $name => $value)
95
        {
96
            $lines[] = sprintf(
97
                '%s%s:%s%s',
98
                $indent,
99
                $name,
100
                $valueSpace,
101
                $value
102
            );
103
        }
104
105
        return $lines;
106
    }
107
}
108