ReverseQuery.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\Query;
  11. use Geocoder\Geocoder;
  12. use Geocoder\Model\Coordinates;
  13. /**
  14. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  15. */
  16. final class ReverseQuery implements Query
  17. {
  18. /**
  19. * @var Coordinates
  20. */
  21. private $coordinates;
  22. /**
  23. * @var string|null
  24. */
  25. private $locale;
  26. /**
  27. * @var int
  28. */
  29. private $limit = Geocoder::DEFAULT_RESULT_LIMIT;
  30. /**
  31. * @var array
  32. */
  33. private $data = [];
  34. /**
  35. * @param Coordinates $coordinates
  36. */
  37. private function __construct(Coordinates $coordinates)
  38. {
  39. $this->coordinates = $coordinates;
  40. }
  41. /**
  42. * @param Coordinates $coordinates
  43. *
  44. * @return ReverseQuery
  45. */
  46. public static function create(Coordinates $coordinates)
  47. {
  48. return new self($coordinates);
  49. }
  50. /**
  51. * @param float $latitude
  52. * @param float $longitude
  53. *
  54. * @return ReverseQuery
  55. */
  56. public static function fromCoordinates($latitude, $longitude): self
  57. {
  58. return new self(new Coordinates($latitude, $longitude));
  59. }
  60. /**
  61. * @param Coordinates $coordinates
  62. *
  63. * @return ReverseQuery
  64. */
  65. public function withCoordinates(Coordinates $coordinates): self
  66. {
  67. $new = clone $this;
  68. $new->coordinates = $coordinates;
  69. return $new;
  70. }
  71. /**
  72. * @param int $limit
  73. *
  74. * @return ReverseQuery
  75. */
  76. public function withLimit(int $limit): self
  77. {
  78. $new = clone $this;
  79. $new->limit = $limit;
  80. return $new;
  81. }
  82. /**
  83. * @param string $locale
  84. *
  85. * @return ReverseQuery
  86. */
  87. public function withLocale(string $locale): self
  88. {
  89. $new = clone $this;
  90. $new->locale = $locale;
  91. return $new;
  92. }
  93. /**
  94. * @param string $name
  95. * @param mixed $value
  96. *
  97. * @return ReverseQuery
  98. */
  99. public function withData(string $name, $value): self
  100. {
  101. $new = clone $this;
  102. $new->data[$name] = $value;
  103. return $new;
  104. }
  105. /**
  106. * @return Coordinates
  107. */
  108. public function getCoordinates(): Coordinates
  109. {
  110. return $this->coordinates;
  111. }
  112. /**
  113. * @return int
  114. */
  115. public function getLimit(): int
  116. {
  117. return $this->limit;
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function getLocale()
  123. {
  124. return $this->locale;
  125. }
  126. /**
  127. * @param string $name
  128. * @param mixed|null $default
  129. *
  130. * @return mixed
  131. */
  132. public function getData(string $name, $default = null)
  133. {
  134. if (!array_key_exists($name, $this->data)) {
  135. return $default;
  136. }
  137. return $this->data[$name];
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function getAllData(): array
  143. {
  144. return $this->data;
  145. }
  146. /**
  147. * String for logging. This is also a unique key for the query.
  148. *
  149. * @return string
  150. */
  151. public function __toString()
  152. {
  153. return sprintf('ReverseQuery: %s', json_encode([
  154. 'lat' => $this->getCoordinates()->getLatitude(),
  155. 'lng' => $this->getCoordinates()->getLongitude(),
  156. 'locale' => $this->getLocale(),
  157. 'limit' => $this->getLimit(),
  158. 'data' => $this->getAllData(),
  159. ]));
  160. }
  161. }