| Total Complexity | 7 |
| Total Lines | 84 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 20 | final class Button extends Widget |
||
| 21 | { |
||
| 22 | private string $tagName = 'button'; |
||
| 23 | private string $label = 'Button'; |
||
| 24 | private bool $encodeLabels = true; |
||
| 25 | private bool $encodeTags = false; |
||
| 26 | private array $options = []; |
||
| 27 | |||
| 28 | 20 | protected function run(): string |
|
| 29 | { |
||
| 30 | 20 | if (!isset($this->options['id'])) { |
|
| 31 | 12 | $this->options['id'] = "{$this->getId()}-button"; |
|
| 32 | } |
||
| 33 | |||
| 34 | /** @psalm-suppress InvalidArgument */ |
||
| 35 | 20 | Html::addCssClass($this->options, ['widget' => 'btn']); |
|
| 36 | |||
| 37 | 20 | return Html::tag( |
|
| 38 | 20 | $this->tagName, |
|
| 39 | 20 | $this->encodeLabels ? Html::encode($this->label) : $this->label, |
|
| 40 | 20 | $this->options |
|
| 41 | ) |
||
| 42 | 20 | ->encode($this->encodeTags) |
|
| 43 | 20 | ->render(); |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * When tags Labels HTML should not be encoded. |
||
| 48 | * |
||
| 49 | * @return self |
||
| 50 | */ |
||
| 51 | 4 | public function withoutEncodeLabels(): self |
|
| 52 | { |
||
| 53 | 4 | $new = clone $this; |
|
| 54 | 4 | $new->encodeLabels = false; |
|
| 55 | |||
| 56 | 4 | return $new; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The button label |
||
| 61 | * |
||
| 62 | * @param string $value |
||
| 63 | * |
||
| 64 | * @return self |
||
| 65 | */ |
||
| 66 | 20 | public function label(string $value): self |
|
| 67 | { |
||
| 68 | 20 | $new = clone $this; |
|
| 69 | 20 | $new->label = $value; |
|
| 70 | |||
| 71 | 20 | return $new; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The HTML attributes for the widget container tag. The following special options are recognized. |
||
| 76 | * |
||
| 77 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 78 | * |
||
| 79 | * @param array $value |
||
| 80 | * |
||
| 81 | * @return self |
||
| 82 | */ |
||
| 83 | 17 | public function options(array $value): self |
|
| 84 | { |
||
| 85 | 17 | $new = clone $this; |
|
| 86 | 17 | $new->options = $value; |
|
| 87 | |||
| 88 | 17 | return $new; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The tag to use to render the button. |
||
| 93 | * |
||
| 94 | * @param string $value |
||
| 95 | * |
||
| 96 | * @return self |
||
| 97 | */ |
||
| 98 | 10 | public function tagName(string $value): self |
|
| 104 | } |
||
| 105 | } |
||
| 106 |