|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Geocoder package. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Danhunsaker\Geocoder\Provider\Mock; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
|
16
|
|
|
use Geocoder\Location; |
|
17
|
|
|
use Geocoder\Model\Address; |
|
18
|
|
|
use Geocoder\Model\AddressBuilder; |
|
19
|
|
|
use Geocoder\Model\AddressCollection; |
|
20
|
|
|
use Geocoder\Query\GeocodeQuery; |
|
21
|
|
|
use Geocoder\Query\ReverseQuery; |
|
22
|
|
|
use Geocoder\Http\Provider\AbstractHttpProvider; |
|
23
|
|
|
use Geocoder\Provider\Provider; |
|
24
|
|
|
use Http\Client\HttpClient; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author Niklas Närhinen <[email protected]> |
|
28
|
|
|
* @author Jonathan Beliën <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class Mock extends AbstractHttpProvider implements Provider |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var float[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $latLong; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $address; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param HttpClient $client an HTTP client |
|
44
|
|
|
* @param float[] $latLong The latitude and longitude to return for all requests |
|
45
|
|
|
* @param string[] $address The address to return for all requests |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function __construct(HttpClient $client, $latLong, $address) |
|
48
|
|
|
{ |
|
49
|
4 |
|
parent::__construct($client); |
|
50
|
|
|
|
|
51
|
4 |
|
$this->latLong = $latLong; |
|
52
|
4 |
|
$this->address = $address; |
|
53
|
|
|
|
|
54
|
4 |
|
if (empty($this->latLong)) { |
|
55
|
|
|
throw new \InvalidArgument('The Lat/Long must be set to use the Mock provider.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
if (empty($this->address)) { |
|
59
|
|
|
throw new \InvalidArgument('The Address must be set to use the Mock provider.'); |
|
60
|
|
|
} |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function geocodeQuery(GeocodeQuery $query): Collection |
|
67
|
|
|
{ |
|
68
|
2 |
|
$address = Address::createFromArray($this->address + [ |
|
69
|
2 |
|
'providedBy' => $this->getName(), |
|
70
|
2 |
|
'latitude' => $this->latLong[0], |
|
71
|
2 |
|
'longitude' => $this->latLong[1], |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
2 |
|
$match = true; |
|
75
|
|
|
|
|
76
|
2 |
|
foreach (explode(' ', $query->getText()) as $part) { |
|
77
|
2 |
|
$part = trim($part, ','); |
|
78
|
|
|
|
|
79
|
2 |
|
if ($part === (string)$address->getStreetNumber()) { |
|
80
|
1 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
if (stripos($address->getStreetName(), $part) !== false) { |
|
84
|
1 |
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
if ($part === $address->getLocality()) { |
|
88
|
1 |
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
foreach ($address->getAdminLevels() as $level) { |
|
92
|
|
|
if ($part === $level->getCode() || $part === $level->getName()) { |
|
93
|
|
|
continue 2; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
if ($part === (string)$address->getPostalCode()) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
if (!empty($address->getCountry()) && ($part === $address->getCountry()->getName() || $part === $address->getCountry()->getCode())) { |
|
102
|
1 |
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
$match = false; |
|
106
|
1 |
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
if ($match) { |
|
110
|
1 |
|
return new AddressCollection([$address]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
return new AddressCollection([]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function reverseQuery(ReverseQuery $query): Collection |
|
120
|
|
|
{ |
|
121
|
2 |
|
if ($query->getCoordinates()->getLatitude() == $this->latLong[0] && $query->getCoordinates()->getLongitude() == $this->latLong[1]) { |
|
122
|
|
|
return new AddressCollection([Address::createFromArray($this->address + [ |
|
123
|
|
|
'providedBy' => $this->getName(), |
|
124
|
|
|
'latitude' => $this->latLong[0], |
|
125
|
|
|
'longitude' => $this->latLong[1], |
|
126
|
|
|
])]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
return new AddressCollection([]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function getName(): string |
|
136
|
|
|
{ |
|
137
|
2 |
|
return 'mock'; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|