index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $data = $data->convertTo(Temperature::UNIT_KELVIN);
  44. $str[] = ($prev == null ? ' ' : ', ') . sprintf('%d %s', $data->getValue(), $data->getDegree());
  45. break;
  46. default:
  47. //$str []= '``' . get_class($data) . '`` missing';
  48. }
  49. $prev = $data;
  50. }
  51. $last = array_pop($str);
  52. if (substr($last, 0, 1) == ',')
  53. $last = substr($last, 1);
  54. if (sizeof($str) == 0)
  55. return strtolower($last);
  56. return strtolower(sprintf(
  57. "%s and %s",
  58. join($str), $last
  59. ));
  60. })()
  61. );
  62. }
  63. public function when(\DateTimeInterface $d): string {
  64. $diff = $d->diff(new \DateTime());
  65. if ($diff->invert && $diff->days == 1)
  66. return sprintf("yesterday %s", $this->period($d));
  67. elseif (!$diff->invert && $diff->days == 1)
  68. return sprintf("tomorrow %s", $this->period($d));
  69. elseif ($diff->days == 0)
  70. return sprintf("This %s", $this->period($d));
  71. return sprintf("the %s at %s of %s %y ",
  72. $this->period($d),
  73. $d->format('dd'), $d->format('m'), $d->format('YY')
  74. );
  75. }
  76. public function period(\DateTimeInterface $d): string {
  77. $hour = (int)$d->format('H');
  78. if ($hour >= 6 && $hour <= 11)
  79. return "morning";
  80. elseif ($hour == 12)
  81. return "noon";
  82. elseif ($hour > 12 && $hour < 18)
  83. return "afternoon";
  84. elseif ($hour >= 18 && $hour < 24)
  85. return "evening";
  86. elseif ($hour == 12)
  87. return "midning";
  88. else
  89. return "night";
  90. }
  91. public function since(\DateTimeInterface $d): string {
  92. }
  93. }
  94. $url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
  95. $forecast = new Forecast($url);
  96. $range = $forecast->getTabular()->getBetween(
  97. $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
  98. );
  99. echo (new TextForcast($range))->start();
  100. ?>