Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.54% covered (danger)
41.54%
27 / 65
54.55% covered (warning)
54.55%
6 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransportConnection
41.54% covered (danger)
41.54%
27 / 65
54.55% covered (warning)
54.55%
6 / 11
139.09
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%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 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 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getSections
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSections
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSuperConnectionOf
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 getFlatHalts
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDestinationHalt
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getCropped
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3namespace Olz\Apps\Oev\Utils;
4
5use Olz\Utils\WithUtilsTrait;
6
7class TransportConnection {
8    use WithUtilsTrait;
9
10    /** @var array<TransportSection> */
11    protected array $sections = [];
12
13    /** @param array<string, mixed> $api_connection */
14    public static function fromTransportApi(array $api_connection): self {
15        $connection = new self();
16        $connection->parseFromTransportApi($api_connection);
17        return $connection;
18    }
19
20    /** @param array<string, mixed> $api_connection */
21    protected function parseFromTransportApi(array $api_connection): void {
22        $this->sections = [];
23        $api_sections = $api_connection['sections'] ?? [];
24        foreach ($api_sections as $api_section) {
25            $section = TransportSection::fromTransportApi($api_section);
26            $this->sections[] = $section;
27        }
28    }
29
30    /** @return array<string, mixed> */
31    public function getFieldValue() {
32        return [
33            'sections' => array_map(function ($section) {
34                return $section->getFieldValue();
35            }, $this->sections),
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        $this->sections = array_map(function ($section) {
49            return TransportSection::fromFieldValue($section);
50        }, $value['sections']);
51    }
52
53    /** @return array<TransportSection> */
54    public function getSections(): array {
55        return $this->sections;
56    }
57
58    /** @param array<TransportSection> $new_sections */
59    public function setSections(array $new_sections): void {
60        $this->sections = $new_sections;
61    }
62
63    public function isSuperConnectionOf(TransportConnection $sub_connection): bool {
64        $super_halts = $this->getFlatHalts();
65        $sub_halts = $sub_connection->getFlatHalts();
66        $sub_halt_index = 0;
67        foreach ($super_halts as $super_halt) {
68            $sub_halt = $sub_halts[$sub_halt_index];
69            if ($super_halt->equals($sub_halt)) {
70                $sub_halt_index++;
71                if ($sub_halt_index >= count($sub_halts)) {
72                    return true;
73                }
74            }
75        }
76        return false;
77    }
78
79    /** @return array<TransportHalt> */
80    public function getFlatHalts(): array {
81        return array_merge(...array_map(function ($section) {
82            return $section->getHalts();
83        }, $this->sections));
84    }
85
86    public function getDestinationHalt(): TransportHalt {
87        $flat_halts = $this->getFlatHalts();
88        $last_index = count($flat_halts) - 1;
89        return $flat_halts[$last_index];
90    }
91
92    public function getCropped(?TransportHalt $start_halt, ?TransportHalt $end_halt): TransportConnection {
93        $stage = 'search_start';
94        $cropped_sections = [];
95        foreach ($this->sections as $section) {
96            if ($stage === 'search_start') {
97                $cropped_section = null;
98                $contains_start = true;
99                try {
100                    $cropped_section = $section->getCropped($start_halt, null);
101                } catch (\ValueError $th) {
102                    $contains_start = false;
103                }
104                if ($contains_start) {
105                    try {
106                        $cropped_section = $section->getCropped($start_halt, $end_halt);
107                        $cropped_sections[] = $cropped_section;
108                        if ($end_halt !== null) {
109                            $stage = 'end_found';
110                        } else {
111                            $stage = 'search_end';
112                        }
113                    } catch (\ValueError $th) {
114                        $cropped_sections[] = $cropped_section;
115                        $stage = 'search_end';
116                    }
117                }
118            } elseif ($stage === 'search_end') {
119                try {
120                    $cropped_section = $section->getCropped(null, $end_halt);
121                    $cropped_sections[] = $cropped_section;
122                    if ($end_halt !== null) {
123                        $stage = 'end_found';
124                    }
125                } catch (\ValueError $th) {
126                    // End not yet found. Add Section as a whole.
127                    $cropped_sections[] = $section;
128                }
129            }
130        }
131        $cropped_connection = new self();
132        $cropped_connection->setSections($cropped_sections);
133        return $cropped_connection;
134    }
135}