Forecast.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * Require the package to your project by adding (or creating)
  12. * the following in the `composer.json`-file:
  13. * ```
  14. * {
  15. * "require": {
  16. * "joachimmg/yr-forecast": "dev-master"
  17. * },
  18. * "repositories": [{
  19. * "type": "vcs",
  20. * "url": "https://git.giaever.org/joachimmg/yr-forecast.git"
  21. * }]
  22. * }
  23. * ```
  24. *
  25. * @author Joachim M. Giæver (joachim[]giaever.org)
  26. * @version 1.0
  27. */
  28. final class Forecast {
  29. private $xml;
  30. private $location;
  31. /**
  32. * @param string $url The XML url to load data from
  33. */
  34. public function __construct(string $url) {
  35. $this->xml = simplexml_load_file($url);
  36. $this->location = new Location($this->xml->location);
  37. }
  38. /**
  39. * Returns the location data for the forecast
  40. *
  41. * @return App\Forecast\Location
  42. */
  43. public function getLocation(): Location {
  44. return $this->location;
  45. }
  46. /**
  47. * Return the credit to Yr.no, Meterogical Institute and NRK
  48. *
  49. * @return array
  50. */
  51. final public function getCredit(): array {
  52. return [
  53. 'text' => (string)$this->xml->credit->link->attributes()['text'],
  54. 'url' => (string)$this->xml->credit->link->attributes()['url']
  55. ];
  56. }
  57. /**
  58. * Returns the time when the sun rise for the location
  59. *
  60. * @return \DateTimeImmutable
  61. */
  62. public function getSunrise(): \DateTimeImmutable {
  63. return new \DateTimeImmutable((string)$this->xml->sun['rise']);
  64. }
  65. /**
  66. * Returns the time when the sun sets for the location
  67. *
  68. * @return DateTimeImmutable
  69. */
  70. public function getSunset(): \DateTimeImmutable {
  71. return new \DateTimeImmutable((string)$this->xml->sun['set']);
  72. }
  73. /**
  74. * Returns links for forecasts in other formats
  75. *
  76. * @return \Generator
  77. */
  78. public function getLinks(): \Generator {
  79. foreach ($this->xml->links->children() as $link)
  80. yield [
  81. 'id' => (string)$link->attributes()['id'],
  82. 'url' => (string)$link->attributes()['url'],
  83. ];
  84. }
  85. /**
  86. * Return the time when this forecast was last update
  87. *
  88. * @return \DateTimeImmutable
  89. */
  90. public function getLastUpdate(): \DateTimeImmutable {
  91. return new \DateTimeImmutable((string)$this->xml->meta->lastupdate);
  92. }
  93. /**
  94. * Return the time when this forecast will update next
  95. *
  96. * @return \DateTimeImmutable
  97. */
  98. public function getNextUpdate(): \DateTimeImmutable {
  99. return new \DateTimeImmutable((string)$this->xml->meta->nextupdate);
  100. }
  101. /**
  102. * Get the forecast data
  103. *
  104. * @return Tabular
  105. */
  106. public function getTabular(): Tabular {
  107. return new Tabular($this, $this->xml->forecast->tabular);
  108. }
  109. }
  110. ?>