@@ -33,48 +33,64 @@ class Vin implements VinInterface
3333{
3434
3535 /**
36- * Regular expression for a VIN parsing (ISO 3779)
36+ * Regular expression for a VIN parsing/validation (ISO 3779)
3737 *
3838 * @var string
3939 */
4040 public const REGEX = '/^(?<wmi>[0-9A-HJ-NPR-Z]{3})(?<vds>[0-9A-HJ-NPR-Z]{6})(?<vis>[0-9A-HJ-NPR-Z]{8})$/ ' ;
4141
4242 /**
43+ * The VIN code
44+ *
4345 * @var string
4446 */
4547 private $ vin ;
4648
4749 /**
50+ * World manufacturer identifier
51+ *
4852 * @var string
4953 */
5054 private $ wmi ;
5155
5256 /**
57+ * Vehicle descriptor section
58+ *
5359 * @var string
5460 */
5561 private $ vds ;
5662
5763 /**
64+ * Vehicle identifier section
65+ *
5866 * @var string
5967 */
6068 private $ vis ;
6169
6270 /**
71+ * Vehicle region
72+ *
6373 * @var null|string
6474 */
6575 private $ region ;
6676
6777 /**
78+ * Vehicle country
79+ *
6880 * @var null|string
6981 */
7082 private $ country ;
7183
7284 /**
85+ * Vehicle manufacturer
86+ *
7387 * @var null|string
7488 */
7589 private $ manufacturer ;
7690
7791 /**
92+ * Vehicle model year
93+ *
7894 * @var int[]
7995 */
8096 private $ modelYear ;
@@ -91,7 +107,7 @@ public function __construct(string $value)
91107 // The given VIN must be in upper case...
92108 $ value = strtoupper ($ value );
93109
94- if (! preg_match (self ::REGEX , $ value , $ match )) {
110+ if (!preg_match (self ::REGEX , $ value , $ match )) {
95111 throw new InvalidArgumentException (
96112 sprintf ('The value "%s" is not a valid VIN ' , $ value )
97113 );
@@ -104,10 +120,10 @@ public function __construct(string $value)
104120 $ this ->vis = $ match ['vis ' ];
105121
106122 // Parsed values
107- $ this ->region = $ this ->identifyRegion ();
108- $ this ->country = $ this ->identifyCountry ();
109- $ this ->manufacturer = $ this ->identifyManufacturer ();
110- $ this ->modelYear = $ this ->identifyModelYear ();
123+ $ this ->region = $ this ->determineRegion ();
124+ $ this ->country = $ this ->determineCountry ();
125+ $ this ->manufacturer = $ this ->determineManufacturer ();
126+ $ this ->modelYear = $ this ->determineModelYear ();
111127 }
112128
113129 /**
@@ -175,7 +191,9 @@ public function getModelYear() : array
175191 }
176192
177193 /**
178- * {@inheritDoc}
194+ * Converts the object to array
195+ *
196+ * @return array
179197 */
180198 public function toArray () : array
181199 {
@@ -192,63 +210,66 @@ public function toArray() : array
192210 }
193211
194212 /**
213+ * Tries to determine vehicle region
214+ *
195215 * @return null|string
196216 */
197- private function identifyRegion () : ?string
217+ private function determineRegion () : ?string
198218 {
199- // undefined region...
200- if (! isset (REGIONS [$ this ->wmi [0 ]])) {
201- return null ;
202- }
203-
204219 return REGIONS [$ this ->wmi [0 ]]['region ' ] ?? null ;
205220 }
206221
207222 /**
223+ * Tries to determine vehicle country
224+ *
208225 * @return null|string
209226 */
210- private function identifyCountry () : ?string
227+ private function determineCountry () : ?string
211228 {
212- // undefined region...
213- if (! isset ( REGIONS [ $ this -> wmi [ 0 ]]) ) {
229+ $ countries = REGIONS [ $ this -> wmi [ 0 ]][ ' countries ' ] ?? null ;
230+ if (null === $ countries ) {
214231 return null ;
215232 }
216233
217- foreach (REGIONS [ $ this -> wmi [ 0 ]][ ' countries ' ] as $ chars => $ title ) {
218- if (! (false === strpbrk ($ this ->wmi [1 ], (string ) $ chars ))) {
219- return $ title ;
234+ foreach ($ countries as $ chars => $ name ) {
235+ if (!(false === strpbrk ($ this ->wmi [1 ], (string ) $ chars ))) {
236+ return $ name ;
220237 }
221238 }
222239
223240 return null ;
224241 }
225242
226243 /**
244+ * Tries to determine vehicle manufacturer
245+ *
227246 * @return null|string
228247 */
229- private function identifyManufacturer () : ?string
248+ private function determineManufacturer () : ?string
230249 {
231250 return MANUFACTURERS [$ this ->wmi ] ?? MANUFACTURERS [$ this ->wmi [0 ] . $ this ->wmi [1 ]] ?? null ;
232251 }
233252
234253 /**
254+ * Tries to determine vehicle model year(s)
255+ *
235256 * @return int[]
236257 */
237- private function identifyModelYear () : array
258+ private function determineModelYear () : array
238259 {
239260 $ comingYear = (int ) date ('Y ' ) + 1 ;
240- $ certainYears = [];
261+ $ estimatedYears = [];
241262
242263 foreach (YEARS as $ year => $ char ) {
243264 if ($ this ->vis [0 ] === $ char ) {
244- $ certainYears [] = $ year ;
265+ $ estimatedYears [] = $ year ;
245266 }
246267
247268 if ($ comingYear === $ year ) {
248269 break ;
249270 }
250271 }
251272
252- return $ certainYears ;
273+ return $ estimatedYears ;
253274 }
254275}
0 commit comments