AddressBuilder.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\Model;
  11. use Geocoder\Exception\InvalidArgument;
  12. use Geocoder\Exception\LogicException;
  13. /**
  14. * A class that builds a Location or any of its subclasses.
  15. *
  16. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  17. */
  18. final class AddressBuilder
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $providedBy;
  24. /**
  25. * @var Coordinates|null
  26. */
  27. private $coordinates;
  28. /**
  29. * @var Bounds|null
  30. */
  31. private $bounds;
  32. /**
  33. * @var string|null
  34. */
  35. private $streetNumber;
  36. /**
  37. * @var string|null
  38. */
  39. private $streetName;
  40. /**
  41. * @var string|null
  42. */
  43. private $locality;
  44. /**
  45. * @var string|null
  46. */
  47. private $postalCode;
  48. /**
  49. * @var string|null
  50. */
  51. private $subLocality;
  52. /**
  53. * @var array
  54. */
  55. private $adminLevels = [];
  56. /**
  57. * @var string|null
  58. */
  59. private $country;
  60. /**
  61. * @var string|null
  62. */
  63. private $countryCode;
  64. /**
  65. * @var string|null
  66. */
  67. private $timezone;
  68. /**
  69. * A storage for extra parameters.
  70. *
  71. * @var array
  72. */
  73. private $data = [];
  74. /**
  75. * @param string $providedBy
  76. */
  77. public function __construct(string $providedBy)
  78. {
  79. $this->providedBy = $providedBy;
  80. }
  81. /**
  82. * @param string $class
  83. *
  84. * @return Address
  85. */
  86. public function build(string $class = Address::class): Address
  87. {
  88. if (!is_a($class, Address::class, true)) {
  89. throw new LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
  90. }
  91. $country = null;
  92. if ((null !== $this->country && '' !== $this->country) || (null !== $this->countryCode && '' !== $this->countryCode)) {
  93. $country = new Country($this->country, $this->countryCode);
  94. }
  95. return new $class(
  96. $this->providedBy,
  97. new AdminLevelCollection($this->adminLevels),
  98. $this->coordinates,
  99. $this->bounds,
  100. $this->streetNumber,
  101. $this->streetName,
  102. $this->postalCode,
  103. $this->locality,
  104. $this->subLocality,
  105. $country,
  106. $this->timezone
  107. );
  108. }
  109. /**
  110. * @param float $south
  111. * @param float $west
  112. * @param float $north
  113. * @param float $east
  114. *
  115. * @return AddressBuilder
  116. */
  117. public function setBounds($south, $west, $north, $east): self
  118. {
  119. try {
  120. $this->bounds = new Bounds($south, $west, $north, $east);
  121. } catch (InvalidArgument $e) {
  122. $this->bounds = null;
  123. }
  124. return $this;
  125. }
  126. /**
  127. * @param float $latitude
  128. * @param float $longitude
  129. *
  130. * @return AddressBuilder
  131. */
  132. public function setCoordinates($latitude, $longitude): self
  133. {
  134. try {
  135. $this->coordinates = new Coordinates($latitude, $longitude);
  136. } catch (InvalidArgument $e) {
  137. $this->coordinates = null;
  138. }
  139. return $this;
  140. }
  141. /**
  142. * @param int $level
  143. * @param string $name
  144. * @param string|null $code
  145. *
  146. * @return AddressBuilder
  147. */
  148. public function addAdminLevel(int $level, string $name, string $code = null): self
  149. {
  150. $this->adminLevels[] = new AdminLevel($level, $name, $code);
  151. return $this;
  152. }
  153. /**
  154. * @param string|null $streetNumber
  155. *
  156. * @return AddressBuilder
  157. */
  158. public function setStreetNumber($streetNumber): self
  159. {
  160. $this->streetNumber = $streetNumber;
  161. return $this;
  162. }
  163. /**
  164. * @param string|null $streetName
  165. *
  166. * @return AddressBuilder
  167. */
  168. public function setStreetName($streetName): self
  169. {
  170. $this->streetName = $streetName;
  171. return $this;
  172. }
  173. /**
  174. * @param string|null $locality
  175. *
  176. * @return AddressBuilder
  177. */
  178. public function setLocality($locality): self
  179. {
  180. $this->locality = $locality;
  181. return $this;
  182. }
  183. /**
  184. * @param string|null $postalCode
  185. *
  186. * @return AddressBuilder
  187. */
  188. public function setPostalCode($postalCode): self
  189. {
  190. $this->postalCode = $postalCode;
  191. return $this;
  192. }
  193. /**
  194. * @param string|null $subLocality
  195. *
  196. * @return AddressBuilder
  197. */
  198. public function setSubLocality($subLocality): self
  199. {
  200. $this->subLocality = $subLocality;
  201. return $this;
  202. }
  203. /**
  204. * @param array $adminLevels
  205. *
  206. * @return AddressBuilder
  207. */
  208. public function setAdminLevels($adminLevels): self
  209. {
  210. $this->adminLevels = $adminLevels;
  211. return $this;
  212. }
  213. /**
  214. * @param string|null $country
  215. *
  216. * @return AddressBuilder
  217. */
  218. public function setCountry($country): self
  219. {
  220. $this->country = $country;
  221. return $this;
  222. }
  223. /**
  224. * @param string|null $countryCode
  225. *
  226. * @return AddressBuilder
  227. */
  228. public function setCountryCode($countryCode): self
  229. {
  230. $this->countryCode = $countryCode;
  231. return $this;
  232. }
  233. /**
  234. * @param string|null $timezone
  235. *
  236. * @return AddressBuilder
  237. */
  238. public function setTimezone($timezone): self
  239. {
  240. $this->timezone = $timezone;
  241. return $this;
  242. }
  243. /**
  244. * @param string $name
  245. * @param mixed $value
  246. *
  247. * @return AddressBuilder
  248. */
  249. public function setValue(string $name, $value): self
  250. {
  251. $this->data[$name] = $value;
  252. return $this;
  253. }
  254. /**
  255. * @param string $name
  256. * @param mixed|null $default
  257. *
  258. * @return mixed
  259. */
  260. public function getValue(string $name, $default = null)
  261. {
  262. if ($this->hasValue($name)) {
  263. return $this->data[$name];
  264. }
  265. return $default;
  266. }
  267. /**
  268. * @param string $name
  269. *
  270. * @return bool
  271. */
  272. public function hasValue(string $name): bool
  273. {
  274. return array_key_exists($name, $this->data);
  275. }
  276. }