index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. use App\Yr\Forecast;
  6. use App\Yr\Forecast\Tabular;
  7. use App\Yr\Forecast\Tabular\Time\Symbol;
  8. use App\Yr\Forecast\Tabular\Time\Temperature;
  9. use App\Yr\Forecast\Tabular\Time\WindDirection;
  10. use App\Yr\Forecast\Tabular\Time\WindSpeed;
  11. require __DIR__ . '/vendor/autoload.php';
  12. class TextForcast {
  13. private $forecast;
  14. public function __construct(Tabular $t) {
  15. $this->forecast = $t;
  16. }
  17. /**
  18. * «Tonight Southeasterly light breeze, cloudy, temperature -8 to -9 degrees»
  19. * @author Helge Tangen
  20. */
  21. public function start(): string {
  22. $time = $this->forecast->getVariations()->getTime();
  23. return sprintf(
  24. "%s from %s (%s) %s",
  25. $this->when($time->getFrom()),
  26. $time->getFrom()->format('H'),
  27. $time->getFrom()->format('h a'),
  28. (function() use ($time){
  29. $str = [];
  30. $prev = null;
  31. foreach ($time as $data) {
  32. switch (get_class($data)) {
  33. case Symbol::class:
  34. $str[] = ($prev == null ? ' ' : ', ') . $data->getName();
  35. break;
  36. case WindSpeed::class:
  37. $str[] = ($prev == null || WindSpeed::class ? " " : ", ") . $data->getName();
  38. break;
  39. case WindDirection::class:
  40. $str[] = ($prev == null ? ' ' : ', ') . $data->getName();
  41. break;
  42. case Temperature::class:
  43. $str[] = ($prev == null ? ' ' : ', ') . sprintf('%d %s', $data->getValue(), $data->getDegree());
  44. break;
  45. default:
  46. //$str []= '``' . get_class($data) . '`` missing';
  47. }
  48. $prev = $data;
  49. }
  50. $last = array_pop($str);
  51. if (substr($last, 0, 1) == ',')
  52. $last = substr($last, 1);
  53. if (sizeof($str) == 0)
  54. return strtolower($last);
  55. return strtolower(sprintf(
  56. "%s and %s",
  57. join($str), $last
  58. ));
  59. })()
  60. );
  61. }
  62. public function when(\DateTimeInterface $d): string {
  63. $diff = $d->diff(new \DateTime());
  64. if ($diff->invert && $diff->days == 1)
  65. return sprintf("yesterday %s", $this->period($d));
  66. elseif (!$diff->invert && $diff->days == 1)
  67. return sprintf("tomorrow %s", $this->period($d));
  68. elseif ($diff->days == 0)
  69. return sprintf("This %s", $this->period($d));
  70. return sprintf("the %s at %s of %s %y ",
  71. $this->period($d),
  72. $d->format('dd'), $d->format('m'), $d->format('YY')
  73. );
  74. }
  75. public function period(\DateTimeInterface $d): string {
  76. $hour = (int)$d->format('H');
  77. if ($hour >= 6 && $hour <= 11)
  78. return "morning";
  79. elseif ($hour == 12)
  80. return "noon";
  81. elseif ($hour > 12 && $hour < 18)
  82. return "afternoon";
  83. elseif ($hour >= 18 && $hour < 24)
  84. return "evening";
  85. elseif ($hour == 12)
  86. return "midning";
  87. else
  88. return "night";
  89. }
  90. public function since(\DateTimeInterface $d): string {
  91. }
  92. }
  93. $url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
  94. $forecast = new Forecast($url);
  95. $range = $forecast->getTabular()->getBetween(
  96. $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
  97. );
  98. echo (new TextForcast($range))->start();
  99. ?>