Forecast.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Yr;
  3. use App\Yr\Forecast\Location;
  4. use App\Yr\Forecast\Tabular;
  5. /**
  6. * Read forecast data from Yr.no for a specific location.
  7. *
  8. * Disclaimer: To use this package you are required
  9. * to print the credits from the method ```getCredit()```
  10. *
  11. * @author Joachim M. Giæver (joachim[]giaever.org)
  12. * @version 1.0
  13. */
  14. final class Forecast {
  15. private $xml;
  16. private $location;
  17. /**
  18. * @param string $url The XML url to load data from
  19. */
  20. public function __construct(string $url) {
  21. $this->xml = simplexml_load_file($url);
  22. $this->location = new Location($this->xml->location);
  23. }
  24. /**
  25. * Returns the location data for the forecast
  26. *
  27. * @return App\Forecast\Location
  28. */
  29. public function getLocation(): Location {
  30. return $this->location;
  31. }
  32. /**
  33. * Return the credit to Yr.no, Meterogical Institute and NRK
  34. *
  35. * @return array
  36. */
  37. final public function getCredit(): array {
  38. return [
  39. 'text' => (string)$this->xml->credit->link->attributes()['text'],
  40. 'url' => (string)$this->xml->credit->link->attributes()['url']
  41. ];
  42. }
  43. /**
  44. * Returns the time when the sun rise for the location
  45. *
  46. * @return \DateTimeImmutable
  47. */
  48. public function getSunrise(): \DateTimeImmutable {
  49. return new \DateTimeImmutable((string)$this->xml->sun['rise']);
  50. }
  51. /**
  52. * Returns the time when the sun sets for the location
  53. *
  54. * @return DateTimeImmutable
  55. */
  56. public function getSunset(): \DateTimeImmutable {
  57. return new \DateTimeImmutable((string)$this->xml->sun['set']);
  58. }
  59. /**
  60. * Returns links for forecasts in other formats
  61. *
  62. * @return \Generator
  63. */
  64. public function getLinks(): \Generator {
  65. foreach ($this->xml->links->children() as $link)
  66. yield [
  67. 'id' => (string)$link->attributes()['id'],
  68. 'url' => (string)$link->attributes()['url'],
  69. ];
  70. }
  71. /**
  72. * Return the time when this forecast was last update
  73. *
  74. * @return \DateTimeImmutable
  75. */
  76. public function getLastUpdate(): \DateTimeImmutable {
  77. return new DateTimeImmutable((string)$this->xml->meta->lastupdate);
  78. }
  79. /**
  80. * Return the time when this forecast will update next
  81. *
  82. * @return \DateTimeImmutable
  83. */
  84. public function getNextUpdate(): \DateTimeImmutable {
  85. return new DateTimeImmutable((string)$this->xml->meta->nextupdate);
  86. }
  87. /**
  88. * Get the forecast data
  89. *
  90. * @return Tabular
  91. */
  92. public function getTabular(): Tabular {
  93. return new Tabular($this->xml->forecast->tabular);
  94. }
  95. }
  96. ?>