Location.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Yr\Forecast;
  3. /**
  4. * Get information about the location,
  5. * such as name, timezone and geodata
  6. *
  7. * @author Joachim M. Giæver (joachim[]giaever.org)
  8. */
  9. final class Location {
  10. private $name;
  11. private $type;
  12. private $country;
  13. private $tz;
  14. private $loc = [];
  15. /**
  16. * @param \SimpleXMLElement $xml The xml-part containing the location data
  17. */
  18. public function __construct(\SimpleXMLElement $xml) {
  19. $this->name = (string)$xml->name;
  20. $this->type = (string)$xml->type;
  21. $this->country = (string)$xml->country;
  22. $this->tz = (string)$xml->timezone['id'];
  23. $this->loc = [
  24. 'lat' => (float)$xml->location['latitude'],
  25. 'lng' => (float)$xml->location['longitude'],
  26. 'alt' => (float)$xml->location['altitude']
  27. ];
  28. }
  29. /**
  30. * Get the location name, e.g «Tromsø»
  31. *
  32. * @return string
  33. */
  34. public function getName(): string {
  35. return $this->name;
  36. }
  37. /**
  38. * Get the location type, e.g «City - large town»
  39. *
  40. * @return string
  41. */
  42. public function getType(): string {
  43. return $this->type;
  44. }
  45. /**
  46. * Get the location country
  47. *
  48. * @return string
  49. */
  50. public function getCountry(): string {
  51. return $this->country;
  52. }
  53. /**
  54. * Get the location timezone
  55. *
  56. * @return \DateTimeZone
  57. */
  58. public function getTimeZone(): \DateTimeZone {
  59. return new \DateTimeZone($this->tz);
  60. }
  61. /**
  62. * Returns the geodata, in the format
  63. * ```[
  64. * 'lat' => (float),
  65. * 'lng' => (float),
  66. * 'alt' => (float),
  67. * ]```
  68. *
  69. * @return array
  70. */
  71. public function getGeoData(): array {
  72. return $this->loc;
  73. }
  74. }
  75. ?>