31 lines
780 B
PHP
31 lines
780 B
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
use Illuminate\Support\Collection;
|
|
use Traversable;
|
|
use UnexpectedValueException;
|
|
|
|
final class StructuredData
|
|
{
|
|
/** @return array<array-key, mixed> */
|
|
public static function collectionItems(mixed $value): array
|
|
{
|
|
if ($value instanceof Collection) {
|
|
return $value->all();
|
|
}
|
|
if ($value instanceof Traversable) {
|
|
return iterator_to_array($value);
|
|
}
|
|
if ($value instanceof Arrayable) {
|
|
return $value->toArray();
|
|
}
|
|
if (! is_array($value)) {
|
|
throw new UnexpectedValueException('Structured data must contain an array; original data has been preserved.');
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|