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