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