Debian Codegraph @

vendor/ipl/web/src/Widget/Icon.php

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 use ipl\Html\Attributes;
6 use ipl\Html\BaseHtmlElement;
7 use ipl\Stdlib\Str;
8
9 /**
10 * Icon element
11 */
12 class Icon extends BaseHtmlElement
13 {
14 protected $tag = 'i';
15
16 /** @var string Icon style */
17 protected $style;
18
19 /** @var string Icon default style */
20 protected $defaultStyle = 'fa';
21
22 /**
23 * Create an icon element
24 *
25 * Creates an icon element from the given name and HTML attributes. The icon element's tag will be <i>. The given
26 * name will be used as automatically added CSS class for the icon element in the format 'icon-$name'. In addition,
27 * the CSS class 'icon' will be automatically added too.
28 *
29 * @param string $name The name of the icon
30 * @param Attributes|array $attributes The HTML attributes for the element
31 */
32 public function __construct(string $name, $attributes = null)
33 {
34 if (! Str::startsWith($name, 'fa-')) {
35 $name = "fa-$name";
36 }
37
38 $this
39 ->getAttributes()
40 ->add('class', ['icon', $name])
41 ->add($attributes);
42 }
43
44 /**
45 * Get the icon style
46 *
47 * @return string
48 */
49 public function getStyle(): string
50 {
51 return $this->style ?? $this->defaultStyle;
52 }
53
54 /**
55 * Set the icon style
56 *
57 * @param string $style Style class with prefix
58 *
59 * @return $this
60 */
61 public function setStyle(string $style): self
62 {
63 $this->style = $style;
64
65 return $this;
66 }
67
68 protected function assemble()
69 {
70 $this->addAttributes(['class' => $this->getStyle()]);
71 }
72 }