|
| 1 | +<?php namespace Toin0u\Geocoder; |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Geocoder package. |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @license MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +use Geocoder\ProviderAggregator; |
| 12 | +use Geocoder\Geocoder; |
| 13 | +use Geocoder\Dumper\Gpx; |
| 14 | +use Geocoder\Dumper\Kml; |
| 15 | +use Geocoder\Dumper\Wkb; |
| 16 | +use Geocoder\Dumper\Wkt; |
| 17 | +use Geocoder\Dumper\GeoJson; |
| 18 | +use Illuminate\Support\Collection; |
| 19 | +use Toin0u\Geocoder\Exceptions\InvalidDumperException; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Mike Bronner <hello@genealabs.com> |
| 23 | + */ |
| 24 | +class ProviderAndDumperAggregator extends ProviderAggregator implements Geocoder |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var AddressCollection |
| 28 | + */ |
| 29 | + protected $results; |
| 30 | + |
| 31 | + /** |
| 32 | + * @return array |
| 33 | + */ |
| 34 | + public function all() |
| 35 | + { |
| 36 | + return $this->results->all(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string |
| 41 | + * @return Collection |
| 42 | + */ |
| 43 | + public function dump($dumper) |
| 44 | + { |
| 45 | + $dumperClasses = collect([ |
| 46 | + 'geojson' => GeoJson::class, |
| 47 | + 'gpx' => Gpx::class, |
| 48 | + 'kml' => Kml::class, |
| 49 | + 'wkb' => Wkb::class, |
| 50 | + 'wkt' => Wkt::class, |
| 51 | + ]); |
| 52 | + |
| 53 | + if (! $dumperClasses->has($dumper)) { |
| 54 | + $errorMessage = implode('', [ |
| 55 | + "The dumper specified ('{$dumper}') is invalid. Valid dumpers ", |
| 56 | + "are: geojson, gpx, kml, wkb, wkt.", |
| 57 | + ]); |
| 58 | + throw new InvalidDumperException($errorMessage); |
| 59 | + } |
| 60 | + |
| 61 | + $dumperClass = $dumperClasses->get($dumper); |
| 62 | + $dumper = new $dumperClass; |
| 63 | + $results = collect($this->results->all()); |
| 64 | + |
| 65 | + return $results->map(function ($result) use ($dumper) { |
| 66 | + return $dumper->dump($result); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string |
| 72 | + * @return ProviderAndDumperAggregator |
| 73 | + */ |
| 74 | + public function geocode($value) |
| 75 | + { |
| 76 | + $this->results = parent::geocode($value); |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return ProviderAndDumperAggregator |
| 83 | + */ |
| 84 | + public function get() |
| 85 | + { |
| 86 | + return $this->results; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param float |
| 91 | + * @param float |
| 92 | + * @return ProviderAndDumperAggregator |
| 93 | + */ |
| 94 | + public function reverse($latitude, $longitude) |
| 95 | + { |
| 96 | + $this->results = parent::reverse($latitude, $longitude); |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | +} |
0 commit comments