Debian Codegraph @

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

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 use ipl\Html\Attribute;
6 use ipl\Html\Attributes;
7 use ipl\Html\BaseHtmlElement;
8 use ipl\Html\HtmlElement;
9 use ipl\Html\Text;
10 use ipl\Html\ValidHtml;
11
12 /**
13 * Ball element that supports different sizes
14 *
15 * The LESS mixins `ball-solid($color)` and `ball-outline($color)` must be used to style the ball,
16 * otherwise only the ball content is visible but not the ball itself.
17 *
18 * @phpstan-type SIZE self::SIZE_TINY|self::SIZE_SMALL|self::SIZE_MEDIUM|_SIZE2
19 * @phpstan-type _SIZE2 self::SIZE_MEDIUM_LARGE|self::SIZE_BIG|self::SIZE_LARGE
20 */
21 class Ball extends BaseHtmlElement
22 {
23 /** Tiny, no content supported */
24 public const SIZE_TINY = 'xs';
25
26 /** Small, no content supported */
27 public const SIZE_SMALL = 's';
28
29 /** Medium, Icon as content only */
30 public const SIZE_MEDIUM = 'm';
31
32 /** Medium large, Icon as content only */
33 public const SIZE_MEDIUM_LARGE = 'ml';
34
35 /** Big, Icon and text as content */
36 public const SIZE_BIG = 'l';
37
38 /** Large, Icon and text as content */
39 public const SIZE_LARGE = 'xl';
40
41 protected $tag = 'span';
42
43 protected $defaultAttributes = ['class' => ['ball']];
44
45 /** @var SIZE */
46 protected $size = self::SIZE_LARGE;
47
48 /**
49 * Create a new ball element
50 *
51 * @param SIZE $size
52 */
53 public function __construct(string $size = self::SIZE_LARGE)
54 {
55 $size = trim($size);
56 if (empty($size)) {
57 $size = self::SIZE_MEDIUM;
58 }
59
60 $this->size = $size;
61 }
62
63 /**
64 * Register the attribute callbacks
65 *
66 * @param Attributes $attributes
67 */
68 protected function registerAttributeCallbacks(Attributes $attributes): void
69 {
70 $attributes->registerAttributeCallback('class', function () {
71 return new Attribute('class', $this->assembleCssClasses());
72 });
73 }
74
75 /**
76 * Assemble the CSS classes for the ball
77 *
78 * @return array
79 */
80 protected function assembleCssClasses(): array
81 {
82 return ['ball-size-' . $this->size];
83 }
84
85 /**
86 * Add content to the ball
87 *
88 * Automatically wraps text content in a span element.
89 *
90 * @param ValidHtml ...$content
91 *
92 * @return $this
93 */
94 public function addHtml(ValidHtml ...$content): static
95 {
96 if (count($content) === 1 && $content[0] instanceof Text) {
97 $content[0] = new HtmlElement('span', null, $content[0]);
98 }
99
100 return parent::addHtml(...$content);
101 }
102 }