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