StatefulGeocoder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Model\Bounds;
  12. use Geocoder\Query\GeocodeQuery;
  13. use Geocoder\Query\ReverseQuery;
  14. use Geocoder\Provider\Provider;
  15. /**
  16. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  17. */
  18. final class StatefulGeocoder implements Geocoder
  19. {
  20. /**
  21. * @var string|null
  22. */
  23. private $locale;
  24. /**
  25. * @var Bounds
  26. */
  27. private $bounds;
  28. /**
  29. * @var int
  30. */
  31. private $limit;
  32. /**
  33. * @var Provider
  34. */
  35. private $provider;
  36. /**
  37. * @param Provider $provider
  38. * @param string $locale
  39. */
  40. public function __construct(Provider $provider, string $locale = null)
  41. {
  42. $this->provider = $provider;
  43. $this->locale = $locale;
  44. $this->limit = Geocoder::DEFAULT_RESULT_LIMIT;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function geocode(string $value): Collection
  50. {
  51. $query = GeocodeQuery::create($value)
  52. ->withLimit($this->limit);
  53. if (null !== $this->locale && '' !== $this->locale) {
  54. $query = $query->withLocale($this->locale);
  55. }
  56. if (!empty($this->bounds)) {
  57. $query = $query->withBounds($this->bounds);
  58. }
  59. return $this->provider->geocodeQuery($query);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function reverse(float $latitude, float $longitude): Collection
  65. {
  66. $query = ReverseQuery::fromCoordinates($latitude, $longitude)
  67. ->withLimit($this->limit);
  68. if (null !== $this->locale && '' !== $this->locale) {
  69. $query = $query->withLocale($this->locale);
  70. }
  71. return $this->provider->reverseQuery($query);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function geocodeQuery(GeocodeQuery $query): Collection
  77. {
  78. $locale = $query->getLocale();
  79. if ((null === $locale || '' === $locale) && null !== $this->locale) {
  80. $query = $query->withLocale($this->locale);
  81. }
  82. $bounds = $query->getBounds();
  83. if (empty($bounds) && null !== $this->bounds) {
  84. $query = $query->withBounds($this->bounds);
  85. }
  86. return $this->provider->geocodeQuery($query);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function reverseQuery(ReverseQuery $query): Collection
  92. {
  93. $locale = $query->getLocale();
  94. if ((null === $locale || '' === $locale) && null !== $this->locale) {
  95. $query = $query->withLocale($this->locale);
  96. }
  97. return $this->provider->reverseQuery($query);
  98. }
  99. /**
  100. * @param string $locale
  101. *
  102. * @return StatefulGeocoder
  103. */
  104. public function setLocale(string $locale): self
  105. {
  106. $this->locale = $locale;
  107. return $this;
  108. }
  109. /**
  110. * @param Bounds $bounds
  111. *
  112. * @return StatefulGeocoder
  113. */
  114. public function setBounds(Bounds $bounds): self
  115. {
  116. $this->bounds = $bounds;
  117. return $this;
  118. }
  119. /**
  120. * @param int $limit
  121. *
  122. * @return StatefulGeocoder
  123. */
  124. public function setLimit(int $limit): self
  125. {
  126. $this->limit = $limit;
  127. return $this;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getName(): string
  133. {
  134. return 'stateful_geocoder';
  135. }
  136. }