ScheduleCartService.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. //
  17. private function baseScheduleData(array $data): array
  18. {
  19. return Arr::except($data, [
  20. 'schedules',
  21. 'dates',
  22. 'date',
  23. 'period_type',
  24. 'start_time',
  25. 'end_time',
  26. 'total_amount',
  27. 'offers_meal',
  28. ]);
  29. }
  30. private function scheduleItems(array $data): array
  31. {
  32. if (! empty($data['schedules'])) {
  33. return $data['schedules'];
  34. }
  35. if (! empty($data['dates'])) {
  36. return array_map(
  37. fn (string $date) => array_merge(
  38. Arr::only($data, ['period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
  39. ['date' => $date],
  40. ),
  41. $data['dates'],
  42. );
  43. }
  44. return [
  45. Arr::only($data, ['date', 'period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
  46. ];
  47. }
  48. }