Passed
Push — master ( e4cc97...45d363 )
by Mr.
02:05
created

SvgInlineBootstrap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YiiRocks\SvgInline\Bootstrap;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Html\Html;
10
11
/**
12
 * SvgInlineBootstrap provides a quick and easy way to access Bootstrap Icons.
13
 */
14
final class SvgInlineBootstrap extends \YiiRocks\SvgInline\SvgInline implements SvgInlineBootstrapInterface
15
{
16
    /** @var string CSS class basename */
17
    protected string $prefix;
18
19
    /** @var string Path to the Bootstrap Icons folder */
20
    private string $bootstrapIconsFolder;
21
22
    /** @var bool `true` for fixed-width class */
23
    private bool $fixedWidth;
24
25
    /** @var BootstrapIcon icon properties */
26
    private BootstrapIcon $icon;
27
28
    /**
29
     * Sets the name of the icon.
30
     *
31
     * @param string $name  name of the icon
32
     * @return BootstrapIcon component object
33
     */
34
    public function name(string $name): BootstrapIcon
35
    {
36
        $this->icon = new BootstrapIcon();
37
        $iconFile = implode(DIRECTORY_SEPARATOR, [$this->bootstrapIconsFolder, "{$name}.svg"]);
38
        $this->icon->setName($iconFile);
39
40
        return $this->icon;
41
    }
42
43
    /**
44
     * @see $bootstrapIconsFolder
45
     * @param string $value
46
     * @return void
47
     */
48
    public function setBootstrapIconsFolder(string $value): void
49
    {
50
        $this->bootstrapIconsFolder = $this->aliases->get($value);
51
    }
52
53
    /**
54
     * @see $fixedWidth
55
     * @param bool $value
56
     * @return void
57
     */
58
    public function setFixedWidth(bool $value): void
59
    {
60
        $this->fixedWidth = $value;
61
    }
62
63
    /**
64
     * @see $prefix
65
     * @param string $value
66
     * @return void
67
     */
68
    public function setPrefix(string $value): void
69
    {
70
        $this->prefix = $value;
71
    }
72
73
    /**
74
     * Prepares either the size class (default) or the width/height if either of these is given manually.
75
     *
76
     * @return void
77
     */
78
    protected function setSvgSize(): void
79
    {
80
        parent::setSvgSize();
81
82
        $width = $this->icon->get('width');
83
        $height = $this->icon->get('height');
84
85
        if (!$width && !$height) {
86
            Html::addCssClass($this->class, $this->prefix);
87
            $widthClass = $this->icon->get('fixedWidth')
88
                ? "{$this->prefix}-fw"
89
                : "{$this->prefix}-w-" . ceil($this->svgWidth / $this->svgHeight * 16);
90
            Html::addCssClass($this->class, $widthClass);
91
        }
92
    }
93
}
94