Skip to content

Commit 94bcd6f

Browse files
committed
Array accessors added
1 parent 50902ca commit 94bcd6f

File tree

1 file changed

+112
-57
lines changed

1 file changed

+112
-57
lines changed

src/Attribute.php

Lines changed: 112 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use ArrayAccess;
1616
use Carbon\Carbon;
17-
use ReturnTypeWillChange;
1817

1918
/**
2019
* Class Attribute
@@ -43,6 +42,26 @@ public function __construct(string $name, mixed $value = null) {
4342
$this->add($value);
4443
}
4544

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+
}
4665

4766
/**
4867
* Return the stringified attribute
@@ -68,7 +87,7 @@ public function toString(): string {
6887
* @return array
6988
*/
7089
public function toArray(): array {
71-
return $this->values;
90+
return $this->__serialize();
7291
}
7392

7493
/**
@@ -84,51 +103,72 @@ public function toDate(): Carbon {
84103
}
85104

86105
/**
87-
* Determine if a value exists at an offset.
106+
* Determine if a value exists at a given key.
88107
*
89-
* @param mixed $offset
108+
* @param int|string $key
90109
* @return bool
91110
*/
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);
94113
}
95114

96115
/**
97-
* Get a value at a given offset.
116+
* Determine if a value exists at a given key.
98117
*
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
100139
* @return mixed
101140
*/
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;
105143
}
106144

107145
/**
108-
* Set the value at a given offset.
146+
* Set the value by a given key.
109147
*
110-
* @param mixed $offset
111-
* @param mixed $value
112-
* @return void
148+
* @param mixed $key
149+
* @param mixed $value
150+
* @return Attribute
113151
*/
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)) {
117154
$this->values[] = $value;
118155
} else {
119-
$this->values[$offset] = $value;
156+
$this->values[$key] = $value;
120157
}
158+
return $this;
121159
}
122160

123161
/**
124-
* Unset the value at a given offset.
162+
* Unset a value by a given key.
125163
*
126-
* @param string $offset
127-
* @return void
164+
* @param int|string $key
165+
* @return Attribute
128166
*/
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;
132172
}
133173

134174
/**
@@ -138,7 +178,7 @@ public function offsetUnset($offset) {
138178
*
139179
* @return Attribute
140180
*/
141-
public function add($value, bool $strict = false): Attribute {
181+
public function add(mixed $value, bool $strict = false): Attribute {
142182
if (is_array($value)) {
143183
return $this->merge($value, $strict);
144184
}elseif ($value !== null) {
@@ -163,29 +203,21 @@ public function merge(array $values, bool $strict = false): Attribute {
163203
return $this;
164204
}
165205

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-
176206
/**
177207
* Attach a given value to the current value array
178208
* @param $value
179209
* @param bool $strict
210+
* @return Attribute
180211
*/
181-
public function attach($value, bool $strict = false) {
212+
public function attach($value, bool $strict = false): Attribute {
182213
if ($strict === true) {
183214
if ($this->contains($value) === false) {
184215
$this->values[] = $value;
185216
}
186217
}else{
187218
$this->values[] = $value;
188219
}
220+
return $this;
189221
}
190222

191223
/**
@@ -214,41 +246,27 @@ public function getName(): string {
214246
*
215247
* @return array
216248
*/
217-
public function get(): array {
218-
return $this->values;
219-
}
220-
221-
/**
222-
* Alias method for self::get()
223-
*
224-
* @return array
225-
*/
226249
public function all(): array {
227-
return $this->get();
250+
reset($this->values);
251+
return $this->values;
228252
}
229253

230254
/**
231255
* Get the first value if possible
232256
*
233257
* @return mixed|null
234258
*/
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);
240261
}
241262

242263
/**
243264
* Get the last value if possible
244265
*
245266
* @return mixed|null
246267
*/
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);
252270
}
253271

254272
/**
@@ -259,4 +277,41 @@ public function last(){
259277
public function count(): int {
260278
return count($this->values);
261279
}
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+
}
262317
}

0 commit comments

Comments
 (0)