1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\Melvin\Parsers; |
6
|
|
|
|
7
|
|
|
use Swis\Melvin\Enums\Direction; |
8
|
|
|
use Swis\Melvin\Enums\RestrictionType; |
9
|
|
|
use Swis\Melvin\Enums\RoadManagementType; |
10
|
|
|
use Swis\Melvin\Enums\TransportMode; |
11
|
|
|
use Swis\Melvin\Enums\VehicleType; |
12
|
|
|
use Swis\Melvin\Models\LaneInformation; |
13
|
|
|
use Swis\Melvin\Models\Restriction; |
14
|
|
|
use Swis\Melvin\Models\RestrictionExtraInfo; |
15
|
|
|
use Swis\Melvin\Models\VehicleInformation; |
16
|
|
|
|
17
|
|
|
class RestrictionParser |
18
|
|
|
{ |
19
|
6 |
|
public function __construct(protected GeometryParser $geometryParser) |
20
|
|
|
{ |
21
|
6 |
|
} |
22
|
|
|
|
23
|
6 |
|
public function parse(\stdClass $object, int $index): Restriction |
24
|
|
|
{ |
25
|
6 |
|
if ($emergencyServices = $object->properties->emergencyServices ?? $object->properties->emergency ?? null) { |
26
|
6 |
|
$emergencyServices = $this->parseExtraInfo($emergencyServices); |
27
|
|
|
} |
28
|
|
|
|
29
|
6 |
|
if ($publicTransport = $object->properties->publicTransport ?? null) { |
30
|
6 |
|
$publicTransport = $this->parseExtraInfo($publicTransport); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
if ($laneInformation = $object->properties->laneInformation ?? null) { |
34
|
6 |
|
$laneInformation = new LaneInformation( |
35
|
6 |
|
$laneInformation->numberOfLanesRestricted, |
36
|
6 |
|
$laneInformation->numberOfOperationalLanes, |
37
|
6 |
|
$laneInformation->originalNumberOfLanes |
38
|
3 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// N.B. vehicleInformation is often an empty object when unknown |
42
|
6 |
|
if ($vehicleInformation = (array) ($object->properties->vehicleInformation ?? []) ? $object->properties->vehicleInformation : null) { |
43
|
6 |
|
$vehicleInformation = new VehicleInformation( |
44
|
6 |
|
$vehicleInformation->heightCharacteristic ?? null, |
45
|
6 |
|
$vehicleInformation->lengthCharacteristic ?? null, |
46
|
6 |
|
$vehicleInformation->widthCharacteristic ?? null, |
47
|
6 |
|
$vehicleInformation->grossWeightCharacteristic ?? null, |
48
|
3 |
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
return new Restriction( |
52
|
6 |
|
$object->id, |
53
|
6 |
|
str_contains($object->properties->type, '_EXTERNAL'), |
54
|
6 |
|
$object->geometry ? $this->geometryParser->parse($object->geometry) : null, |
55
|
6 |
|
($object->properties->name ?? '') ?: sprintf('Beperking %d', $index + 1), |
56
|
6 |
|
Direction::from($object->properties->direction), |
57
|
6 |
|
!empty($object->properties->transportMode) ? TransportMode::from($object->properties->transportMode) : null, |
58
|
6 |
|
RestrictionType::from($object->properties->restrictionType), |
59
|
6 |
|
array_map([VehicleType::class, 'from'], $object->properties->vehicleTypes ?? $object->properties->vehicles ?? []), |
60
|
3 |
|
$emergencyServices, |
61
|
3 |
|
$publicTransport, |
62
|
6 |
|
$object->properties->maximumSpeed ?? null, |
63
|
3 |
|
$laneInformation, |
64
|
3 |
|
$vehicleInformation, |
65
|
6 |
|
property_exists($object->properties, 'roadManagementTypes') |
66
|
6 |
|
? array_map([RoadManagementType::class, 'from'], $object->properties->roadManagementTypes) |
67
|
6 |
|
: null, |
68
|
6 |
|
($object->properties->roadManagementType ?? null) ? RoadManagementType::from($object->properties->roadManagementType) : null |
69
|
3 |
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
protected function parseExtraInfo(\stdClass $object): RestrictionExtraInfo |
73
|
|
|
{ |
74
|
6 |
|
$passageAllowed = $object->passageAllowed ?? null; |
75
|
|
|
|
76
|
6 |
|
if ($passageAllowed === 'true' || $passageAllowed === 'YES') { |
77
|
6 |
|
$passageAllowed = true; |
78
|
6 |
|
} elseif ($passageAllowed === 'false' || $passageAllowed === 'NO') { |
79
|
6 |
|
$passageAllowed = false; |
80
|
|
|
} else { |
81
|
6 |
|
$passageAllowed = null; |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
return new RestrictionExtraInfo( |
85
|
6 |
|
$passageAllowed, |
86
|
6 |
|
$object->description ?? '' |
87
|
3 |
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|