1 | <?php |
||
2 | |||
3 | namespace kalanis\simple_vin; |
||
4 | |||
5 | |||
6 | use kalanis\simple_vin\Support\Manufacturer; |
||
7 | use Psr\Clock\ClockInterface; |
||
8 | |||
9 | |||
10 | /** |
||
11 | * Extended class for accessing VIN data |
||
12 | */ |
||
13 | class DeeperVin |
||
14 | { |
||
15 | protected readonly SimpleVin $simple; |
||
16 | protected readonly Datasources\WorldManufacturerClasses $worldManufacturerIdentifiers; |
||
17 | |||
18 | 7 | public function __construct( |
|
19 | ?ClockInterface $clock = null, |
||
20 | ) |
||
21 | { |
||
22 | // no DI |
||
23 | 7 | $this->simple = new SimpleVin($clock); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
24 | 7 | $this->worldManufacturerIdentifiers = new Datasources\WorldManufacturerClasses(); |
|
0 ignored issues
–
show
|
|||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param string $vinOrWmi |
||
29 | * @return Manufacturer[] |
||
30 | */ |
||
31 | 7 | public function getPossibleManufacturers(string $vinOrWmi): array |
|
32 | { |
||
33 | 7 | if (!$this->simple->isValid($vinOrWmi)) { |
|
34 | 1 | return []; |
|
35 | } |
||
36 | |||
37 | 6 | return $this->filterByYears( |
|
38 | 6 | $this->simple->getModelYear($vinOrWmi), |
|
39 | 6 | $this->getByCode($vinOrWmi) |
|
40 | 6 | ); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $code |
||
45 | * @return Manufacturer[] |
||
46 | */ |
||
47 | 6 | protected function getByCode(string $code): array |
|
48 | { |
||
49 | 6 | $used = []; |
|
50 | 6 | foreach ($this->worldManufacturerIdentifiers as $item) { |
|
51 | 6 | if (is_object($item) && is_a($item, Manufacturer::class) && str_starts_with($code, $item->code)) { |
|
52 | 6 | $used[] = $item; |
|
53 | } |
||
54 | } |
||
55 | 6 | return $used; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Years matters only for americans |
||
60 | * @param int|null $year |
||
61 | * @param Manufacturer[] $availableManufacturers |
||
62 | * @return Manufacturer[] |
||
63 | */ |
||
64 | 6 | protected function filterByYears(?int $year, array $availableManufacturers): array |
|
65 | { |
||
66 | 6 | if (is_null($year)) { |
|
67 | 2 | return $this->filterLongest($availableManufacturers); |
|
68 | } |
||
69 | |||
70 | // years from |
||
71 | 4 | $passedFrom = []; |
|
72 | 4 | foreach ($availableManufacturers as $availableManufacturer) { |
|
73 | 4 | if (!$availableManufacturer->canCompareDates) { |
|
74 | 3 | $passedFrom[] = $availableManufacturer; |
|
75 | 3 | continue; |
|
76 | } |
||
77 | 2 | if (is_null($availableManufacturer->from) || ($availableManufacturer->from <= $year)) { |
|
78 | 2 | $passedFrom[] = $availableManufacturer; |
|
79 | } |
||
80 | } |
||
81 | |||
82 | // years to |
||
83 | 4 | $passedTo = []; |
|
84 | 4 | foreach ($passedFrom as $availableManufacturer) { |
|
85 | 4 | if (!$availableManufacturer->canCompareDates) { |
|
86 | 3 | $passedTo[] = $availableManufacturer; |
|
87 | 3 | continue; |
|
88 | } |
||
89 | 2 | if (is_null($availableManufacturer->to) || ($availableManufacturer->to >= $year)) { |
|
90 | 1 | $passedTo[] = $availableManufacturer; |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 4 | return $this->filterLongest($passedTo); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param Manufacturer[] $entries |
||
99 | * @return Manufacturer[] |
||
100 | */ |
||
101 | 6 | protected function filterLongest(array $entries): array |
|
102 | { |
||
103 | 6 | $longestCodeLen = 0; |
|
104 | 6 | foreach ($entries as $item) { |
|
105 | 5 | $longestCodeLen = max($longestCodeLen, strlen($item->code)); |
|
106 | } |
||
107 | 6 | $onlyLongest = []; |
|
108 | 6 | foreach ($entries as $item) { |
|
109 | 5 | if (strlen($item->code) == $longestCodeLen) { |
|
110 | 5 | $onlyLongest[] = $item; |
|
111 | } |
||
112 | } |
||
113 | |||
114 | 6 | return $onlyLongest; |
|
115 | } |
||
116 | } |
||
117 |