1 | <?php |
||
11 | class Attributes implements AttributesContract |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $attributes; |
||
17 | |||
18 | /** |
||
19 | * @param array $attributes |
||
20 | */ |
||
21 | 24 | public function __construct(array $attributes) |
|
22 | { |
||
23 | 24 | $this->attributes = $attributes; |
|
24 | 24 | } |
|
25 | |||
26 | /** |
||
27 | * Get attribute by name |
||
28 | * |
||
29 | * @param string $name |
||
30 | * @param string|null $default |
||
31 | * @return string|null |
||
32 | */ |
||
33 | 4 | public function get($name, $default = null) |
|
34 | { |
||
35 | 4 | if (array_key_exists($name, $this->attributes)) { |
|
36 | 3 | return $this->attributes[$name]; |
|
37 | } |
||
38 | 1 | return $default; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Set array attributes |
||
43 | * |
||
44 | * @param array $attributes |
||
45 | * @return AttributesContract |
||
46 | */ |
||
47 | 2 | public function set(array $attributes) |
|
48 | { |
||
49 | 2 | $this->attributes = $attributes; |
|
50 | |||
51 | 2 | return $this; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get all attributes |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | 9 | public function all() |
|
60 | { |
||
61 | 9 | return $this->attributes; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Check exits attribute by name |
||
66 | * |
||
67 | * @param string $name |
||
68 | * @return bool |
||
69 | */ |
||
70 | 2 | public function has($name) |
|
71 | { |
||
72 | 2 | return array_key_exists($name, $this->attributes); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Delete attribute by name |
||
77 | * |
||
78 | * @param string $name |
||
79 | */ |
||
80 | 1 | public function forget($name) |
|
81 | { |
||
82 | 1 | if ($this->has($name)) { |
|
83 | 1 | unset($this->attributes[$name]); |
|
84 | 1 | } |
|
85 | 1 | } |
|
86 | |||
87 | /** |
||
88 | * Set attribute or attributes. |
||
89 | * No merge attributes value. |
||
90 | * |
||
91 | * @param array $attributes |
||
92 | * @return AttributesContract |
||
93 | */ |
||
94 | 2 | public function push(array $attributes) |
|
95 | { |
||
96 | 2 | $this->attributes = array_merge($this->attributes, $attributes); |
|
97 | |||
98 | 2 | return $this; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Set attribute value |
||
103 | * |
||
104 | * @param string $name |
||
105 | * @param string $value |
||
106 | * @return AttributesContract |
||
107 | */ |
||
108 | 1 | public function put($name, $value) |
|
114 | |||
115 | /** |
||
116 | * Merge attributes and merge their values |
||
117 | * |
||
118 | * @param array $attributes |
||
119 | * @return AttributesContract |
||
120 | */ |
||
121 | 1 | public function merge(array $attributes) |
|
127 | |||
128 | /** |
||
129 | * Build attributes html valid string |
||
130 | * |
||
131 | * @param array $attributes |
||
132 | * @return string |
||
133 | */ |
||
134 | 5 | public function build($attributes = []) |
|
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | public function __toString() |
|
154 | } |