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

FontFallback::compileStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\StyleCollection\StyleBuilder\Flavors\Font;
6
7
use AppUtils\StyleCollection;
8
use AppUtils\StyleCollection\StyleBuilder;
9
use AppUtils\StyleCollection\StyleBuilder\StyleContainer;
10
11
class FontFallback extends StyleContainer
12
{
13
    /**
14
     * @var string[]
15
     */
16
    private $fonts;
17
18
    /**
19
     * @param StyleBuilder $styles
20
     * @param StyleCollection $collection
21
     * @param string[] $fonts
22
     */
23
    public function __construct(StyleBuilder $styles, StyleCollection $collection, array $fonts)
24
    {
25
        parent::__construct($styles, $collection);
26
27
        $this->fonts = $fonts;
28
    }
29
30
    protected function getName() : string
31
    {
32
        return 'font-family';
33
    }
34
35
    private function compileStyle(string $fallback, bool $important) : StyleBuilder
36
    {
37
        return $this->setStyle(implode(', ', $this->renderFonts($fallback)), $important);
38
    }
39
40
    private function renderFonts(string $fallback) : array
41
    {
42
        $fonts = $this->fonts;
43
        $fonts[] = $fallback;
44
        $keep = array();
45
46
        foreach($fonts as $font)
47
        {
48
            if(strpos($font, ' ') !== false)
49
            {
50
                $font = "'".$font."'";
51
            }
52
53
            $keep[] = $font;
54
        }
55
56
        return $keep;
57
    }
58
59
    // region: Fallback fonts
60
61
    public const FALLBACK_SERIF = 'serif';
62
    public const FALLBACK_SANS_SERIF = 'sans-serif';
63
    public const FALLBACK_MONOSPACE = 'monospace';
64
    public const FALLBACK_CURSIVE = 'cursive';
65
    public const FALLBACK_FANTASY = 'fantasy';
66
67
    public function serif(bool $important=false) : StyleBuilder
68
    {
69
        return $this->compileStyle(self::FALLBACK_SERIF, $important);
70
    }
71
72
    public function sansSerif(bool $important=false) : StyleBuilder
73
    {
74
        return $this->compileStyle(self::FALLBACK_SANS_SERIF, $important);
75
    }
76
77
    public function cursive(bool $important=false) : StyleBuilder
78
    {
79
        return $this->compileStyle(self::FALLBACK_CURSIVE, $important);
80
    }
81
82
    public function monospace(bool $important=false) : StyleBuilder
83
    {
84
        return $this->compileStyle(self::FALLBACK_MONOSPACE, $important);
85
    }
86
87
    public function fantasy(bool $important=false) : StyleBuilder
88
    {
89
        return $this->compileStyle(self::FALLBACK_FANTASY, $important);
90
    }
91
92
    // endregion
93
}
94