HtmlAttributes.php
68 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Forms\Field\Traits; |
| 4 | |
| 5 | /** |
| 6 | * Implementation of HTML attributes like id, name, action etc. |
| 7 | * |
| 8 | * @package WPDesk\Forms\Field\Traits |
| 9 | */ |
| 10 | trait HtmlAttributes |
| 11 | { |
| 12 | /** @var string[] */ |
| 13 | protected $attributes; |
| 14 | /** |
| 15 | * Get list of all attributes except given. |
| 16 | * |
| 17 | * @param string[] $except |
| 18 | * |
| 19 | * @return string[] |
| 20 | */ |
| 21 | public function get_attributes($except = ['name', 'type']) |
| 22 | { |
| 23 | return array_filter($this->attributes, function ($value, $key) use ($except) { |
| 24 | return !in_array($key, $except, \true); |
| 25 | }, \ARRAY_FILTER_USE_BOTH); |
| 26 | } |
| 27 | /** |
| 28 | * @param string $name |
| 29 | * @param string $value |
| 30 | * |
| 31 | * @return $this |
| 32 | */ |
| 33 | public function set_attribute($name, $value) |
| 34 | { |
| 35 | $this->attributes[$name] = $value; |
| 36 | return $this; |
| 37 | } |
| 38 | /** |
| 39 | * @param string $name |
| 40 | * |
| 41 | * @return $this |
| 42 | */ |
| 43 | public function unset_attribute($name) |
| 44 | { |
| 45 | unset($this->attributes[$name]); |
| 46 | return $this; |
| 47 | } |
| 48 | /** |
| 49 | * @param string $name |
| 50 | * |
| 51 | * @return bool |
| 52 | */ |
| 53 | public function is_attribute_set($name) |
| 54 | { |
| 55 | return isset($this->attributes[$name]); |
| 56 | } |
| 57 | /** |
| 58 | * @param string $name |
| 59 | * @param mixed $default |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function get_attribute($name, $default = null) |
| 64 | { |
| 65 | return $this->attributes[$name] ?? $default; |
| 66 | } |
| 67 | } |
| 68 |