o2system /
html
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Html\Element; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Spl\Patterns\Structural\Composite\RenderableInterface; |
||
| 19 | use O2System\Spl\Patterns\Structural\Repository\AbstractRepository; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class Attributes |
||
| 23 | * |
||
| 24 | * @package O2System\Html\Element |
||
| 25 | */ |
||
| 26 | class Attributes extends AbstractRepository implements RenderableInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Attributes::setAttributeId |
||
| 30 | * |
||
| 31 | * @param string $id |
||
| 32 | * |
||
| 33 | * @return static |
||
| 34 | */ |
||
| 35 | public function setAttributeId($id) |
||
| 36 | { |
||
| 37 | $this->addAttribute('id', $id); |
||
| 38 | |||
| 39 | return $this; |
||
| 40 | } |
||
| 41 | |||
| 42 | // ------------------------------------------------------------------------ |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Attributes::addAttribute |
||
| 46 | * |
||
| 47 | * @param string $name |
||
| 48 | * @param string $value |
||
| 49 | * |
||
| 50 | * @return static |
||
| 51 | */ |
||
| 52 | public function addAttribute($name, $value) |
||
| 53 | { |
||
| 54 | if ($name === 'class') { |
||
| 55 | $this->addAttributeClass($value); |
||
| 56 | } elseif ($name === 'style') { |
||
| 57 | $value = rtrim($value, ';') . ';'; |
||
| 58 | $value = explode(';', $value); |
||
| 59 | $value = array_filter($value); |
||
| 60 | |||
| 61 | foreach ($value as $style) { |
||
| 62 | if (preg_match_all("/(.*)(: )(.*)/", $style, $match)) { |
||
| 63 | $this->addAttributeStyle($match[ 1 ][ 0 ], $match[ 3 ][ 0 ]); |
||
| 64 | } elseif (preg_match_all("/(.*)(:)(.*)/", $style, $match)) { |
||
| 65 | $this->addAttributeStyle($match[ 1 ][ 0 ], $match[ 3 ][ 0 ]); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } elseif (is_string($value)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 69 | $this->storage[ $name ] = trim($value); |
||
| 70 | } else { |
||
| 71 | $this->storage[ $name ] = $value; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | // ------------------------------------------------------------------------ |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Attributes::addAttributeClass |
||
| 81 | * |
||
| 82 | * @param array|string $classes |
||
| 83 | * |
||
| 84 | * @return static |
||
| 85 | */ |
||
| 86 | public function addAttributeClass($classes) |
||
| 87 | { |
||
| 88 | if (is_string($classes)) { |
||
| 89 | $classes = explode(',', $classes); |
||
| 90 | } |
||
| 91 | |||
| 92 | $classes = array_map('trim', $classes); |
||
| 93 | $classes = array_filter($classes); |
||
| 94 | |||
| 95 | if ( ! $this->offsetExists('class')) { |
||
| 96 | $this->storage[ 'class' ] = []; |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->storage[ 'class' ] = array_merge($this->storage[ 'class' ], $classes); |
||
| 100 | $this->storage[ 'class' ] = array_unique($this->storage[ 'class' ]); |
||
| 101 | |||
| 102 | if (in_array('disabled', $this->storage[ 'class' ])) { |
||
| 103 | $this->removeAttributeClass('active'); |
||
| 104 | } |
||
| 105 | |||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | // ------------------------------------------------------------------------ |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Attributes::removeAttributeClass |
||
| 113 | * |
||
| 114 | * @param array|string $classes |
||
| 115 | */ |
||
| 116 | public function removeAttributeClass($classes) |
||
| 117 | { |
||
| 118 | if ($this->offsetExists('class')) { |
||
| 119 | if (is_string($classes)) { |
||
| 120 | $classes = explode(',', $classes); |
||
| 121 | } |
||
| 122 | |||
| 123 | $classes = array_map('trim', $classes); |
||
| 124 | $classes = array_filter($classes); |
||
| 125 | |||
| 126 | foreach ($classes as $class) { |
||
| 127 | if (false !== ($key = array_search($class, $this->storage[ 'class' ]))) { |
||
| 128 | unset($this->storage[ 'class' ][ $key ]); |
||
| 129 | } elseif (strpos($class, '*') !== false) { |
||
| 130 | $class = str_replace('*', '', $class); |
||
| 131 | foreach ($this->storage[ 'class' ] as $key => $value) { |
||
| 132 | if (preg_match("/\b$class\b/i", $value)) { |
||
| 133 | unset($this->storage[ 'class' ][ $key ]); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | if (count($this->storage[ 'class' ]) == 0) { |
||
| 140 | unset($this->storage[ 'class' ]); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | // ------------------------------------------------------------------------ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Attributes::addAttributeStyle |
||
| 149 | * |
||
| 150 | * @param string|array $styles |
||
| 151 | * @param string|null $value |
||
| 152 | * |
||
| 153 | * @return static |
||
| 154 | */ |
||
| 155 | public function addAttributeStyle($styles, $value = null) |
||
| 156 | { |
||
| 157 | if (is_string($styles)) { |
||
| 158 | $styles = [$styles => $value]; |
||
| 159 | } |
||
| 160 | |||
| 161 | if ( ! $this->offsetExists('style')) { |
||
| 162 | $this->storage[ 'style' ] = []; |
||
| 163 | } |
||
| 164 | |||
| 165 | foreach ($styles as $key => $value) { |
||
| 166 | if (empty($value)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | $styles[ trim($key) ] = trim($value); |
||
| 170 | } |
||
| 171 | |||
| 172 | $this->storage[ 'style' ] = array_merge($this->storage[ 'style' ], $styles); |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | // ------------------------------------------------------------------------ |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Attributes::hasAttribute |
||
| 181 | * |
||
| 182 | * @param string $name |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function hasAttribute($name) |
||
| 187 | { |
||
| 188 | if ($name === 'id') { |
||
| 189 | return empty($this->storage[ 'id' ]) ? false : true; |
||
| 190 | } elseif ($name === 'class') { |
||
| 191 | return empty($this->storage[ 'class' ]) ? false : true; |
||
| 192 | } else { |
||
| 193 | return isset($this->storage[ $name ]); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | // ------------------------------------------------------------------------ |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Attributes::getAttribute |
||
| 201 | * |
||
| 202 | * @param string|null $name |
||
| 203 | * |
||
| 204 | * @return array|bool |
||
| 205 | */ |
||
| 206 | public function getAttribute($name = null) |
||
| 207 | { |
||
| 208 | if (empty($name)) { |
||
| 209 | return $this->storage; |
||
| 210 | } elseif (isset($this->storage[ $name ])) { |
||
| 211 | return $this->storage[ $name ]; |
||
| 212 | } |
||
| 213 | |||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | // ------------------------------------------------------------------------ |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Attributes::removeAttribute |
||
| 221 | * |
||
| 222 | * @param string|array $attributes |
||
| 223 | */ |
||
| 224 | public function removeAttribute($attributes) |
||
| 225 | { |
||
| 226 | if (is_string($attributes)) { |
||
| 227 | $attributes = explode(',', $attributes); |
||
| 228 | } |
||
| 229 | |||
| 230 | $attributes = array_map('trim', $attributes); |
||
| 231 | $attributes = array_filter($attributes); |
||
| 232 | |||
| 233 | foreach ($attributes as $attribute) { |
||
| 234 | if (array_key_exists($attribute, $this->storage)) { |
||
| 235 | unset($this->storage[ $attribute ]); |
||
| 236 | } elseif (strpos($attribute, '*') !== false) { |
||
| 237 | $attribute = str_replace('*', '', $attribute); |
||
| 238 | foreach ($this->storage as $key => $value) { |
||
| 239 | if (preg_match("/\b$attribute\b/i", $key)) { |
||
| 240 | unset($this->storage[ $key ]); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | // ------------------------------------------------------------------------ |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Attributes::getAttributeId |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function getAttributeId() |
||
| 255 | { |
||
| 256 | if ($this->hasAttributeId()) { |
||
| 257 | return $this->storage[ 'id' ]; |
||
| 258 | } |
||
| 259 | |||
| 260 | return false; |
||
| 261 | } |
||
| 262 | |||
| 263 | // ------------------------------------------------------------------------ |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Attributes::hasAttributeId |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function hasAttributeId() |
||
| 271 | { |
||
| 272 | return (bool)empty($this->storage[ 'id' ]) ? false : true; |
||
| 273 | } |
||
| 274 | |||
| 275 | // ------------------------------------------------------------------------ |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Attributes::hasAttributeClass |
||
| 279 | * |
||
| 280 | * @param string $className |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function hasAttributeClass($className) |
||
| 285 | { |
||
| 286 | if ( ! $this->offsetExists('class')) { |
||
| 287 | $this->storage[ 'class' ] = []; |
||
| 288 | } |
||
| 289 | |||
| 290 | return in_array($className, $this->storage[ 'class' ]); |
||
| 291 | } |
||
| 292 | |||
| 293 | // ------------------------------------------------------------------------ |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Attributes::getAttributeClass |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getAttributeClass() |
||
| 301 | { |
||
| 302 | if ( ! $this->offsetExists('class')) { |
||
| 303 | $this->storage[ 'class' ] = []; |
||
| 304 | } |
||
| 305 | |||
| 306 | return implode(', ', $this->storage[ 'class' ]); |
||
| 307 | } |
||
| 308 | |||
| 309 | // ------------------------------------------------------------------------ |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Attributes::replaceAttributeClass |
||
| 313 | * |
||
| 314 | * @param string $class |
||
| 315 | * @param string $replace |
||
| 316 | */ |
||
| 317 | public function replaceAttributeClass($class, $replace) |
||
| 318 | { |
||
| 319 | if ($this->offsetExists('class')) { |
||
| 320 | foreach ($this->storage[ 'class' ] as $key => $value) { |
||
| 321 | if (preg_match("/\b$class\b/i", $value)) { |
||
| 322 | $this->storage[ 'class' ][ $key ] = str_replace($class, $replace, $value); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | if (count($this->storage[ 'class' ]) == 0) { |
||
| 327 | unset($this->storage[ 'class' ]); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | // ------------------------------------------------------------------------ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Attributes::findAttributeClass |
||
| 336 | * |
||
| 337 | * @param string $class |
||
| 338 | * |
||
| 339 | * @return array|bool |
||
| 340 | */ |
||
| 341 | public function findAttributeClass($class) |
||
| 342 | { |
||
| 343 | if ($this->offsetExists('class')) { |
||
| 344 | $matches = []; |
||
| 345 | |||
| 346 | foreach ($this->storage[ 'class' ] as $key => $value) { |
||
| 347 | if (preg_match("/\b$class\b/i", $value)) { |
||
| 348 | $matches[] = $value; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | if (count($matches)) { |
||
| 353 | return $matches; |
||
| 354 | } |
||
| 355 | |||
| 356 | } |
||
| 357 | |||
| 358 | return false; |
||
| 359 | } |
||
| 360 | |||
| 361 | // ------------------------------------------------------------------------ |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Attributes::__toString |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function __toString() |
||
| 369 | { |
||
| 370 | return $this->render(); |
||
| 371 | } |
||
| 372 | |||
| 373 | // ------------------------------------------------------------------------ |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Attributes::render |
||
| 377 | * |
||
| 378 | * @param array $options |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function render(array $options = []) |
||
| 383 | { |
||
| 384 | $output = ''; |
||
| 385 | |||
| 386 | if ($this->count()) { |
||
| 387 | foreach ($this->storage as $key => $value) { |
||
| 388 | $output = trim($output); |
||
| 389 | switch ($key) { |
||
| 390 | case 'style': |
||
| 391 | if (count($value) == 0) { |
||
| 392 | continue 2; |
||
| 393 | } |
||
| 394 | |||
| 395 | $output .= 'style="'; |
||
| 396 | foreach ($value as $styleKey => $styleValue) { |
||
| 397 | $output .= $styleKey . ':' . $styleValue . ';'; |
||
| 398 | } |
||
| 399 | $output .= '"'; |
||
| 400 | |||
| 401 | break; |
||
| 402 | case 'class': |
||
| 403 | if (count($value) == 0) { |
||
| 404 | continue 2; |
||
| 405 | } |
||
| 406 | $output .= ' ' . $key . '="' . implode(' ', $value) . '"'; |
||
| 407 | break; |
||
| 408 | case 'js': |
||
| 409 | $output .= ' ' . $key . '="' . $value . '"'; |
||
| 410 | break; |
||
| 411 | default: |
||
| 412 | if (is_array($value)) { |
||
| 413 | if (count($value) == 0) { |
||
| 414 | continue 2; |
||
| 415 | } |
||
| 416 | $value = implode(', ', $value); |
||
| 417 | } |
||
| 418 | |||
| 419 | if (is_bool($value)) { |
||
| 420 | $value = $value === true ? 'true' : 'false'; |
||
| 421 | } |
||
| 422 | |||
| 423 | if (in_array($key, [ |
||
| 424 | 'controls', |
||
| 425 | 'disabled', |
||
| 426 | 'readonly', |
||
| 427 | 'autocomplete', |
||
| 428 | 'checked', |
||
| 429 | 'loop', |
||
| 430 | 'autoplay', |
||
| 431 | 'muted', |
||
| 432 | ])) { |
||
| 433 | $output .= ' ' . $key; |
||
| 434 | } else { |
||
| 435 | $output .= ' ' . $key . '="' . $value . '"'; |
||
| 436 | } |
||
| 437 | break; |
||
| 438 | } |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | return $output; |
||
| 443 | } |
||
| 444 | } |