|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Html\Widget; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use Yiisoft\Html\Html; |
| 9 | +use Yiisoft\Html\NoEncodeStringableInterface; |
| 10 | +use Yiisoft\Html\Tag\Button; |
| 11 | + |
| 12 | +use function is_array; |
| 13 | +use function is_string; |
| 14 | + |
| 15 | +/** |
| 16 | + * `ButtonGroup` represents a group of buttons. |
| 17 | + */ |
| 18 | +final class ButtonGroup implements NoEncodeStringableInterface |
| 19 | +{ |
| 20 | + private ?string $containerTag = 'div'; |
| 21 | + private array $containerAttributes = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Button[] |
| 25 | + */ |
| 26 | + private array $buttons = []; |
| 27 | + private array $buttonAttributes = []; |
| 28 | + private string $separator = "\n"; |
| 29 | + |
| 30 | + public static function create(): self |
| 31 | + { |
| 32 | + return new self(); |
| 33 | + } |
| 34 | + |
| 35 | + public function withoutContainer(): self |
| 36 | + { |
| 37 | + return $this->containerTag(null); |
| 38 | + } |
| 39 | + |
| 40 | + public function containerTag(?string $name): self |
| 41 | + { |
| 42 | + $new = clone $this; |
| 43 | + $new->containerTag = $name; |
| 44 | + return $new; |
| 45 | + } |
| 46 | + |
| 47 | + public function containerAttributes(array $attributes): self |
| 48 | + { |
| 49 | + $new = clone $this; |
| 50 | + $new->containerAttributes = $attributes; |
| 51 | + return $new; |
| 52 | + } |
| 53 | + |
| 54 | + public function buttons(Button ...$buttons): self |
| 55 | + { |
| 56 | + $new = clone $this; |
| 57 | + $new->buttons = $buttons; |
| 58 | + return $new; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param array $data Array of buttons. Each button is an array with label as first element and additional |
| 63 | + * name-value pairs as attrbiutes of button. |
| 64 | + * |
| 65 | + * Example: |
| 66 | + * ```php |
| 67 | + * [ |
| 68 | + * ['Reset', 'type' => 'reset', 'class' => 'default'], |
| 69 | + * ['Send', 'type' => 'submit', 'class' => 'primary'], |
| 70 | + * ] |
| 71 | + * ``` |
| 72 | + * @param bool $encode Whether button content should be HTML-encoded. |
| 73 | + */ |
| 74 | + public function buttonsData(array $data, bool $encode = true): self |
| 75 | + { |
| 76 | + $buttons = []; |
| 77 | + foreach ($data as $row) { |
| 78 | + if (!is_array($row) || !isset($row[0]) || !is_string($row[0])) { |
| 79 | + throw new InvalidArgumentException( |
| 80 | + 'Invalid buttons data. A data row must be array with label as first element ' . |
| 81 | + 'and additional name-value pairs as attrbiutes of button.' |
| 82 | + ); |
| 83 | + } |
| 84 | + $label = $row[0]; |
| 85 | + unset($row[0]); |
| 86 | + $buttons[] = Html::button($label, $row)->encode($encode); |
| 87 | + } |
| 88 | + return $this->buttons(...$buttons); |
| 89 | + } |
| 90 | + |
| 91 | + public function buttonAttributes(array $attributes): self |
| 92 | + { |
| 93 | + $new = clone $this; |
| 94 | + $new->buttonAttributes = array_merge($new->buttonAttributes, $attributes); |
| 95 | + return $new; |
| 96 | + } |
| 97 | + |
| 98 | + public function replaceButtonAttributes(array $attributes): self |
| 99 | + { |
| 100 | + $new = clone $this; |
| 101 | + $new->buttonAttributes = $attributes; |
| 102 | + return $new; |
| 103 | + } |
| 104 | + |
| 105 | + public function disabled(?bool $disabled = true): self |
| 106 | + { |
| 107 | + $new = clone $this; |
| 108 | + $new->buttonAttributes['disabled'] = $disabled; |
| 109 | + return $new; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Specifies the form element the buttons belongs to. The value of this attribute must be the ID attribute of a form |
| 114 | + * element in the same document. |
| 115 | + * |
| 116 | + * @param string|null $id ID of a form. |
| 117 | + * |
| 118 | + * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
| 119 | + */ |
| 120 | + public function form(?string $id): self |
| 121 | + { |
| 122 | + $new = clone $this; |
| 123 | + $new->buttonAttributes['form'] = $id; |
| 124 | + return $new; |
| 125 | + } |
| 126 | + |
| 127 | + public function separator(string $separator): self |
| 128 | + { |
| 129 | + $new = clone $this; |
| 130 | + $new->separator = $separator; |
| 131 | + return $new; |
| 132 | + } |
| 133 | + |
| 134 | + public function render(): string |
| 135 | + { |
| 136 | + if (empty($this->buttons)) { |
| 137 | + return ''; |
| 138 | + } |
| 139 | + |
| 140 | + if (empty($this->buttonAttributes)) { |
| 141 | + $lines = $this->buttons; |
| 142 | + } else { |
| 143 | + $lines = []; |
| 144 | + foreach ($this->buttons as $button) { |
| 145 | + $lines[] = $button->unionAttributes($this->buttonAttributes); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + $html = []; |
| 150 | + if (!empty($this->containerTag)) { |
| 151 | + $html[] = Html::openTag($this->containerTag, $this->containerAttributes); |
| 152 | + } |
| 153 | + $html[] = implode($this->separator, $lines); |
| 154 | + if (!empty($this->containerTag)) { |
| 155 | + $html[] = Html::closeTag($this->containerTag); |
| 156 | + } |
| 157 | + |
| 158 | + return implode("\n", $html); |
| 159 | + } |
| 160 | + |
| 161 | + public function __toString(): string |
| 162 | + { |
| 163 | + return $this->render(); |
| 164 | + } |
| 165 | + |
| 166 | + private function __construct() |
| 167 | + { |
| 168 | + } |
| 169 | +} |
0 commit comments