14
14
15
15
use ArrayAccess ;
16
16
use Carbon \Carbon ;
17
- use ReturnTypeWillChange ;
18
17
19
18
/**
20
19
* Class Attribute
@@ -43,6 +42,26 @@ public function __construct(string $name, mixed $value = null) {
43
42
$ this ->add ($ value );
44
43
}
45
44
45
+ /**
46
+ * Handle class invocation calls
47
+ *
48
+ * @return array|string
49
+ */
50
+ public function __invoke (): array |string {
51
+ if ($ this ->count () > 1 ) {
52
+ return $ this ->toArray ();
53
+ }
54
+ return $ this ->toString ();
55
+ }
56
+
57
+ /**
58
+ * Return the serialized address
59
+ *
60
+ * @return array
61
+ */
62
+ public function __serialize (){
63
+ return $ this ->values ;
64
+ }
46
65
47
66
/**
48
67
* Return the stringified attribute
@@ -68,7 +87,7 @@ public function toString(): string {
68
87
* @return array
69
88
*/
70
89
public function toArray (): array {
71
- return $ this ->values ;
90
+ return $ this ->__serialize () ;
72
91
}
73
92
74
93
/**
@@ -84,51 +103,72 @@ public function toDate(): Carbon {
84
103
}
85
104
86
105
/**
87
- * Determine if a value exists at an offset .
106
+ * Determine if a value exists at a given key .
88
107
*
89
- * @param mixed $offset
108
+ * @param int|string $key
90
109
* @return bool
91
110
*/
92
- public function offsetExists ( $ offset ): bool {
93
- return array_key_exists ($ offset , $ this ->values );
111
+ public function has ( mixed $ key = 0 ): bool {
112
+ return array_key_exists ($ key , $ this ->values );
94
113
}
95
114
96
115
/**
97
- * Get a value at a given offset .
116
+ * Determine if a value exists at a given key .
98
117
*
99
- * @param mixed $offset
118
+ * @param int|string $key
119
+ * @return bool
120
+ */
121
+ public function exist (mixed $ key = 0 ): bool {
122
+ return $ this ->has ($ key );
123
+ }
124
+
125
+ /**
126
+ * Check if the attribute contains the given value
127
+ * @param mixed $value
128
+ *
129
+ * @return bool
130
+ */
131
+ public function contains (mixed $ value ): bool {
132
+ return in_array ($ value , $ this ->values , true );
133
+ }
134
+
135
+ /**
136
+ * Get a value by a given key.
137
+ *
138
+ * @param int|string $key
100
139
* @return mixed
101
140
*/
102
- #[ReturnTypeWillChange]
103
- public function offsetGet ($ offset ) {
104
- return $ this ->values [$ offset ];
141
+ public function get (int |string $ key = 0 ): mixed {
142
+ return $ this ->values [$ key ] ?? null ;
105
143
}
106
144
107
145
/**
108
- * Set the value at a given offset .
146
+ * Set the value by a given key .
109
147
*
110
- * @param mixed $offset
111
- * @param mixed $value
112
- * @return void
148
+ * @param mixed $key
149
+ * @param mixed $value
150
+ * @return Attribute
113
151
*/
114
- #[ReturnTypeWillChange]
115
- public function offsetSet ($ offset , $ value ) {
116
- if (is_null ($ offset )) {
152
+ public function set (mixed $ value , mixed $ key = 0 ): Attribute {
153
+ if (is_null ($ key )) {
117
154
$ this ->values [] = $ value ;
118
155
} else {
119
- $ this ->values [$ offset ] = $ value ;
156
+ $ this ->values [$ key ] = $ value ;
120
157
}
158
+ return $ this ;
121
159
}
122
160
123
161
/**
124
- * Unset the value at a given offset .
162
+ * Unset a value by a given key .
125
163
*
126
- * @param string $offset
127
- * @return void
164
+ * @param int| string $key
165
+ * @return Attribute
128
166
*/
129
- #[ReturnTypeWillChange]
130
- public function offsetUnset ($ offset ) {
131
- unset($ this ->values [$ offset ]);
167
+ public function remove (int |string $ key = 0 ): Attribute {
168
+ if (isset ($ this ->values [$ key ])) {
169
+ unset($ this ->values [$ key ]);
170
+ }
171
+ return $ this ;
132
172
}
133
173
134
174
/**
@@ -138,7 +178,7 @@ public function offsetUnset($offset) {
138
178
*
139
179
* @return Attribute
140
180
*/
141
- public function add ($ value , bool $ strict = false ): Attribute {
181
+ public function add (mixed $ value , bool $ strict = false ): Attribute {
142
182
if (is_array ($ value )) {
143
183
return $ this ->merge ($ value , $ strict );
144
184
}elseif ($ value !== null ) {
@@ -163,29 +203,21 @@ public function merge(array $values, bool $strict = false): Attribute {
163
203
return $ this ;
164
204
}
165
205
166
- /**
167
- * Check if the attribute contains the given value
168
- * @param mixed $value
169
- *
170
- * @return bool
171
- */
172
- public function contains ($ value ): bool {
173
- return in_array ($ value , $ this ->values , true );
174
- }
175
-
176
206
/**
177
207
* Attach a given value to the current value array
178
208
* @param $value
179
209
* @param bool $strict
210
+ * @return Attribute
180
211
*/
181
- public function attach ($ value , bool $ strict = false ) {
212
+ public function attach ($ value , bool $ strict = false ): Attribute {
182
213
if ($ strict === true ) {
183
214
if ($ this ->contains ($ value ) === false ) {
184
215
$ this ->values [] = $ value ;
185
216
}
186
217
}else {
187
218
$ this ->values [] = $ value ;
188
219
}
220
+ return $ this ;
189
221
}
190
222
191
223
/**
@@ -214,41 +246,27 @@ public function getName(): string {
214
246
*
215
247
* @return array
216
248
*/
217
- public function get (): array {
218
- return $ this ->values ;
219
- }
220
-
221
- /**
222
- * Alias method for self::get()
223
- *
224
- * @return array
225
- */
226
249
public function all (): array {
227
- return $ this ->get ();
250
+ reset ($ this ->values );
251
+ return $ this ->values ;
228
252
}
229
253
230
254
/**
231
255
* Get the first value if possible
232
256
*
233
257
* @return mixed|null
234
258
*/
235
- public function first (){
236
- if ($ this ->offsetExists (0 )) {
237
- return $ this ->values [0 ];
238
- }
239
- return null ;
259
+ public function first (): mixed {
260
+ return reset ($ this ->values );
240
261
}
241
262
242
263
/**
243
264
* Get the last value if possible
244
265
*
245
266
* @return mixed|null
246
267
*/
247
- public function last (){
248
- if (($ cnt = $ this ->count ()) > 0 ) {
249
- return $ this ->values [$ cnt - 1 ];
250
- }
251
- return null ;
268
+ public function last (): mixed {
269
+ return end ($ this ->values );
252
270
}
253
271
254
272
/**
@@ -259,4 +277,41 @@ public function last(){
259
277
public function count (): int {
260
278
return count ($ this ->values );
261
279
}
280
+
281
+ /**
282
+ * @see ArrayAccess::offsetExists
283
+ * @param mixed $offset
284
+ * @return bool
285
+ */
286
+ public function offsetExists (mixed $ offset ): bool {
287
+ return $ this ->has ($ offset );
288
+ }
289
+
290
+ /**
291
+ * @see ArrayAccess::offsetGet
292
+ * @param mixed $offset
293
+ * @return mixed
294
+ */
295
+ public function offsetGet (mixed $ offset ): mixed {
296
+ return $ this ->get ($ offset );
297
+ }
298
+
299
+ /**
300
+ * @see ArrayAccess::offsetSet
301
+ * @param mixed $offset
302
+ * @param mixed $value
303
+ * @return void
304
+ */
305
+ public function offsetSet (mixed $ offset , mixed $ value ): void {
306
+ $ this ->set ($ value , $ offset );
307
+ }
308
+
309
+ /**
310
+ * @see ArrayAccess::offsetUnset
311
+ * @param mixed $offset
312
+ * @return void
313
+ */
314
+ public function offsetUnset (mixed $ offset ): void {
315
+ $ this ->remove ($ offset );
316
+ }
262
317
}
0 commit comments