1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Itmedia\ZippyBusBundle\Factory; |
6
|
|
|
|
7
|
|
|
use Itmedia\ZippyBusBundle\Schedule\City; |
8
|
|
|
use Itmedia\ZippyBusBundle\Schedule\Direction; |
9
|
|
|
use Itmedia\ZippyBusBundle\Schedule\Route; |
10
|
|
|
use Itmedia\ZippyBusBundle\Schedule\ScheduleDate; |
11
|
|
|
use Itmedia\ZippyBusBundle\Schedule\Stop; |
12
|
|
|
use Itmedia\ZippyBusBundle\Schedule\StopTime; |
13
|
|
|
|
14
|
|
|
class ScheduleObjectFromArrayFactory |
15
|
|
|
{ |
16
|
|
|
public function createCity(array $array): City |
17
|
|
|
{ |
18
|
|
|
$version = 0; |
19
|
|
|
$dateUpdated = null; |
20
|
|
|
if (isset($array['currentVersions'][0])) { |
21
|
|
|
$versionData = $array['currentVersions'][0]; |
22
|
|
|
$dateUpdated = new \DateTime($versionData['startDate']); |
23
|
|
|
$version = (int)$versionData['id']; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
return new City( |
28
|
|
|
(int)$array['id'], |
29
|
|
|
(string)$array['name'], |
30
|
|
|
$version, |
31
|
|
|
$dateUpdated ?: new \DateTime('now') |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function createRoute(array $array, ScheduleDate $date): Route |
37
|
|
|
{ |
38
|
|
|
$directions = []; |
39
|
|
|
foreach ($array['directions'] ?? [] as $directionArray) { |
40
|
|
|
$direction = new Direction( |
41
|
|
|
(int)$directionArray['id'], |
42
|
|
|
(string)$directionArray['name'], |
43
|
|
|
(string)$directionArray['techName'], |
44
|
|
|
$directionArray['days'] |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if (in_array($date->getWeekDay(), $direction->getDays(), true)) { |
48
|
|
|
$directions[] = $direction; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new Route( |
53
|
|
|
(int)$array['id'], |
54
|
|
|
(string)$array['uniqueTechName'], |
55
|
|
|
(string)$array['name'], |
56
|
|
|
(int)$array['versionId'], |
57
|
|
|
$directions |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function createStop(array $array): Stop |
63
|
|
|
{ |
64
|
|
|
$times = []; |
65
|
|
|
foreach ($array['schedule']['minutes'] ?? [] as $minute) { |
66
|
|
|
$times[(int)$minute] = new StopTime((int)$minute, false); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($array['schedule']['partialMinutes'][0]['minutes'] ?? [] as $minute) { |
70
|
|
|
$times[(int)$minute] = new StopTime((int)$minute, true, $array['schedule']['partialMinutes'][0]['notes'] ?? ''); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new Stop( |
74
|
|
|
(int)$array['id'], |
75
|
|
|
(string)$array['name'], |
76
|
|
|
(string)$array['uniqueTechName'], |
77
|
|
|
$times |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|