Browse Source

get schedules

tags/v1.0.0
Robin Thoni 8 years ago
parent
commit
90a30269ce

+ 123
- 0
app/Business/SchedulesBusiness.php View File

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

+ 36
- 0
app/Controller/SchedulesController.php View File

@@ -0,0 +1,36 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/1/16
6
+ * Time: 5:33 PM
7
+ */
8
+
9
+namespace App\Controller;
10
+
11
+use App\Business\SchedulesBusiness;
12
+use App\Dbo\StopsLiteRoutesLiteDboArray;
13
+use Luticate\Utils\Controller\LuController;
14
+use Luticate\Utils\Dbo\LuDateTimeDbo;
15
+use Luticate\Utils\Dbo\LuIntDbo;
16
+use Luticate\Utils\Dbo\LuPaginatedDbo;
17
+
18
+class SchedulesController extends LuController
19
+{
20
+    protected function getBusiness()
21
+    {
22
+        return new SchedulesBusiness();
23
+    }
24
+
25
+    /**
26
+     * Get schedules for multiple stops/routes
27
+     * @param StopsLiteRoutesLiteDboArray $stops The stops and routes to retrieve
28
+     * @param LuDateTimeDbo $date The date to get the schedules
29
+     * @param LuIntDbo $count The maximum number of schedules per stops/routes
30
+     * @return LuPaginatedDbo
31
+     */
32
+    public function getMultiple(StopsLiteRoutesLiteDboArray $stops, LuDateTimeDbo $date, LuIntDbo $count)
33
+    {
34
+        return static::getBusiness()->getMultiple($stops, $date->getDateTime(), $count->getInt());
35
+    }
36
+}

+ 25
- 0
app/DataAccess/SchedulesDataAccess.php View File

@@ -0,0 +1,25 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/1/16
6
+ * Time: 5:32 PM
7
+ */
8
+
9
+namespace App\DataAccess;
10
+
11
+
12
+use GuzzleHttp\Client;
13
+use Luticate\Utils\Controller\LuticateApplication;
14
+
15
+class SchedulesDataAccess
16
+{
17
+    public static function get(string $resourceId, string $type, string $stopId, string $routeId)
18
+    {
19
+        $client = new Client();
20
+        $entrypoint = LuticateApplication::getInstance()->getSetting("API_ENTRYPOINT");
21
+        $response = $client->request("GET", $entrypoint . "transit/${resourceId}/${type}/stops/${stopId}/routes/${routeId}/schedule.json");
22
+        $data = json_decode($response->getBody(), true);
23
+        return $data;
24
+    }
25
+}

+ 57
- 0
app/Dbo/StopsLiteRoutesLiteDbo.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/1/16
6
+ * Time: 5:39 PM
7
+ */
8
+
9
+namespace App\Dbo;
10
+
11
+use Luticate\Utils\Dbo\LuDbo;
12
+
13
+class StopsLiteRoutesLiteDbo extends LuDbo
14
+{
15
+    /**
16
+     * @var $_stopId string
17
+     */
18
+    protected $_stopId;
19
+
20
+    /**
21
+     * @var $_routeId string
22
+     */
23
+    protected $_routeId;
24
+
25
+    /**
26
+     * @return string
27
+     */
28
+    public function getStopId(): string
29
+    {
30
+        return $this->_stopId;
31
+    }
32
+
33
+    /**
34
+     * @param string $stopId
35
+     */
36
+    public function setStopId(string $stopId)
37
+    {
38
+        $this->_stopId = $stopId;
39
+    }
40
+
41
+    /**
42
+     * @return string
43
+     */
44
+    public function getRouteId(): string
45
+    {
46
+        return $this->_routeId;
47
+    }
48
+
49
+    /**
50
+     * @param string $routeId
51
+     */
52
+    public function setRouteId(string $routeId)
53
+    {
54
+        $this->_routeId = $routeId;
55
+    }
56
+
57
+}

+ 55
- 0
app/Dbo/StopsLiteRoutesLiteDboArray.php View File

@@ -0,0 +1,55 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/1/16
6
+ * Time: 5:52 PM
7
+ */
8
+
9
+namespace App\Dbo;
10
+
11
+use Luticate\Utils\Dbo\LuDbo;
12
+use Luticate\Utils\Dbo\LuDboDeserializeException;
13
+
14
+class StopsLiteRoutesLiteDboArray extends LuDbo
15
+{
16
+    /**
17
+     * @var StopsLiteRoutesLiteDbo[]
18
+     */
19
+    protected $_array;
20
+    public function getArray()
21
+    {
22
+        return $this->_array;
23
+    }
24
+    public function setArray($value)
25
+    {
26
+        $this->_array = $value;
27
+    }
28
+
29
+    public function jsonSerialize()
30
+    {
31
+        return $this->_array;
32
+    }
33
+
34
+    public static function jsonDeserialize($json)
35
+    {
36
+        if (!is_array($json)) {
37
+            throw new LuDboDeserializeException("Invalid array value");
38
+        }
39
+        $dbo = new self();
40
+        $array = [];
41
+        foreach ($json as $data) {
42
+            $array[] = StopsLiteRoutesLiteDbo::jsonDeserialize($data);
43
+        }
44
+        $dbo->setArray($array);
45
+        return $dbo;
46
+    }
47
+
48
+    public static function generateSample()
49
+    {
50
+        return [
51
+            StopsLiteRoutesLiteDbo::generateSample(),
52
+            StopsLiteRoutesLiteDbo::generateSample()
53
+        ];
54
+    }
55
+}

+ 37
- 0
app/Dbo/StopsSchedulesDbo.php View File

@@ -0,0 +1,37 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 10/1/16
6
+ * Time: 9:45 PM
7
+ */
8
+
9
+namespace App\Dbo;
10
+
11
+use Carbon\Carbon;
12
+
13
+class StopsSchedulesDbo extends StopsLiteRoutesLiteDbo
14
+{
15
+    /**
16
+     * @var $_schedules Carbon[]
17
+     */
18
+    protected $_schedules;
19
+
20
+    /**
21
+     * @return \Carbon\Carbon[]
22
+     */
23
+    public function getSchedules(): array
24
+    {
25
+        return $this->_schedules;
26
+    }
27
+
28
+    /**
29
+     * @param \Carbon\Carbon[] $schedules
30
+     */
31
+    public function setSchedules(array $schedules)
32
+    {
33
+        $this->_schedules = $schedules;
34
+    }
35
+
36
+
37
+}

+ 2
- 0
app/routes.php View File

@@ -13,3 +13,5 @@ $route = $app->getRouter();
13 13
 $route->get("/routes", "Routes", "getAll");
14 14
 
15 15
 $route->get("/stops", "Stops", "getAll");
16
+
17
+$route->post("/schedules/multiple", "Schedules", "getMultiple");

Loading…
Cancel
Save