ScheduleCartService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Arr;
  4. class ScheduleCartService
  5. {
  6. public function __construct(
  7. private readonly ScheduleService $scheduleService,
  8. ) {}
  9. public function create(array $data): array
  10. {
  11. return $this->scheduleService->createSingleOrMultiple(
  12. baseData: $this->baseScheduleData($data),
  13. schedules: $this->scheduleItems($data),
  14. );
  15. }
  16. private function baseScheduleData(array $data): array
  17. {
  18. return Arr::except($data, [
  19. 'schedules',
  20. 'dates',
  21. 'date',
  22. 'period_type',
  23. 'start_time',
  24. 'end_time',
  25. 'total_amount',
  26. 'offers_meal',
  27. ]);
  28. }
  29. private function scheduleItems(array $data): array
  30. {
  31. if (! empty($data['schedules'])) {
  32. return $data['schedules'];
  33. }
  34. if (! empty($data['dates'])) {
  35. return array_map(
  36. fn (string $date) => array_merge(
  37. Arr::only($data, ['period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
  38. ['date' => $date],
  39. ),
  40. $data['dates'],
  41. );
  42. }
  43. return [
  44. Arr::only($data, ['date', 'period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
  45. ];
  46. }
  47. }