Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Country.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Helpers;
  3. use ArrayAccess;
  4. use Illuminate\Support\Collection;
  5. class Country implements ArrayAccess
  6. {
  7. /**
  8. * The country data array.
  9. */
  10. protected array $data;
  11. /**
  12. * Create a new country instance.
  13. */
  14. public function __construct($data)
  15. {
  16. $this->setData($data);
  17. }
  18. /**
  19. * Set the country data array.
  20. */
  21. public function setData($data): static
  22. {
  23. if (is_array($data)) {
  24. $this->data = $data;
  25. } elseif ($data instanceof self) {
  26. $this->data = $data->getData();
  27. } else {
  28. $this->data = [];
  29. }
  30. return $this;
  31. }
  32. /**
  33. * Get the country data array.
  34. */
  35. public function getData(): ?array
  36. {
  37. return $this->data;
  38. }
  39. /**
  40. * Set a single country data array value.
  41. */
  42. public function set($key, $value): static
  43. {
  44. $this->data[$key] = $value;
  45. return $this;
  46. }
  47. /**
  48. * Get a single country data array value.
  49. */
  50. public function get($key): mixed
  51. {
  52. return $this->data[$key] ?? null;
  53. }
  54. /**
  55. * Check if an offset exists in the data array.
  56. */
  57. public function offsetExists($offset): bool
  58. {
  59. return isset($this->data[$offset]);
  60. }
  61. /**
  62. * Get an offset from the data array.
  63. */
  64. public function offsetGet($offset): mixed
  65. {
  66. return $this->data[$offset] ?? null;
  67. }
  68. /**
  69. * Set an offset in the data array.
  70. */
  71. public function offsetSet($offset, $value): void
  72. {
  73. if ($offset === null) {
  74. $this->data[] = $value;
  75. } else {
  76. $this->data[$offset] = $value;
  77. }
  78. }
  79. /**
  80. * Unset an offset in the data array.
  81. */
  82. public function offsetUnset($offset): void
  83. {
  84. unset($this->data[$offset]);
  85. }
  86. /**
  87. * Get the country id.
  88. */
  89. public function getId(): ?int
  90. {
  91. return $this->get('id');
  92. }
  93. /**
  94. * Get the country name.
  95. */
  96. public function getName(): ?string
  97. {
  98. return $this->get('name');
  99. }
  100. /**
  101. * Get the country iso2.
  102. */
  103. public function getIso2(): ?string
  104. {
  105. return $this->get('iso2');
  106. }
  107. /**
  108. * Get the country iso3.
  109. */
  110. public function getIso3(): ?string
  111. {
  112. return $this->get('iso3');
  113. }
  114. /**
  115. * Get the country numeric code.
  116. */
  117. public function getNumericCode(): ?string
  118. {
  119. return $this->get('numeric_code');
  120. }
  121. /**
  122. * Get the country phone code.
  123. */
  124. public function getPhoneCode(): ?string
  125. {
  126. return $this->get('phone_code');
  127. }
  128. /**
  129. * Get the country capital.
  130. */
  131. public function getCapital(): ?string
  132. {
  133. return $this->get('capital');
  134. }
  135. /**
  136. * Get the country currency code.
  137. */
  138. public function getCurrency(): ?string
  139. {
  140. return $this->get('currency');
  141. }
  142. /**
  143. * Get the country currency name.
  144. */
  145. public function getCurrencyName(): ?string
  146. {
  147. return $this->get('currency_name');
  148. }
  149. /**
  150. * Get the country currency symbol.
  151. */
  152. public function getCurrencySymbol(): ?string
  153. {
  154. return $this->get('currency_symbol');
  155. }
  156. /**
  157. * Get the country tld.
  158. */
  159. public function getTld(): ?string
  160. {
  161. return $this->get('tld');
  162. }
  163. /**
  164. * Get the country native name.
  165. */
  166. public function getNative(): ?string
  167. {
  168. return $this->get('native');
  169. }
  170. /**
  171. * Get the country region.
  172. */
  173. public function getRegion(): ?string
  174. {
  175. return $this->get('region');
  176. }
  177. /**
  178. * Get the country region id.
  179. */
  180. public function getRegionId(): ?int
  181. {
  182. return $this->get('region_id');
  183. }
  184. /**
  185. * Get the country subregion.
  186. */
  187. public function getSubregion(): ?string
  188. {
  189. return $this->get('subregion');
  190. }
  191. /**
  192. * Get the country subregion id.
  193. */
  194. public function getSubregionId(): ?int
  195. {
  196. return $this->get('subregion_id');
  197. }
  198. /**
  199. * Get the country nationality.
  200. */
  201. public function getNationality(): ?string
  202. {
  203. return $this->get('nationality');
  204. }
  205. /**
  206. * Get the country timezones.
  207. */
  208. public function getTimezones(): Collection
  209. {
  210. return collect($this->get('timezones') ?? []);
  211. }
  212. /**
  213. * Get the country translations.
  214. */
  215. public function getTranslations(): Collection
  216. {
  217. return collect($this->get('translations') ?? []);
  218. }
  219. /**
  220. * Get the country latitude.
  221. */
  222. public function getLatitude(): ?string
  223. {
  224. return $this->get('latitude');
  225. }
  226. /**
  227. * Get the country longitude.
  228. */
  229. public function getLongitude(): ?string
  230. {
  231. return $this->get('longitude');
  232. }
  233. /**
  234. * Get the country flag emoji.
  235. */
  236. public function getEmoji(): ?string
  237. {
  238. return $this->get('emoji');
  239. }
  240. /**
  241. * Get the country flag unicode.
  242. */
  243. public function getEmojiU(): ?string
  244. {
  245. return $this->get('emojiU');
  246. }
  247. /**
  248. * Get the country states.
  249. */
  250. public function getStates(): ?array
  251. {
  252. $countryCode = $this->getIso2();
  253. return LocationDataLoader::getAllStates($countryCode, false)->all();
  254. }
  255. }