geocoder-php /
Geocoder
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 Geocoder; |
||
| 14 | |||
| 15 | use Geocoder\Exception\ProviderNotRegistered; |
||
| 16 | use Geocoder\Model\Coordinates; |
||
| 17 | use Geocoder\Query\GeocodeQuery; |
||
| 18 | use Geocoder\Query\ReverseQuery; |
||
| 19 | use Geocoder\Provider\Provider; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @author William Durand <[email protected]> |
||
| 23 | */ |
||
| 24 | class ProviderAggregator implements Geocoder |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var Provider[] |
||
| 28 | */ |
||
| 29 | private $providers = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Provider |
||
| 33 | */ |
||
| 34 | private $provider; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $limit; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A callable that decided what provider to use. |
||
| 43 | * |
||
| 44 | * @var callable |
||
| 45 | */ |
||
| 46 | private $decider; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param callable|null $decider |
||
| 50 | * @param int $limit |
||
| 51 | */ |
||
| 52 | public function __construct(callable $decider = null, int $limit = Geocoder::DEFAULT_RESULT_LIMIT) |
||
| 53 | { |
||
| 54 | $this->limit = $limit; |
||
| 55 | $this->decider = $decider ?? __CLASS__.'::getProvider'; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 62 | { |
||
| 63 | if (null === $query->getLimit()) { |
||
| 64 | $query = $query->withLimit($this->limit); |
||
| 65 | } |
||
| 66 | |||
| 67 | return call_user_func($this->decider, $query, $this->providers, $this->provider)->geocodeQuery($query); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function reverseQuery(ReverseQuery $query): Collection |
||
| 74 | { |
||
| 75 | if (null === $query->getLimit()) { |
||
| 76 | $query = $query->withLimit($this->limit); |
||
| 77 | } |
||
| 78 | |||
| 79 | return call_user_func($this->decider, $query, $this->providers, $this->provider)->reverseQuery($query); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function getName(): string |
||
| 86 | { |
||
| 87 | return 'provider_aggregator'; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function geocode(string $value): Collection |
||
| 94 | { |
||
| 95 | return $this->geocodeQuery(GeocodeQuery::create($value) |
||
| 96 | ->withLimit($this->limit)); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function reverse(float $latitude, float $longitude): Collection |
||
| 103 | { |
||
| 104 | return $this->reverseQuery(ReverseQuery::create(new Coordinates($latitude, $longitude)) |
||
| 105 | ->withLimit($this->limit)); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Registers a new provider to the aggregator. |
||
| 110 | * |
||
| 111 | * @param Provider $provider |
||
| 112 | * |
||
| 113 | * @return ProviderAggregator |
||
| 114 | */ |
||
| 115 | public function registerProvider(Provider $provider): self |
||
| 116 | { |
||
| 117 | $this->providers[$provider->getName()] = $provider; |
||
| 118 | |||
| 119 | return $this; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Registers a set of providers. |
||
| 124 | * |
||
| 125 | * @param Provider[] $providers |
||
| 126 | * |
||
| 127 | * @return ProviderAggregator |
||
| 128 | */ |
||
| 129 | public function registerProviders(array $providers = []): self |
||
| 130 | { |
||
| 131 | foreach ($providers as $provider) { |
||
| 132 | $this->registerProvider($provider); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Sets the default provider to use. |
||
| 140 | * |
||
| 141 | * @param string $name |
||
| 142 | * |
||
| 143 | * @return ProviderAggregator |
||
| 144 | */ |
||
| 145 | public function using(string $name): self |
||
| 146 | { |
||
| 147 | if (!isset($this->providers[$name])) { |
||
| 148 | throw ProviderNotRegistered::create($name ?? '', $this->providers); |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->provider = $this->providers[$name]; |
||
| 152 | |||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns all registered providers indexed by their name. |
||
| 158 | * |
||
| 159 | * @return Provider[] |
||
| 160 | */ |
||
| 161 | public function getProviders(): array |
||
| 162 | { |
||
| 163 | return $this->providers; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get a provider to use for this query. |
||
| 168 | * |
||
| 169 | * @param GeocodeQuery|ReverseQuery $query |
||
| 170 | * @param Provider[] $providers |
||
| 171 | * @param Provider $currentProvider |
||
|
0 ignored issues
–
show
|
|||
| 172 | * |
||
| 173 | * @return Provider |
||
| 174 | * |
||
| 175 | * @throws ProviderNotRegistered |
||
| 176 | */ |
||
| 177 | private static function getProvider($query, array $providers, Provider $currentProvider = null): Provider |
||
|
0 ignored issues
–
show
|
|||
| 178 | { |
||
| 179 | if (null !== $currentProvider) { |
||
| 180 | return $currentProvider; |
||
| 181 | } |
||
| 182 | |||
| 183 | if (0 === count($providers)) { |
||
| 184 | throw ProviderNotRegistered::noProviderRegistered(); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Take first |
||
| 188 | $key = key($providers); |
||
| 189 | |||
| 190 | return $providers[$key]; |
||
| 191 | } |
||
| 192 | } |
||
| 193 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.