-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search endpoints Add CORS support Support only PHP 7.4 or newer Update example to use search endpoints
- Loading branch information
1 parent
0ecc4ef
commit 400b682
Showing
15 changed files
with
1,282 additions
and
672 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
|
||
use JsonMachine\JsonMachine; | ||
use Exception; | ||
use function iconv; | ||
use function mb_stripos; | ||
use function preg_replace; | ||
|
||
/** | ||
* This class handles location data. | ||
|
@@ -15,26 +18,21 @@ | |
class Location | ||
{ | ||
/** | ||
* Location constructor. | ||
* @param $filename | ||
* @var string $file | ||
*/ | ||
public function __construct($filename) | ||
{ | ||
/** @var array $this ->data */ | ||
$this->file = $filename; | ||
} | ||
public static string $file = 'data/geo.json'; | ||
|
||
/** | ||
* Return the json encoded list of all countries. | ||
* | ||
* @author Shivam Mathur <[email protected]> | ||
* | ||
* @return string | ||
* @param string|NULL $search | ||
* @return false|string | ||
*/ | ||
public function getAllCountries() | ||
public function getCountries(string $search = NULL) | ||
{ | ||
try { | ||
$allCountries = $this->countries(); | ||
$allCountries = $this->countries($search); | ||
} catch (Exception $exception) { | ||
return json_encode(["error" => "true", "message" => $exception->getMessage()]); | ||
} | ||
|
@@ -46,16 +44,16 @@ public function getAllCountries() | |
* Return the json encoded list of all cities in a country. | ||
* | ||
* @author Shivam Mathur <[email protected]> | ||
* | ||
* @param $countryName | ||
* @return string | ||
* @param string $countryName | ||
* @param string|NULL $search | ||
* @return false|string | ||
*/ | ||
public function getAllCities($countryName) | ||
public function getCities(string $countryName, string $search = NULL) | ||
{ | ||
$countryName = trim(stripslashes(ucwords($countryName))); | ||
|
||
try { | ||
$allCities = $this->cities($countryName); | ||
$allCities = $this->cities($countryName, $search); | ||
} catch (Exception $exception) { | ||
return json_encode(["error" => "true", "message" => $exception->getMessage()]); | ||
} | ||
|
@@ -67,19 +65,36 @@ public function getAllCities($countryName) | |
* Return the array containing all countries. | ||
* | ||
* @author Shivam Mathur <[email protected]> | ||
* @param string $haystack | ||
* @param string $needle | ||
* @return bool|false|int | ||
*/ | ||
public function mb_search(string $haystack, string $needle) | ||
{ | ||
$haystack = preg_replace('/[\'^`~\"]/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $haystack)); | ||
$needle = preg_replace('/[\'^`~\"]/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $needle)); | ||
return mb_stripos($haystack, $needle); | ||
} | ||
|
||
/** | ||
* Return the array containing all countries. | ||
* | ||
* @author Shivam Mathur <[email protected]> | ||
* @param string|NULL $search | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
private function countries() | ||
private function countries(string $search = NULL) | ||
{ | ||
if (!$this->file) { | ||
if (self::$file == '') { | ||
throw new Exception('Invalid data filename'); | ||
} | ||
|
||
$data = JsonMachine::fromFile($this->file); | ||
$countries = array_keys(iterator_to_array($data, true)); | ||
|
||
$data = JsonMachine::fromFile(self::$file); | ||
$countries = array_filter( | ||
array_keys(iterator_to_array($data, true)), | ||
fn (string $country) => !$search || $this->mb_search($country, $search) !== false | ||
); | ||
sort($countries); | ||
return $countries; | ||
} | ||
|
@@ -88,25 +103,29 @@ private function countries() | |
* Returns the array containing all cities in a country. | ||
* | ||
* @author Shivam Mathur <[email protected]> | ||
* | ||
* @param $countryName | ||
* @param string $countryName | ||
* @param string|NULL $search | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
private function cities($countryName) | ||
private function cities(string $countryName, string $search = NULL) | ||
{ | ||
if (!$this->file) { | ||
if (self::$file == '') { | ||
throw new Exception('Invalid data filename'); | ||
} | ||
|
||
$data = JsonMachine::fromFile($this->file); | ||
$data = JsonMachine::fromFile(self::$file); | ||
|
||
$cities = []; | ||
$found = false; | ||
foreach ($data as $key => $value) { | ||
if($countryName == ucwords($key)) { | ||
$found = true; | ||
$cities = $value; | ||
$cities = array_filter( | ||
$value, | ||
fn (string $city) => !$search || $this->mb_search($city, $search) !== false | ||
); | ||
sort($cities); | ||
break; | ||
} | ||
} | ||
|
@@ -115,8 +134,6 @@ private function cities($countryName) | |
throw new Exception('Invalid country name: '. $countryName); | ||
} | ||
|
||
sort($cities); | ||
|
||
return $cities; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.