TalismanFR /
yii2-yandex-geocode-advanced
| 1 | <?php |
||||
| 2 | |||||
| 3 | |||||
| 4 | namespace talismanfr\geocode; |
||||
| 5 | |||||
| 6 | |||||
| 7 | use talismanfr\geocode\contracts\Collection; |
||||
| 8 | use talismanfr\geocode\contracts\Geocode; |
||||
| 9 | use talismanfr\geocode\entity\Address; |
||||
| 10 | use talismanfr\geocode\entity\AddressDetails; |
||||
| 11 | use talismanfr\geocode\entity\Geo; |
||||
| 12 | use talismanfr\geocode\entity\Point; |
||||
| 13 | use talismanfr\geocode\exeptions\FormatResponseException; |
||||
| 14 | use yii\helpers\ArrayHelper; |
||||
| 15 | |||||
| 16 | class GeocodeCollection implements Collection |
||||
| 17 | { |
||||
| 18 | private $geocode; |
||||
| 19 | |||||
| 20 | public function __construct(Geocode $geocode) |
||||
| 21 | { |
||||
| 22 | $this->geocode = $geocode; |
||||
| 23 | } |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * Get array Geo entity |
||||
| 27 | * @param $query |
||||
| 28 | * @param array $params params fro url to yandex geocode service |
||||
| 29 | * @return array |
||||
| 30 | * @throws FormatResponseException |
||||
| 31 | */ |
||||
| 32 | public function get($query, $params = []): array |
||||
| 33 | { |
||||
| 34 | return $this->find($query, $params); |
||||
| 35 | } |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @param $query |
||||
| 39 | * @param array $params |
||||
| 40 | * @return Geo |
||||
| 41 | * @throws FormatResponseException |
||||
| 42 | */ |
||||
| 43 | public function one($query,$params=[]) |
||||
| 44 | { |
||||
| 45 | $params['results']=1; |
||||
| 46 | $geos=$this->find($query,$params); |
||||
| 47 | |||||
| 48 | if(count($geos)>0){ |
||||
| 49 | return array_shift($geos); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | return null; |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | /** |
||||
| 56 | * @param string $query |
||||
| 57 | * @param array $params |
||||
| 58 | * @return array |
||||
| 59 | * @throws FormatResponseException |
||||
| 60 | */ |
||||
| 61 | private function find(string $query, $params = []): array |
||||
| 62 | { |
||||
| 63 | $geo_result = $this->geocode->get($query, $params); |
||||
| 64 | |||||
| 65 | try { |
||||
| 66 | $geo_result = json_decode((string)$geo_result, true); |
||||
| 67 | } catch (\Exception $e) { |
||||
| 68 | throw new FormatResponseException($e->getMessage()); |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | $data = ArrayHelper::getValue($geo_result, 'response.GeoObjectCollection.featureMember'); |
||||
| 72 | |||||
| 73 | if (!$data) { |
||||
| 74 | $meta=ArrayHelper::getValue($geo_result,'response.GeoObjectCollection.metaDataProperty.GeocoderResponseMetaData'); |
||||
| 75 | |||||
| 76 | if(!$meta) { |
||||
| 77 | throw new FormatResponseException('Error format response from query ' . $query); |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | } |
||||
| 81 | |||||
| 82 | $geos = []; |
||||
| 83 | foreach ($data as $datum) { |
||||
| 84 | $geos[] = $this->mapsGeo($datum); |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | return $geos; |
||||
| 88 | |||||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * @param array $data Data from response yandex geocode service |
||||
| 93 | * @return Geo |
||||
| 94 | */ |
||||
| 95 | private function mapsGeo(array $data): Geo |
||||
| 96 | { |
||||
| 97 | $data = ArrayHelper::getValue($data, 'GeoObject'); |
||||
| 98 | $name = ArrayHelper::getValue($data, 'name'); |
||||
| 99 | $description = ArrayHelper::getValue($data, 'description'); |
||||
| 100 | $meta = ArrayHelper::getValue($data, 'metaDataProperty.GeocoderMetaData'); |
||||
| 101 | |||||
| 102 | $kind = ArrayHelper::getValue($meta, 'kind'); |
||||
| 103 | $text = ArrayHelper::getValue($meta, 'text'); |
||||
| 104 | |||||
| 105 | $address = Address::fromState(ArrayHelper::getValue($meta, 'Address')); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 106 | $addressDetails = AddressDetails::fromState(ArrayHelper::getValue($meta, 'AddressDetails')); |
||||
|
0 ignored issues
–
show
It seems like
yii\helpers\ArrayHelper:...meta, 'AddressDetails') can also be of type null; however, parameter $state of talismanfr\geocode\entit...essDetails::fromState() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 107 | |||||
| 108 | $point = new Point(ArrayHelper::getValue($data, 'Point.pos')); |
||||
| 109 | |||||
| 110 | $geo = new Geo($address, $addressDetails, $point, $name, $description, $kind, $text); |
||||
| 111 | |||||
| 112 | return $geo; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | |||||
| 116 | } |