GeocodeQuery.php 3.7 KB

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