BaseDTOCollection.php 863 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace Knuckles\Camel;
  3. use Knuckles\Camel\Extraction\T;
  4. use Spatie\DataTransferObject\DataTransferObjectCollection;
  5. /**
  6. * @template T of \Spatie\DataTransferObject
  7. */
  8. class BaseDTOCollection extends DataTransferObjectCollection
  9. {
  10. /**
  11. * @var class-string<T> The name of the base DTO class.
  12. */
  13. public static string $base = '';
  14. public function __construct(array $collection = [])
  15. {
  16. // Manually cast nested arrays
  17. $collection = array_map(
  18. fn($item) => is_array($item) ? new static::$base($item) : $item,
  19. $collection
  20. );
  21. parent::__construct($collection);
  22. }
  23. /**
  24. * @param T[] $items
  25. */
  26. public function concat($items)
  27. {
  28. foreach ($items as $item) {
  29. $this[] = is_array($item) ? new static::$base($item) : $item;
  30. }
  31. }
  32. }