Skip to content

Commit bbe16ed

Browse files
author
Arowosegbe Ifeoluwa
committed
Made code modifications.
1 parent debd5af commit bbe16ed

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/Mvrd.php

+40-20
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,17 @@ public function getData()
8080
$html = (string) $this->response->getBody();
8181

8282
$crawler = new Crawler($html);
83-
$data = [];
83+
$rawVehicleData = [];
8484

8585
$nodeValues = $crawler->filter('table > tbody > tr')->each( function( $node, $i) {
8686
// Remove double spaces, and newline(s)
8787
$formData = trim(preg_replace('/\s+/', ' ', $node->text()));
8888

89-
// Prepare data to be stored as key=>value pairs
90-
$string = explode(' ', $formData, 3);
91-
// Use the number of spaces in the string as a requisite to handle them.
92-
// e.g Color field(1 space) contains less spaces than Plate Number(2 spaces) e.t.c
93-
if (substr_count($formData, ' ') > 1) {
94-
// Check if the string holds Vehicle Model data as this needs to be stored differently.
95-
$string[0] == 'Model' ? $true = true : $true = false;
96-
if ($true) {
97-
$data[$string[0]] = $string[1] .' '. $string[2];
98-
}else{
99-
$string[0] = $string[0] == 'Isssue' ? 'Issue' : $string[0];
100-
$data[$string[0].$string[1]] = $string[2];
101-
}
102-
}else{
103-
$data[$string[0]] = $string[1];
104-
}
105-
106-
return $data;
89+
return $this->processData($formData);
90+
10791
});
108-
// Strip multi-dimensional array into simple associative array.
92+
93+
// Strip multi-dimensional data array into simple associative array.
10994
$vehicleData = [];
11095
foreach ($nodeValues as $index => $value) {
11196
foreach ($value as $key => $carData) {
@@ -115,5 +100,40 @@ public function getData()
115100

116101
return $vehicleData;
117102
}
103+
104+
105+
/**
106+
* Process vehicle data to make it possible to store them as key-value pairs in an array.
107+
* @param $data (string to be processed)
108+
* @return array
109+
*/
110+
public function processData($data)
111+
{
112+
// Prepare data to be stored as key=>value pairs
113+
$string = explode(' ', $data, 3);
114+
115+
// Use the number of spaces in the string as a requisite to determine how to handle them.
116+
// e.g Color field(1 space) contains less spaces than Plate Number(2 spaces) e.t.c
117+
if (substr_count($data, ' ') > 1) {
118+
119+
// Check if the string holds Vehicle Model data as this needs to be stored differently.
120+
$isModelPresent = ($string[0] == 'Model') ? true : false;
121+
122+
if ($isModelPresent) {
123+
$rawVehicleData[$string[0]] = $string[1] .' '. $string[2];
124+
}else{
125+
/* An error on the host website (lsmvaapvs.org) sees 'Issue' misspelt as 'Isssue'
126+
we try to fix this by detecting that string and correcting it before handling the data.
127+
*/
128+
$string[0] = $string[0] == 'Isssue' ? 'Issue' : $string[0];
129+
$rawVehicleData[$string[0].$string[1]] = $string[2];
130+
}
131+
132+
}else{
133+
$rawVehicleData[$string[0]] = $string[1];
134+
}
135+
136+
return $rawVehicleData;
137+
}
118138
}
119139

test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Unicodeveloper\Mvrd\Mvrd;
77

88
// Instantiate Mvrd and pass a valid plate number
9-
$obj = new Mvrd('xxxxxx');
9+
$obj = new Mvrd('xxxxxxx');
1010

1111
// Call the getData method to return an array with the following details:
1212
// plateNumber, Owner Name, Color, Model, Chasis Number, Vehicle Status, License Issue Date and Expiry Date

0 commit comments

Comments
 (0)