Forecast.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Yr;
  3. use App\Yr\Forecast\Location;
  4. use App\Yr\Forecast\Tabular;
  5. final class Forecast {
  6. private $xml;
  7. private $location;
  8. public function __construct(string $url) {
  9. $this->xml = simplexml_load_file($url);
  10. $this->location = new Location($this->xml->location);
  11. }
  12. public function getLocation(): Location {
  13. return $this->location;
  14. }
  15. final public function getCredit(): array {
  16. return [
  17. 'text' => (string)$this->xml->credit->link->attributes()['text'],
  18. 'url' => (string)$this->xml->credit->link->attributes()['url']
  19. ];
  20. }
  21. public function getSunrise(): \DateTimeImmutable {
  22. return (new \DateTimeImmutable($this->xml->sun['rise']));
  23. }
  24. public function getSunset(): \DateTimeImmutable {
  25. return (new \DateTimeImmutable($this->xml->sun['seẗ́']));
  26. }
  27. public function getLinks(): \Generator {
  28. foreach ($this->xml->links->children() as $link)
  29. yield [
  30. 'id' => (string)$link->attributes()['id'],
  31. 'url' => (string)$link->attributes()['url'],
  32. ];
  33. }
  34. public function getLastUpdate(): \DateTimeImmutable {
  35. return new DateTimeImmutable($this->xml->meta->lastupdate);
  36. }
  37. public function getNextUpdate(): \DateTimeImmutable {
  38. return new DateTimeImmutable($this->xml->meta->nextupdate);
  39. }
  40. public function getTabular(): Tabular {
  41. return new Tabular($this->xml->forecast->tabular);
  42. }
  43. }
  44. ?>