Lines
78.94%
15 / 19
Functions and Methods
75.00%
3 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Coordinates | 78.94% | 15 / 19 | 75.00% | 3 / 4 | 12.13 | 0.00% | 0 / 1 | |||
| getLatLon | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| zip2LatLon | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| loadData | 69.23% | 9 / 13 | 0.00% | 0 / 1 | 8.43 | |||||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsdldb\Plz; | |
| 4 | ||
| 5 | /** | |
| 6 | * Translate a german zip code into wgs84 coordinates | |
| 7 | */ | |
| 8 | class Coordinates | |
| 9 | { | |
| 10 | /** | |
| 11 | * Store data | |
| 12 | * @var Array $data | |
| 13 | */ | |
| 14 | public $data = array(); | |
| 15 | ||
| 16 | /** | |
| 17 | * @param Int $plz | |
| 18 | */ | |
| 19 | public function getLatLon($plz): mixed | |
| 20 | { | |
| 21 | if (array_key_exists($plz, $this->data)) { | |
| 22 | return $this->data[$plz]; | |
| 23 | } | |
| 24 | return false; | |
| 25 | } | |
| 26 | ||
| 27 | public static function zip2LatLon(mixed $plz): mixed | |
| 28 | { | |
| 29 | $coordinates = new self(); | |
| 30 | return $coordinates->getLatLon($plz); | |
| 31 | } | |
| 32 | ||
| 33 | /** | |
| 34 | * @param null|string $file | |
| 35 | * | |
| 36 | * @return void | |
| 37 | */ | |
| 38 | public function loadData(string|null $file = null) | |
| 39 | { | |
| 40 | if (null === $file) { | |
| 41 | $file = __DIR__ . DIRECTORY_SEPARATOR . 'plz_geodb.json'; | |
| 42 | } | |
| 43 | if (!is_readable($file) || !is_file($file)) { | |
| 44 | throw new \Exception("Cannot read file $file"); | |
| 45 | } | |
| 46 | $json = file_get_contents($file); | |
| 47 | if ($json === false) { | |
| 48 | throw new \Exception("Cannot read file $file"); | |
| 49 | } | |
| 50 | try { | |
| 51 | $decoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR); | |
| 52 | } catch (\JsonException $exception) { | |
| 53 | throw new \Exception("Cannot parse file $file", 0, $exception); | |
| 54 | } | |
| 55 | if (!is_array($decoded)) { | |
| 56 | throw new \Exception("Invalid coordinate data in $file"); | |
| 57 | } | |
| 58 | $this->data = $decoded; | |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * @param String $file (optional) Path to json file with data | |
| 63 | * | |
| 64 | */ | |
| 65 | public function __construct($file = null) | |
| 66 | { | |
| 67 | $this->loadData($file); | |
| 68 | } | |
| 69 | } |