@@ -80,32 +80,17 @@ public function getData()
80
80
$ html = (string ) $ this ->response ->getBody ();
81
81
82
82
$ crawler = new Crawler ($ html );
83
- $ data = [];
83
+ $ rawVehicleData = [];
84
84
85
85
$ nodeValues = $ crawler ->filter ('table > tbody > tr ' )->each ( function ( $ node , $ i ) {
86
86
// Remove double spaces, and newline(s)
87
87
$ formData = trim (preg_replace ('/\s+/ ' , ' ' , $ node ->text ()));
88
88
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
+
107
91
});
108
- // Strip multi-dimensional array into simple associative array.
92
+
93
+ // Strip multi-dimensional data array into simple associative array.
109
94
$ vehicleData = [];
110
95
foreach ($ nodeValues as $ index => $ value ) {
111
96
foreach ($ value as $ key => $ carData ) {
@@ -115,5 +100,40 @@ public function getData()
115
100
116
101
return $ vehicleData ;
117
102
}
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
+ }
118
138
}
119
139
0 commit comments