Skip to content

Commit 6ef610c

Browse files
authored
Merge pull request #9 from amattu2/feat/additional-endpoints
feat: Support `GetModelsForMakeYear` and `GetModelsForMake`
2 parents 6bf35c0 + f526948 commit 6ef610c

5 files changed

Lines changed: 265 additions & 9 deletions

File tree

README.md

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $decode = amattu2\NHTSA\Client::decodeVIN("VIN_NUMBER");
4343

4444
Success return result
4545

46-
```
46+
```txt
4747
Array
4848
(
4949
[Error Text] =>
@@ -125,7 +125,7 @@ $recalls = amattu2\NHTSA\Client::getRecalls(2015, "Ford", "Mustang");
125125

126126
Success return result
127127

128-
```
128+
```txt
129129
Array
130130
(
131131
[0] => Array
@@ -171,7 +171,7 @@ $pretty_recalls = amattu2\NHTSA\Client::parseRecalls($recalls);
171171

172172
Success return result
173173

174-
```
174+
```txt
175175
Array
176176
(
177177
[2] => Array
@@ -191,6 +191,146 @@ Array
191191
)
192192
```
193193

194+
## getModelsForMakeByYear
195+
196+
PHPDoc
197+
198+
```PHP
199+
/**
200+
* Get all available models for a make by year
201+
*
202+
* @note This is a free-text match. Recommend using `getModelsForMakeIdByYear`
203+
* @param int $year The model year
204+
* @param string $make The make
205+
*/
206+
```
207+
208+
Usage
209+
210+
```PHP
211+
$models = amattu2\NHTSA\Client::getModelsForMakeByYear(2015, "Ford");
212+
```
213+
214+
Success return result
215+
216+
```txt
217+
Array
218+
(
219+
[0] => Array
220+
(
221+
[Make_ID] => 441
222+
[Make_Name] => FORD
223+
[Model_ID] => 2021
224+
[Model_Name] => C-MAX
225+
)
226+
...
227+
)
228+
```
229+
230+
## getModelsForMakeIdByYear
231+
232+
PHPDoc
233+
234+
```PHP
235+
/**
236+
* Get all available models for a make by year
237+
*
238+
* @param int $year The model year
239+
* @param int $make_id The make ID
240+
*/
241+
```
242+
243+
Usage
244+
245+
```PHP
246+
$models = amattu2\NHTSA\Client::getModelsForMakeIdByYear(2015, 441);
247+
```
248+
249+
Success return result
250+
251+
```txt
252+
Array
253+
(
254+
[0] => Array
255+
(
256+
[Make_ID] => 441
257+
[Make_Name] => FORD
258+
[Model_ID] => 2021
259+
[Model_Name] => C-MAX
260+
)
261+
...
262+
)
263+
```
264+
265+
## getModelsForMake
266+
267+
PHPDoc
268+
269+
```PHP
270+
/**
271+
* Get all available models for a make
272+
*
273+
* @param string $make The make
274+
*/
275+
```
276+
277+
Usage
278+
279+
```PHP
280+
$models = amattu2\NHTSA\Client::getModelsForMake("Ford");
281+
```
282+
283+
Success return result
284+
285+
```txt
286+
Array
287+
(
288+
[0] => Array
289+
(
290+
[Make_ID] => 441
291+
[Make_Name] => FORD
292+
[Model_ID] => 2021
293+
[Model_Name] => C-MAX
294+
)
295+
...
296+
)
297+
```
298+
299+
## getModelsForMakeId
300+
301+
PHPDoc
302+
303+
```PHP
304+
/**
305+
* Get all available models for a make
306+
*
307+
* @param int $make_id The make ID
308+
*/
309+
```
310+
311+
Usage
312+
313+
```PHP
314+
$models = amattu2\NHTSA\Client::getModelsForMakeId(441);
315+
```
316+
317+
Success return result
318+
319+
```txt
320+
Array
321+
(
322+
[0] => Array
323+
(
324+
[Make_ID] => 441
325+
[Make_Name] => FORD
326+
[Model_ID] => 2021
327+
[Model_Name] => C-MAX
328+
)
329+
...
330+
)
331+
```
332+
194333
# Requirements & Dependencies
195334

196-
PHP 7.0 +
335+
- PHP 7.4+
336+
- cURL Extension

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
"email": "alecmattu1234@gmail.com"
1515
}
1616
],
17-
"require": {}
17+
"require": {
18+
"php": ">=7.4",
19+
"ext-curl": "*"
20+
}
1821
}

composer.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23-
2423
// Files
2524
require "vendor/autoload.php";
2625

2726
// Optional VIN $_GET input
28-
$vin = isset($_GET['vin']) && !empty($_GET['vin']) ? $_GET['vin'] : "2B3KA43R86H389824";
27+
// $vin = isset($_GET['vin']) && !empty($_GET['vin']) ? $_GET['vin'] : "2B3KA43R86H389824";
2928

3029
// Perform raw decode
3130
// echo "<h1>Perform raw decode</h1>", "<pre>";
@@ -48,3 +47,27 @@
4847
// echo "<h1>Pretty parse raw recall request</h1>", "<pre>";
4948
// print_r(amattu2\NHTSA\Client::parseRecalls($recalls));
5049
// echo "</pre>";
50+
51+
// getModelsForMakeByYear
52+
53+
// echo "<h1>Get models for make by year</h1>", "<pre>";
54+
// print_r(amattu2\NHTSA\Client::getModelsForMakeByYear(2015, "Ford));
55+
// echo "</pre>";
56+
57+
// getModelsForMakeIdByYear
58+
59+
// echo "<h1>Get models for make by year</h1>", "<pre>";
60+
// print_r(amattu2\NHTSA\Client::getModelsForMakeIdByYear(2015, 460));
61+
// echo "</pre>";
62+
63+
// getModelsForMake
64+
65+
// echo "<h1>Get models for make</h1>", "<pre>";
66+
// print_r(amattu2\NHTSA\Client::getModelsForMake(make: "Tesla"));
67+
// echo "</pre>";
68+
69+
// getModelsForMakeId
70+
71+
// echo "<h1>Get models for make ID</h1>", "<pre>";
72+
// print_r(amattu2\NHTSA\Client::getModelsForMakeId(441));
73+
// echo "</pre>";

src/Client.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ class Client
3333
private static $endpoints = [
3434
"decode" => "https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/%s?format=json",
3535
"recalls" => "https://api.nhtsa.gov/recalls/recallsByVehicle?modelYear=%d&make=%s&model=%s",
36+
"GetModelsForMakeYear" => "https://vpic.nhtsa.dot.gov/api/vehicles/getmodelsformakeyear/make/%s/modelyear/%d?format=json",
37+
"GetModelsForMakeIdYear" => "https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/%d/modelyear/%d?format=json",
38+
"GetModelsForMake" => "https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMake/%s?format=json",
39+
"GetModelsForMakeId" => "https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeId/%d?format=json",
3640
];
41+
3742
private static $minimum_year = 1950;
3843
private static $minimum_make_length = 3;
3944
private static $minimum_model_length = 3;
@@ -228,6 +233,88 @@ public static function parseRecalls(array $recalls = []): ?array
228233
return $parsed_result;
229234
}
230235

236+
/**
237+
* Get all available models for a make by year
238+
*
239+
* @note This is a free-text match. Recommend using `getModelsForMakeIdByYear`
240+
* @param int $year The model year
241+
* @param string $make The make
242+
*/
243+
public static function getModelsForMakeByYear(int $year, string $make): ?array
244+
{
245+
if (!$year || $year < self::$minimum_year) {
246+
return null;
247+
}
248+
if (!$make || strlen($make) < self::$minimum_make_length) {
249+
return null;
250+
}
251+
252+
// Fetch Data
253+
$endpoint = sprintf(self::$endpoints["GetModelsForMakeYear"], rawurlencode(strtoupper($make)), $year);
254+
$result = json_decode(self::http_get($endpoint), true);
255+
256+
return $result && isset($result["Count"]) && $result["Count"] > 0 ? $result["Results"] : null;
257+
}
258+
259+
/**
260+
* Get all available models for a make Id by year
261+
*
262+
* @param int $year The model year
263+
* @param int $make_id The make id
264+
*/
265+
public static function getModelsForMakeIdByYear(int $year, int $make_id): ?array
266+
{
267+
if (!$make_id) {
268+
return null;
269+
}
270+
if (!$year || $year < self::$minimum_year) {
271+
return null;
272+
}
273+
274+
// Fetch Data
275+
$endpoint = sprintf(self::$endpoints["GetModelsForMakeIdYear"], $make_id, $year);
276+
$result = json_decode(self::http_get($endpoint), true);
277+
278+
return $result && isset($result["Count"]) && $result["Count"] > 0 ? $result["Results"] : null;
279+
}
280+
281+
/**
282+
* Get all available models for a make
283+
*
284+
* @note This is a free-text match. Recommend using `getModelsForMakeId`
285+
* @param string $make
286+
*/
287+
public static function GetModelsForMake(string $make): ?array
288+
{
289+
if (!$make || strlen($make) < self::$minimum_make_length) {
290+
return null;
291+
}
292+
293+
// Fetch Data
294+
$endpoint = sprintf(self::$endpoints["GetModelsForMake"], rawurlencode(strtoupper($make)));
295+
$result = json_decode(self::http_get($endpoint), true);
296+
297+
return $result && isset($result["Count"]) && $result["Count"] > 0 ? $result["Results"] : null;
298+
}
299+
300+
/**
301+
* Get all available models for a make Id
302+
*
303+
* @param int $make_id The make id
304+
*/
305+
public static function GetModelsForMakeId(int $make_id): ?array
306+
{
307+
if (!$make_id) {
308+
return null;
309+
}
310+
311+
// Fetch Data
312+
$endpoint = sprintf(self::$endpoints["GetModelsForMakeId"], $make_id);
313+
$result = json_decode(self::http_get($endpoint), true);
314+
315+
return $result && isset($result["Count"]) && $result["Count"] > 0 ? $result["Results"] : null;
316+
}
317+
231318
/**
232319
* Parse Unix Timestamp (Miliseconds with offset)
233320
*

0 commit comments

Comments
 (0)