Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SchedulesBusiness.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 GuzzleHttp\Exception\ClientException;
  15. use Luticate\Utils\Dbo\LuPaginatedDbo;
  16. class SchedulesBusiness
  17. {
  18. public static $days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
  19. public static $dateFormat = "Y-m-d";
  20. public static $timeFormat = "H:i:s";
  21. protected static function getDataAccess()
  22. {
  23. return new SchedulesDataAccess();
  24. }
  25. public static function getScheduleTypeForDate(array $config, Carbon $date)
  26. {
  27. foreach ($config["scheduleTypes"] as $scheduleType) {
  28. if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
  29. $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
  30. $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
  31. if ($date->between($from, $to)) {
  32. return $scheduleType;
  33. }
  34. }
  35. }
  36. foreach ($config["scheduleTypes"] as $scheduleType) {
  37. if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
  38. return $scheduleType;
  39. }
  40. }
  41. foreach ($config["scheduleTypes"] as $scheduleType) {
  42. if ($scheduleType["type"] == "week") {
  43. $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
  44. $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
  45. if ($date->between($from, $to)) {
  46. return $scheduleType;
  47. }
  48. }
  49. }
  50. foreach ($config["scheduleTypes"] as $scheduleType) {
  51. if ($scheduleType["type"] == "week") {
  52. return $scheduleType;
  53. }
  54. }
  55. return $config["scheduleTypes"][0];
  56. }
  57. public static function getSchedulesForDate(array $schedules, Carbon $date)
  58. {
  59. foreach ($schedules as $schedule) {
  60. if (in_array($date->dayOfWeek, $schedule["week_days"])) {
  61. return $schedule["schedule"];
  62. }
  63. }
  64. return null;
  65. }
  66. public static function getSingle(array $scheduleType, StopsLiteRoutesLiteDbo $stop, Carbon $date, int $count)
  67. {
  68. $dbo = new StopsSchedulesDbo();
  69. $dbo->setRouteId($stop->getRouteId());
  70. $dbo->setStopId($stop->getStopId());
  71. try {
  72. $data = static::getDataAccess()->get($scheduleType["resourceId"], "allweek", $stop->getStopId(), $stop->getRouteId());
  73. }
  74. catch (ClientException $e) {
  75. if ($e->getCode() == 404) {
  76. $dbo->setSchedules([]);
  77. return $dbo;
  78. }
  79. else {
  80. throw $e;
  81. }
  82. }
  83. $schedules = static::getSchedulesForDate($data["data"]["schedules"], $date);
  84. $schedulesCarbon = [];
  85. for ($i = 0; $i < count($schedules) && $count > 0; ++$i) {
  86. $scheduleDate = Carbon::createFromFormat(static::$timeFormat . " " . static::$dateFormat, $schedules[$i] . " " . $date->toDateString());
  87. if ($scheduleDate->gte($date)) {
  88. --$count;
  89. $schedulesCarbon[] = $scheduleDate;
  90. }
  91. }
  92. $dbo->setSchedules($schedulesCarbon);
  93. return $dbo;
  94. }
  95. public static function getMultiple(StopsLiteRoutesLiteDboArray $stops, Carbon $date, int $count)
  96. {
  97. $config = MiscBusiness::getConfig();
  98. $scheduleType = static::getScheduleTypeForDate($config, $date);
  99. $dbos = [];
  100. foreach ($stops->getArray() as $stop) {
  101. $dbo = static::getSingle($scheduleType, $stop, $date, $count);
  102. if (count($dbo->getSchedules()) < $count) {
  103. $count2 = $count - count($dbo->getSchedules());
  104. $date2 = $date->copy();
  105. $date2->addDay(1);
  106. $date2->setTime(0, 0, 0);
  107. $scheduleType2 = static::getScheduleTypeForDate($config, $date2);
  108. $dbo2 = static::getSingle($scheduleType2, $stop, $date2, $count2);
  109. $dbo->setSchedules(array_merge($dbo->getSchedules(), $dbo2->getSchedules()));
  110. }
  111. $dbos[] = $dbo;
  112. }
  113. return new LuPaginatedDbo(count($dbos), $dbos);
  114. }
  115. }