| Total Complexity | 14 |
| Total Lines | 54 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 1 |
| 1 | <?php |
||
| 5 | trait GluesAttributes |
||
| 6 | { |
||
| 7 | public function glueAttributes($attributesList = null) |
||
| 8 | { |
||
| 9 | if (is_null($attributesList)) { |
||
| 10 | $attributesList = $this->attributesList(); |
||
| 11 | } |
||
| 12 | |||
| 13 | $pairs = []; |
||
| 14 | foreach ($attributesList as $attr) { |
||
| 15 | if (isset($this->{$attr})) { |
||
| 16 | |||
| 17 | // Edge cases |
||
| 18 | if ($attr == 'autocomplete') { |
||
| 19 | $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr} ? 'on' : 'off'); |
||
| 20 | continue; |
||
| 21 | } |
||
| 22 | |||
| 23 | if ($attr == 'class' && is_array($this->{$attr})) { |
||
| 24 | $pairs[] = sprintf('%s="%s"', $attr, implode(' ', $this->{$attr})); |
||
| 25 | continue; |
||
| 26 | } |
||
| 27 | |||
| 28 | // Required, disabled, readonly: true/false |
||
| 29 | if (is_bool($this->{$attr})) { |
||
| 30 | if ($this->{$attr} == true) { |
||
| 31 | $pairs[] = $attr; |
||
| 32 | } |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | |||
| 36 | $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr}); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | $customAttributes = $this->customAttributes(); |
||
| 41 | |||
| 42 | if (!empty($customAttributes)) { |
||
| 43 | foreach ($customAttributes as $attrName => $attrVal) { |
||
| 44 | $pairs[] = sprintf('%s="%s"', $attrName, $attrVal); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | return implode(' ', $pairs); |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function attributesList() |
||
| 52 | { |
||
| 53 | return []; |
||
| 54 | } |
||
| 55 | |||
| 56 | protected function customAttributes() |
||
| 59 | } |
||
| 60 | } |
||
| 61 |