|
| 1 | +<?php |
| 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 | +namespace Geocoder\Provider\MapQuest; |
| 12 | + |
| 13 | +use Geocoder\Exception\InvalidArgument; |
| 14 | +use Geocoder\Formatter\StringFormatter; |
| 15 | +use Geocoder\Location; |
| 16 | +use Geocoder\Query\GeocodeQuery as BaseGeocodeQuery; |
| 17 | + |
| 18 | +class GeocodeQuery extends BaseGeocodeQuery implements GetAddressInterface |
| 19 | +{ |
| 20 | + const DATA_KEY_ADDRESS = 'address'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The address or text that should be geocoded. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $text; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + protected $data = []; |
| 33 | + |
| 34 | + protected function __construct($address) |
| 35 | + { |
| 36 | + if ($address instanceof Location) { |
| 37 | + $this->data[static::DATA_KEY_ADDRESS] = $address; |
| 38 | + } else { |
| 39 | + if (empty($address)) { |
| 40 | + throw new InvalidArgument('Geocode query cannot be empty'); |
| 41 | + } |
| 42 | + $this->text = $address; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static function create(string $text): BaseGeocodeQuery |
| 47 | + { |
| 48 | + return new self($text); |
| 49 | + } |
| 50 | + |
| 51 | + public static function createFromAddress(Location $address): self |
| 52 | + { |
| 53 | + return new self($address); |
| 54 | + } |
| 55 | + |
| 56 | + public function withText(string $text): BaseGeocodeQuery |
| 57 | + { |
| 58 | + $new = clone $this; |
| 59 | + $new->text = $text; |
| 60 | + |
| 61 | + return $new; |
| 62 | + } |
| 63 | + |
| 64 | + public function withData(string $name, $value): BaseGeocodeQuery |
| 65 | + { |
| 66 | + $new = clone $this; |
| 67 | + $new->data[$name] = $value; |
| 68 | + |
| 69 | + return $new; |
| 70 | + } |
| 71 | + |
| 72 | + public function getAddress() |
| 73 | + { |
| 74 | + return $this->getData(static::DATA_KEY_ADDRESS); |
| 75 | + } |
| 76 | + |
| 77 | + public function getText(): string |
| 78 | + { |
| 79 | + if (!$this->text) { |
| 80 | + $address = $this->getAddress(); |
| 81 | + if ($address instanceof Location) { |
| 82 | + $this->text = $this->formatAddress($address); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return $this->text; |
| 87 | + } |
| 88 | + |
| 89 | + public function getData(string $name, $default = null) |
| 90 | + { |
| 91 | + if (!array_key_exists($name, $this->data)) { |
| 92 | + return $default; |
| 93 | + } |
| 94 | + |
| 95 | + return $this->data[$name]; |
| 96 | + } |
| 97 | + |
| 98 | + public function getAllData(): array |
| 99 | + { |
| 100 | + return $this->data; |
| 101 | + } |
| 102 | + |
| 103 | + protected function formatAddress(Location $address): string |
| 104 | + { |
| 105 | + $formatter = new StringFormatter(); |
| 106 | + |
| 107 | + return trim($formatter->format($address, '%n %S, %L, %a1 %z, %C')); |
| 108 | + } |
| 109 | +} |
0 commit comments