Debian Codegraph @

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

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 /**
6 * State ball element that supports different sizes and colors
7 *
8 * @phpstan-import-type SIZE from Ball
9 * @phpstan-type STATE 'none'|'pending'|'up'|'down'|'ok'|'critical'|'warning'|'unknown'|'unreachable'
10 */
11 class StateBall extends Ball
12 {
13 protected $defaultAttributes = ['class' => 'state-ball'];
14
15 /** @var STATE */
16 protected $state = 'none';
17
18 /** @var bool */
19 protected $handled = false;
20
21 /**
22 * Create a new state ball element
23 *
24 * @param STATE $state
25 * @param SIZE $size
26 */
27 public function __construct(string $state = 'none', string $size = self::SIZE_SMALL)
28 {
29 $state = trim($state);
30 if (empty($state)) {
31 $state = 'none';
32 }
33
34 $this->state = $state;
35
36 parent::__construct($size);
37 }
38
39 /**
40 * Show the handled state instead
41 *
42 * @param bool $handled
43 *
44 * @return $this
45 */
46 public function setHandled(bool $handled): self
47 {
48 $this->handled = $handled;
49
50 return $this;
51 }
52
53 protected function assembleCssClasses(): array
54 {
55 $classes = parent::assembleCssClasses();
56 $classes[] = 'state-' . $this->state;
57 if ($this->handled) {
58 $classes[] = 'handled';
59 }
60
61 return $classes;
62 }
63 }