ProviderAggregator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. declare(strict_types=1);
  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. namespace Geocoder;
  11. use Geocoder\Exception\ProviderNotRegistered;
  12. use Geocoder\Model\Coordinates;
  13. use Geocoder\Query\GeocodeQuery;
  14. use Geocoder\Query\ReverseQuery;
  15. use Geocoder\Provider\Provider;
  16. /**
  17. * @author William Durand <william.durand1@gmail.com>
  18. */
  19. class ProviderAggregator implements Geocoder
  20. {
  21. /**
  22. * @var Provider[]
  23. */
  24. private $providers = [];
  25. /**
  26. * @var Provider
  27. */
  28. private $provider;
  29. /**
  30. * @var int
  31. */
  32. private $limit;
  33. /**
  34. * A callable that decided what provider to use.
  35. *
  36. * @var callable
  37. */
  38. private $decider;
  39. /**
  40. * @param callable|null $decider
  41. * @param int $limit
  42. */
  43. public function __construct(callable $decider = null, int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
  44. {
  45. $this->limit = $limit;
  46. $this->decider = $decider ?? __CLASS__.'::getProvider';
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function geocodeQuery(GeocodeQuery $query): Collection
  52. {
  53. if (null === $query->getLimit()) {
  54. $query = $query->withLimit($this->limit);
  55. }
  56. return call_user_func($this->decider, $query, $this->providers, $this->provider)->geocodeQuery($query);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function reverseQuery(ReverseQuery $query): Collection
  62. {
  63. if (null === $query->getLimit()) {
  64. $query = $query->withLimit($this->limit);
  65. }
  66. return call_user_func($this->decider, $query, $this->providers, $this->provider)->reverseQuery($query);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getName(): string
  72. {
  73. return 'provider_aggregator';
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function geocode(string $value): Collection
  79. {
  80. return $this->geocodeQuery(GeocodeQuery::create($value)
  81. ->withLimit($this->limit));
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function reverse(float $latitude, float $longitude): Collection
  87. {
  88. return $this->reverseQuery(ReverseQuery::create(new Coordinates($latitude, $longitude))
  89. ->withLimit($this->limit));
  90. }
  91. /**
  92. * Registers a new provider to the aggregator.
  93. *
  94. * @param Provider $provider
  95. *
  96. * @return ProviderAggregator
  97. */
  98. public function registerProvider(Provider $provider): self
  99. {
  100. $this->providers[$provider->getName()] = $provider;
  101. return $this;
  102. }
  103. /**
  104. * Registers a set of providers.
  105. *
  106. * @param Provider[] $providers
  107. *
  108. * @return ProviderAggregator
  109. */
  110. public function registerProviders(array $providers = []): self
  111. {
  112. foreach ($providers as $provider) {
  113. $this->registerProvider($provider);
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Sets the default provider to use.
  119. *
  120. * @param string $name
  121. *
  122. * @return ProviderAggregator
  123. */
  124. public function using(string $name): self
  125. {
  126. if (!isset($this->providers[$name])) {
  127. throw ProviderNotRegistered::create($name, array_keys($this->providers));
  128. }
  129. $this->provider = $this->providers[$name];
  130. return $this;
  131. }
  132. /**
  133. * Returns all registered providers indexed by their name.
  134. *
  135. * @return Provider[]
  136. */
  137. public function getProviders(): array
  138. {
  139. return $this->providers;
  140. }
  141. /**
  142. * Get a provider to use for this query.
  143. *
  144. * @param GeocodeQuery|ReverseQuery $query
  145. * @param Provider[] $providers
  146. * @param Provider $currentProvider
  147. *
  148. * @return Provider
  149. *
  150. * @throws ProviderNotRegistered
  151. */
  152. private static function getProvider($query, array $providers, Provider $currentProvider = null): Provider
  153. {
  154. if (null !== $currentProvider) {
  155. return $currentProvider;
  156. }
  157. if ([] === $providers) {
  158. throw ProviderNotRegistered::noProviderRegistered();
  159. }
  160. // Take first
  161. $key = key($providers);
  162. return $providers[$key];
  163. }
  164. }