Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| TransportHalt | |
0.00% |
0 / 26 |
|
0.00% |
0 / 10 |
156 | |
0.00% |
0 / 1 |
| fromTransportApi | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| parseFromTransportApi | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getFieldValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| fromFieldValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| populateFromFieldValue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getStationId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStationName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTimeSeconds | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTimeString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| equals | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Apps\Oev\Utils; |
| 4 | |
| 5 | use Olz\Utils\WithUtilsTrait; |
| 6 | |
| 7 | class 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 | } |