選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SchedulesBusiness.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 10/1/16
  6. * Time: 5:32 PM
  7. */
  8. namespace App\Business;
  9. use App\DataAccess\SchedulesDataAccess;
  10. use App\Dbo\StopsLiteRoutesLiteDbo;
  11. use App\Dbo\StopsLiteRoutesLiteDboArray;
  12. use App\Dbo\StopsSchedulesDbo;
  13. use Carbon\Carbon;
  14. use Luticate\Utils\Dbo\LuPaginatedDbo;
  15. class SchedulesBusiness
  16. {
  17. public static $days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
  18. public static $dateFormat = "Y-m-d";
  19. public static $timeFormat = "H:i:s";
  20. protected static function getDataAccess()
  21. {
  22. return new SchedulesDataAccess();
  23. }
  24. public static function getScheduleTypeForDate(array $config, Carbon $date)
  25. {
  26. foreach ($config["scheduleTypes"] as $scheduleType) {
  27. if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
  28. $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
  29. $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
  30. if ($date->between($from, $to)) {
  31. return $scheduleType;
  32. }
  33. }
  34. }
  35. foreach ($config["scheduleTypes"] as $scheduleType) {
  36. if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
  37. return $scheduleType;
  38. }
  39. }
  40. foreach ($config["scheduleTypes"] as $scheduleType) {
  41. if ($scheduleType["type"] == "week") {
  42. $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
  43. $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
  44. if ($date->between($from, $to)) {
  45. return $scheduleType;
  46. }
  47. }
  48. }
  49. foreach ($config["scheduleTypes"] as $scheduleType) {
  50. if ($scheduleType["type"] == "week") {
  51. return $scheduleType;
  52. }
  53. }
  54. return $config["scheduleTypes"][0];
  55. }
  56. public static function getSchedulesForDate(array $schedules, Carbon $date)
  57. {
  58. foreach ($schedules as $schedule) {
  59. if (in_array($date->dayOfWeek, $schedule["week_days"])) {
  60. return $schedule["schedule"];
  61. }
  62. }
  63. return null;
  64. }
  65. public static function getSingle(array $scheduleType, StopsLiteRoutesLiteDbo $stop, Carbon $date, int $count)
  66. {
  67. $data = static::getDataAccess()->get($scheduleType["resourceId"], "allweek", $stop->getStopId(), $stop->getRouteId());
  68. $schedules = static::getSchedulesForDate($data["data"]["schedules"], $date);
  69. $schedulesCarbon = [];
  70. for ($i = 0; $i < min($count, count($schedules)); ++$i) {
  71. $scheduleDate = Carbon::createFromFormat(static::$timeFormat . " " . static::$dateFormat, $schedules[$i] . " " . $date->toDateString());
  72. if ($scheduleDate->gte($date)) {
  73. $schedulesCarbon[] = $scheduleDate;
  74. }
  75. }
  76. $dbo = new StopsSchedulesDbo();
  77. $dbo->setRouteId($stop->getRouteId());
  78. $dbo->setStopId($stop->getStopId());
  79. $dbo->setSchedules($schedulesCarbon);
  80. return $dbo;
  81. }
  82. public static function getMultiple(StopsLiteRoutesLiteDboArray $stops, Carbon $date, int $count)
  83. {
  84. $config = MiscBusiness::getConfig();
  85. $scheduleType = static::getScheduleTypeForDate($config, $date);
  86. $dbos = [];
  87. foreach ($stops->getArray() as $stop) {
  88. $dbo = static::getSingle($scheduleType, $stop, $date, $count);
  89. if (count($dbo->getSchedules()) < $count) {
  90. $count2 = $count - count($dbo->getSchedules());
  91. $date2 = $date->copy();
  92. $date2->addDay(1);
  93. $date2->setTime(0, 0, 0);
  94. $scheduleType2 = static::getScheduleTypeForDate($config, $date2);
  95. $dbo2 = static::getSingle($scheduleType2, $stop, $date2, $count2);
  96. $dbo->setSchedules(array_merge($dbo->getSchedules(), $dbo2->getSchedules()));
  97. }
  98. $dbos[] = $dbo;
  99. }
  100. return $dbos;
  101. }
  102. }