| Total Complexity | 22 |
| Total Lines | 322 |
| Duplicated Lines | 0 % |
| Coverage | 79.01% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 28 | final class ButtonDropdown extends Widget |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * The css class part of dropdown |
||
| 32 | */ |
||
| 33 | public const DIRECTION_DOWN = 'down'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The css class part of dropleft |
||
| 37 | */ |
||
| 38 | public const DIRECTION_LEFT = 'left'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The css class part of dropright |
||
| 42 | */ |
||
| 43 | public const DIRECTION_RIGHT = 'right'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The css class part of dropup |
||
| 47 | */ |
||
| 48 | public const DIRECTION_UP = 'up'; |
||
| 49 | |||
| 50 | private string $label = 'Button'; |
||
| 51 | private array $options = []; |
||
| 52 | private array $buttonOptions = []; |
||
| 53 | private array $dropdown = []; |
||
| 54 | private string $direction = self::DIRECTION_DOWN; |
||
| 55 | private bool $split = false; |
||
| 56 | private string $tagName = 'button'; |
||
| 57 | private bool $encodeLabels = true; |
||
| 58 | private bool $encodeTags = false; |
||
| 59 | private string $dropdownClass = Dropdown::class; |
||
| 60 | private bool $renderContainer = true; |
||
| 61 | 3 | ||
| 62 | public function run(): string |
||
| 94 | 3 | } |
|
| 95 | |||
| 96 | 3 | /** |
|
| 97 | * The HTML attributes of the button. |
||
| 98 | 3 | * |
|
| 99 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 100 | 3 | * |
|
| 101 | 3 | * @param array $value |
|
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | 3 | */ |
|
| 105 | 1 | public function withButtonOptions(array $value): self |
|
| 106 | { |
||
| 107 | 1 | $new = clone $this; |
|
| 108 | 1 | $new->buttonOptions = $value; |
|
| 109 | 1 | ||
| 110 | return $new; |
||
| 111 | 1 | } |
|
| 112 | |||
| 113 | 1 | /** |
|
| 114 | * The drop-direction of the widget. |
||
| 115 | 1 | * |
|
| 116 | 1 | * Possible values are 'left', 'right', 'up', or 'down' (default) |
|
| 117 | 1 | * |
|
| 118 | 1 | * @param string $value |
|
| 119 | 1 | * |
|
| 120 | * @return $this |
||
| 121 | 2 | */ |
|
| 122 | public function withDirection(string $value): self |
||
| 123 | 2 | { |
|
| 124 | $new = clone $this; |
||
| 125 | 2 | $new->direction = $value; |
|
| 126 | 2 | ||
| 127 | 2 | return $new; |
|
| 128 | 2 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | 3 | * The configuration array for example: |
|
| 132 | * |
||
| 133 | * ```php |
||
| 134 | * [ |
||
| 135 | * 'items' => [ |
||
| 136 | 3 | * ['label' => 'DropdownA', 'url' => '/'], |
|
| 137 | 3 | * ['label' => 'DropdownB', 'url' => '#'], |
|
| 138 | 3 | * ], |
|
| 139 | 3 | * ] |
|
| 140 | 3 | * ``` |
|
| 141 | 3 | * |
|
| 142 | 3 | * {@see Dropdown} |
|
| 143 | * |
||
| 144 | * @param array $value |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function withDropdown(array $value): self |
||
| 149 | { |
||
| 150 | 3 | $new = clone $this; |
|
| 151 | $new->dropdown = $value; |
||
| 152 | 3 | ||
| 153 | return $new; |
||
| 154 | 3 | } |
|
| 155 | 3 | ||
| 156 | 3 | /** |
|
| 157 | * Name of a class to use for rendering dropdowns withing this widget. Defaults to {@see Dropdown}. |
||
| 158 | * |
||
| 159 | * @param string $value |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function withDropdownClass(string $value): self |
||
| 164 | { |
||
| 165 | $new = clone $this; |
||
| 166 | $new->dropdownClass = $value; |
||
| 167 | |||
| 168 | return $new; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Whether the label should be HTML-encoded. |
||
| 173 | * |
||
| 174 | * @param bool $value |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function withoutEncodeLabels(bool $value = false): self |
||
| 179 | { |
||
| 180 | $new = clone $this; |
||
| 181 | $new->encodeLabels = $value; |
||
| 182 | |||
| 183 | return $new; |
||
| 184 | 3 | } |
|
| 185 | |||
| 186 | 3 | /** |
|
| 187 | * The button label. |
||
| 188 | 3 | * |
|
| 189 | * @param string $value |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function withLabel(string $value): self |
||
| 194 | { |
||
| 195 | $new = clone $this; |
||
| 196 | $new->label = $value; |
||
| 197 | |||
| 198 | return $new; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The HTML attributes for the container tag. The following special options are recognized. |
||
| 203 | * |
||
| 204 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 205 | * |
||
| 206 | * @param array $value |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | 3 | */ |
|
| 210 | public function withOptions(array $value): self |
||
| 211 | 3 | { |
|
| 212 | $new = clone $this; |
||
| 213 | 3 | $new->options = $value; |
|
| 214 | |||
| 215 | return $new; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Whether to render the container using the {@see options} as HTML attributes. If set to `false`, the container |
||
| 220 | * element enclosing the button and dropdown will NOT be rendered. |
||
| 221 | * |
||
| 222 | * @param bool $value |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function withoutRenderContainer(bool $value = false): self |
||
| 227 | { |
||
| 228 | $new = clone $this; |
||
| 229 | $new->renderContainer = $value; |
||
| 230 | |||
| 231 | return $new; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Whether to display a group of split-styled button group. |
||
| 236 | * |
||
| 237 | * @param bool $value |
||
| 238 | * |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function withSplit(bool $value = true): self |
||
| 242 | { |
||
| 243 | $new = clone $this; |
||
| 244 | $new->split = $value; |
||
| 245 | |||
| 246 | return $new; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * The tag to use to render the button. |
||
| 251 | 3 | * |
|
| 252 | * @param string $value |
||
| 253 | 3 | * |
|
| 254 | * @return $this |
||
| 255 | 3 | */ |
|
| 256 | public function withTagName(string $value): self |
||
| 257 | { |
||
| 258 | $new = clone $this; |
||
| 259 | $new->tagName = $value; |
||
| 260 | |||
| 261 | return $new; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Allows you to enable or disable the encoding tags html. |
||
| 266 | * |
||
| 267 | 1 | * @param bool $value |
|
| 268 | * |
||
| 269 | 1 | * @return self |
|
| 270 | */ |
||
| 271 | 1 | public function withEncodeTags(bool $value = true): self |
|
| 272 | { |
||
| 273 | $new = clone $this; |
||
| 274 | $new->encodeTags = $value; |
||
| 275 | |||
| 276 | return $new; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Generates the button dropdown. |
||
| 281 | * |
||
| 282 | * @throws InvalidConfigException |
||
| 283 | * |
||
| 284 | * @return string the rendering result. |
||
| 285 | */ |
||
| 286 | private function renderButton(): string |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Generates the dropdown menu. |
||
| 339 | * |
||
| 340 | * @return string the rendering result. |
||
| 341 | */ |
||
| 342 | private function renderDropdown(): string |
||
| 350 | } |
||
| 351 | } |
||
| 352 |