Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.08% covered (warning)
73.08%
19 / 26
80.00% covered (warning)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransportHalt
73.08% covered (warning)
73.08%
19 / 26
80.00% covered (warning)
80.00%
8 / 10
14.81
0.00% covered (danger)
0.00%
0 / 1
 fromTransportApi
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 parseFromTransportApi
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getFieldValue
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 fromFieldValue
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 populateFromFieldValue
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getStationId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStationName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTimeSeconds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTimeString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 equals
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Olz\Apps\Oev\Utils;
4
5use Olz\Utils\WithUtilsTrait;
6
7class TransportHalt {
8    use WithUtilsTrait;
9
10    protected string $stationId;
11    protected string $stationName;
12    protected ?int $timeSeconds;
13    protected string $timeString;
14
15    /** @param array<string, mixed> $api_halt */
16    public static function fromTransportApi(array $api_halt): self {
17        $halt = new self();
18        $halt->parseFromTransportApi($api_halt);
19        return $halt;
20    }
21
22    /** @param array<string, mixed> $api_halt */
23    protected function parseFromTransportApi(array $api_halt): void {
24        date_default_timezone_set('Europe/Zurich');
25        $this->stationId = $api_halt['station']['id'];
26        $this->stationName = $api_halt['station']['name'];
27        $this->timeSeconds = $api_halt['departureTimestamp'] ?? $api_halt['arrivalTimestamp'] ?? null;
28    }
29
30    /** @return array<string, mixed> */
31    public function getFieldValue(): array {
32        return [
33            'stationId' => $this->stationId,
34            'stationName' => $this->stationName,
35            'time' => $this->getTimeString(),
36        ];
37    }
38
39    /** @param array<string, mixed> $value */
40    public static function fromFieldValue(array $value): self {
41        $instance = new self();
42        $instance->populateFromFieldValue($value);
43        return $instance;
44    }
45
46    /** @param array<string, mixed> $value */
47    protected function populateFromFieldValue(array $value): void {
48        date_default_timezone_set('Europe/Zurich');
49        $this->stationId = $value['stationId'];
50        $this->stationName = $value['stationName'];
51        $this->timeSeconds = strtotime($value['time']);
52    }
53
54    public function getStationId(): string {
55        return $this->stationId;
56    }
57
58    public function getStationName(): string {
59        return $this->stationName;
60    }
61
62    public function getTimeSeconds(): ?int {
63        return $this->timeSeconds;
64    }
65
66    public function getTimeString(): ?string {
67        return $this->timeSeconds ? date('Y-m-d H:i:s', $this->timeSeconds) : null;
68    }
69
70    public function equals(TransportHalt $other_halt): bool {
71        return
72            $this->getStationId() === $other_halt->getStationId()
73            && $this->getTimeSeconds() === $other_halt->getTimeSeconds();
74    }
75}