* * @throws \RuntimeException */ public static function toArray(mixed $data, ?int $limit = null): array { return self::toArrayWithDepth($data, $limit ?? PHP_INT_MAX, 1); } /** * @return ($depth is 1 ? array : mixed) */ private static function toArrayWithDepth(mixed $data, int $limit, int $depth): mixed { if ($limit <= 0) { throw new UnexpectedValueException('Limit value should be positive number'); } if ($depth > $limit) { return $data; } if ($data instanceof Arrayable) { $data = $data->__toArray(); } elseif ($data instanceof stdClass) { $data = (array) $data; } if (is_iterable($data)) { $arrayData = []; foreach ($data as $k => $v) { if (!is_int($k) && !is_string($k)) { throw new RuntimeException('Iterable key must be int|string'); } $arrayData[$k] = self::toArrayWithDepth($v, $limit, $depth + 1); } return $arrayData; } if ($depth === 1) { throw new RuntimeException('Cannot cast to array'); } return $data; } }