public function testFindsNoComma() { $value = 'las vegas '; $results = LocationParser::parseLocation($value); $this->assertEquals($results[0], 'las'); $this->assertEquals($results[1], 'vegas'); }
public function search(Request $request) { $location = $request->q; $zip = LocationParser::getDigits($location); $locations = null; // Zip is trump: Use it first if we have it if (!empty($zip)) { $locations = App\Location::where('zip', 'like', $zip . '%')->get(); } else { $parsedLocation = LocationParser::parseLocation($location); if (isset($parsedLocation['state'])) { $state = $parsedLocation['state']; $city = $parsedLocation['rest']; $rawLocations = App\Location::where('city', '=', $city)->where('state', 'like', $state . '%')->get(); } else { $city = implode(' ', $parsedLocation); $rawLocations = App\Location::where('city', 'LIKE', $city . '%')->get(); } $filtered = $rawLocations->filter(function ($item) { return $item['zip'] == ''; }); $locations = $filtered->isEmpty() ? $rawLocations : $filtered; } // Show the most populous first $sorted = $locations->sortByDesc('pop'); // if ($this->hasZip($location)) { // $locations = App\Location::where('zip', 'like', $location . '%') // ->orWhere('city', 'like', $location . '%') // ->groupBy(['city', 'zip']) // ->get(); // } else if ($this->hasComma($location)) { // if we have a comma, split the string on it // $locationSplit = $this->splitLocation($location); // $city = trim($locationSplit[0]); // $state = trim($locationSplit[1]); // $locations = App\Location::where('city', '=', $city) // ->where('state', 'like', $state. '%') // ->get(); // } else { // $locations = App\Location::where('zip', 'like', $location . '%') // ->orWhere('city', 'like', $location . '%') // ->groupBy(['city', 'zip']) // ->get(); // } if (!$sorted->isEmpty()) { return $this->response->withCollection($sorted, new LocationTransformer()); } $errorMeta = ['error' => ['code' => 'GEN-NOT-FOUND', 'http_code' => 404, 'message' => 'Location not found']]; return $this->response->withArray($errorMeta); }