diff --git a/app/Paper/Traficar.php b/app/Paper/Traficar.php index 2b145fa..ccd0fb3 100644 --- a/app/Paper/Traficar.php +++ b/app/Paper/Traficar.php @@ -14,6 +14,7 @@ class Traficar private $homeCoords = ['lat' => 54.417475, 'lng' => 18.481913]; + private $traficarJson; //dystans w kilometrach w którym auta brane są pod uwagę const CLOSEST_DISTANCE = 2; @@ -52,15 +53,46 @@ class Traficar 'Numer boczny: ' . $info['car']['orderNumber'] . "\n" . 'Rejestracja: ' . $info['car']['regNumber'] . "\n" . 'Lokalizacja: ' . $info['car']['location'] . "\n" . - 'Odległość: ' . $info['distance'] . "km\n"; + 'Odległość: ' . $info['distance'] . "km\n\n" . + 'Traficarów w promieniu ' . self::CLOSEST_DISTANCE . 'km: ' . $this->getTotalCarsInArea() . "\n"; } return $formatedInfo; } + /** + * pobiera informacje na temat aut traficatowych i zapisuje do pamięci + * @return mixed + */ + private function getTraficarJson() + { + if (is_null($this->traficarJson)) { + $this->traficarJson = json_decode(file_get_contents('https://api.traficar.pl/eaw-rest-api/car?shapeId=5'), 1); + } + return $this->traficarJson; + + } + + + /** + * zwraca ilość aut w podanym promieniu + * @return int + */ + private function getTotalCarsInArea() + { + $trafcarData = $this->getTraficarJson(); + $numberOfCarsInArea = 0; + foreach ($trafcarData['cars'] as $car) { + $distance = $this->distance($this->homeCoords['lat'], $this->homeCoords['lng'], $car['latitude'], $car['longitude']); + if ($distance < self::CLOSEST_DISTANCE) { + $numberOfCarsInArea++; + } + } + return $numberOfCarsInArea; + } public function nearestCarInfo() { - $trafcarData = json_decode(file_get_contents('https://api.traficar.pl/eaw-rest-api/car?shapeId=5'), 1); + $trafcarData = $this->getTraficarJson(); $closestCar = []; $closestDistance = 1000; foreach ($trafcarData['cars'] as $car) {