BaseDTOCollection.php 891 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 string The name of the base DTO class.
  12. */
  13. public static $base = '';
  14. public function __construct(array $collection = [])
  15. {
  16. // Manually cast nested arrays
  17. $collection = array_map(
  18. function ($item) {
  19. return is_array($item) ? new static::$base($item) : $item;
  20. },
  21. $collection
  22. );
  23. parent::__construct($collection);
  24. }
  25. /**
  26. * @param T[] $items
  27. */
  28. public function concat($items)
  29. {
  30. foreach ($items as $item) {
  31. $this[] = is_array($item) ? new static::$base($item) : $item;
  32. }
  33. }
  34. }