1: <?php declare(strict_types=1);
2:
3: namespace Salient\Collection;
4:
5: use Salient\Contract\Core\Arrayable;
6:
7: /**
8: * @api
9: *
10: * @template TKey of array-key
11: * @template TValue
12: */
13: trait RecursiveArrayableCollectionTrait
14: {
15: /** @use HasItems<TKey,TValue> */
16: use HasItems;
17:
18: /**
19: * @inheritDoc
20: */
21: public function toArray(bool $preserveKeys = true): array
22: {
23: foreach ($this->Items as $key => $value) {
24: if ($value instanceof Arrayable) {
25: $value = $value->toArray($preserveKeys);
26: }
27: if ($preserveKeys) {
28: $array[$key] = $value;
29: } else {
30: $array[] = $value;
31: }
32: }
33: return $array ?? [];
34: }
35: }
36: