123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 10/1/16
- * Time: 5:32 PM
- */
-
- namespace App\Business;
-
-
- use App\DataAccess\SchedulesDataAccess;
- use App\Dbo\StopsLiteRoutesLiteDbo;
- use App\Dbo\StopsLiteRoutesLiteDboArray;
- use App\Dbo\StopsSchedulesDbo;
- use Carbon\Carbon;
- use Luticate\Utils\Dbo\LuPaginatedDbo;
-
- class SchedulesBusiness
- {
- public static $days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
- public static $dateFormat = "Y-m-d";
- public static $timeFormat = "H:i:s";
-
- protected static function getDataAccess()
- {
- return new SchedulesDataAccess();
- }
-
- public static function getScheduleTypeForDate(array $config, Carbon $date)
- {
-
- foreach ($config["scheduleTypes"] as $scheduleType) {
- if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
- $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
- $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
- if ($date->between($from, $to)) {
- return $scheduleType;
- }
- }
- }
-
- foreach ($config["scheduleTypes"] as $scheduleType) {
- if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
- return $scheduleType;
- }
- }
-
- foreach ($config["scheduleTypes"] as $scheduleType) {
- if ($scheduleType["type"] == "week") {
- $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
- $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
- if ($date->between($from, $to)) {
- return $scheduleType;
- }
- }
- }
-
- foreach ($config["scheduleTypes"] as $scheduleType) {
- if ($scheduleType["type"] == "week") {
- return $scheduleType;
- }
- }
-
- return $config["scheduleTypes"][0];
- }
-
- public static function getSchedulesForDate(array $schedules, Carbon $date)
- {
- foreach ($schedules as $schedule) {
- if (in_array($date->dayOfWeek, $schedule["week_days"])) {
- return $schedule["schedule"];
- }
- }
- return null;
- }
-
- public static function getSingle(array $scheduleType, StopsLiteRoutesLiteDbo $stop, Carbon $date, int $count)
- {
- $data = static::getDataAccess()->get($scheduleType["resourceId"], "allweek", $stop->getStopId(), $stop->getRouteId());
- $schedules = static::getSchedulesForDate($data["data"]["schedules"], $date);
-
- $schedulesCarbon = [];
-
- for ($i = 0; $i < min($count, count($schedules)); ++$i) {
- $scheduleDate = Carbon::createFromFormat(static::$timeFormat . " " . static::$dateFormat, $schedules[$i] . " " . $date->toDateString());
- if ($scheduleDate->gte($date)) {
- $schedulesCarbon[] = $scheduleDate;
- }
- }
-
- $dbo = new StopsSchedulesDbo();
- $dbo->setRouteId($stop->getRouteId());
- $dbo->setStopId($stop->getStopId());
- $dbo->setSchedules($schedulesCarbon);
- return $dbo;
- }
-
- public static function getMultiple(StopsLiteRoutesLiteDboArray $stops, Carbon $date, int $count)
- {
- $config = MiscBusiness::getConfig();
-
- $scheduleType = static::getScheduleTypeForDate($config, $date);
-
- $dbos = [];
-
- foreach ($stops->getArray() as $stop) {
- $dbo = static::getSingle($scheduleType, $stop, $date, $count);
- if (count($dbo->getSchedules()) < $count) {
- $count2 = $count - count($dbo->getSchedules());
- $date2 = $date->copy();
- $date2->addDay(1);
- $date2->setTime(0, 0, 0);
- $scheduleType2 = static::getScheduleTypeForDate($config, $date2);
- $dbo2 = static::getSingle($scheduleType2, $stop, $date2, $count2);
- $dbo->setSchedules(array_merge($dbo->getSchedules(), $dbo2->getSchedules()));
- }
- $dbos[] = $dbo;
- }
-
- return $dbos;
- }
- }
|