Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

City.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Helpers;
  3. use ArrayAccess;
  4. class City implements ArrayAccess
  5. {
  6. protected array $data;
  7. public function __construct($data)
  8. {
  9. $this->setData($data);
  10. }
  11. /**
  12. * Set the city data array.
  13. */
  14. public function setData($data): static
  15. {
  16. $this->data = $data;
  17. return $this;
  18. }
  19. /**
  20. * Get the city data array.
  21. */
  22. public function getData(): ?array
  23. {
  24. return $this->data;
  25. }
  26. /**
  27. * Set a single city data array value.
  28. */
  29. public function set($key, $value): static
  30. {
  31. $this->data[$key] = $value;
  32. return $this;
  33. }
  34. /**
  35. * Get a single city data array value.
  36. */
  37. public function get($key): mixed
  38. {
  39. return $this->data[$key] ?? null;
  40. }
  41. /**
  42. * Check if an offset exists in the data array.
  43. */
  44. public function offsetExists($offset): bool
  45. {
  46. return isset($this->data[$offset]);
  47. }
  48. /**
  49. * Get an offset from the data array.
  50. */
  51. public function offsetGet($offset): mixed
  52. {
  53. return $this->data[$offset] ?? null;
  54. }
  55. /**
  56. * Set an offset in the data array.
  57. */
  58. public function offsetSet($offset, $value): void
  59. {
  60. if ($offset === null) {
  61. $this->data[] = $value;
  62. } else {
  63. $this->data[$offset] = $value;
  64. }
  65. }
  66. /**
  67. * Unset an offset in the data array.
  68. */
  69. public function offsetUnset($offset): void
  70. {
  71. unset($this->data[$offset]);
  72. }
  73. /**
  74. * Get the city ID.
  75. */
  76. public function getId(): ?int
  77. {
  78. return $this->get('id');
  79. }
  80. /**
  81. * Get the city name.
  82. */
  83. public function getName(): ?string
  84. {
  85. return $this->get('name');
  86. }
  87. /**
  88. * Get the city latitude.
  89. */
  90. public function getLatitude(): ?string
  91. {
  92. return $this->get('latitude');
  93. }
  94. /**
  95. * Get the city longitude.
  96. */
  97. public function getLongitude(): ?string
  98. {
  99. return $this->get('longitude');
  100. }
  101. }