Skip to content

Commit bc162cc

Browse files
author
liutao
committed
update toArray
1 parent b61a14d commit bc162cc

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/Data.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ class Data implements ArrayAble, ArrayAccess, JsonSerializable
2121
protected ?ReflectionClass $_staticReflection = null;
2222

2323
/**
24-
* @param array|ArrayAble $data
2524
* @throws
2625
*/
27-
public function __construct(array|Arrayable $data = [])
26+
public function __construct($data = [])
2827
{
29-
$this->fill($data instanceof Arrayable ? $data->toArray() : $data);
28+
$this->fill($this->isArrayAble($data) ? $data->toArray() : $data);
3029
}
3130

3231
public function __toString(): string
@@ -40,7 +39,7 @@ public function __debugInfo(): ?array
4039
}
4140

4241

43-
public static function from(array|Arrayable $data): static
42+
public static function from($data): static
4443
{
4544
return new static($data);
4645
}
@@ -127,7 +126,7 @@ protected function forValue(mixed $value, bool $toSnake)
127126
return array_map(fn($item) => $this->forValue($item, $toSnake), $value);
128127
}
129128
if (is_object($value)) {
130-
if ($value instanceof Arrayable) {
129+
if ($this->isArrayAble($value)) {
131130
return $value->toArray();
132131
}
133132
return $this->objectToArray($value, $toSnake);
@@ -162,4 +161,8 @@ protected function isInsideProperty(ReflectionProperty $property): bool
162161
return Str::startsWith($property->getName(), '_');
163162
}
164163

164+
protected function isArrayAble($data): bool
165+
{
166+
return $data instanceof ArrayAble || is_object($data) && method_exists($data, 'toArray');
167+
}
165168
}

0 commit comments

Comments
 (0)