index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\AbstractUnit;
  8. use App\Yr\Forecast\Tabular\Time\Symbol;
  9. use App\Yr\Forecast\Tabular\Time\Temperature;
  10. use App\Yr\Forecast\Tabular\Time\WindDirection;
  11. use App\Yr\Forecast\Tabular\Time\WindSpeed;
  12. require __DIR__ . '/vendor/autoload.php';
  13. class TextForcast {
  14. const HOUR_FORMAT = 'H (h a)';
  15. private $forecast;
  16. public function __construct(Tabular $t) {
  17. $this->forecast = $t;
  18. }
  19. /**
  20. * «Tonight Southeasterly light breeze, cloudy, temperature -8 to -9 degrees»
  21. * @author Helge Tangen
  22. */
  23. public function json(): string {
  24. $time = $this->forecast->getVariations()->getTime();
  25. $arr = [
  26. "from" => $this->forecast->getFrom()->format('Y-m-d H:i'),
  27. "until" => $this->forecast->getUntil()->format("Y-m-d H:i"),
  28. "initial" => [],
  29. "changes" => [],
  30. "credit" => $this->forecast->getForecast()->getCredit()
  31. ];
  32. foreach ($time as $ent) {
  33. $arr['initial'][] = (string)$ent;
  34. }
  35. $vars = $this->forecast->getVariations()->filter(function($e, $p, $n) {
  36. return ($p == null ? true : $e->thresholdDiff($p->getEntity(get_class($e))));
  37. });
  38. foreach ($vars as $var) {
  39. $changes = [
  40. "at" => [
  41. "time" => $var->getTime()->getFrom()->format('Y-m-d H:i'),
  42. "period" => $this->when($var->getTime()->getFrom())
  43. ],
  44. "to" => [],
  45. ];
  46. $var->operate(function($e, $p, $n) use (&$changes) {
  47. $changes['to'][] = (string)$e;
  48. });
  49. $arr['changes'][] = $changes;
  50. }
  51. return json_encode($arr);
  52. }
  53. public function when(\DateTimeInterface $d): string {
  54. $a = DateTime::createFromFormat('d/m/Y', $d->format('d/m/Y'));
  55. $b = DateTime::createFromFormat('d/m/Y', (new \DateTime())->format('d/m/Y'));
  56. $diff = $b->diff($a);
  57. if ($diff->invert && $diff->days == 1)
  58. return $this->period($d, "yesterday");
  59. elseif (!$diff->invert && $diff->days == 1)
  60. return $this->period($d, "tomorrow");
  61. elseif ($diff->days == 0)
  62. return $this->period($d, "today");
  63. return sprintf("the %s at %s of %s %y ",
  64. $this->period($d),
  65. $d->format('dd'), $d->format('m'), $d->format('YY')
  66. );
  67. }
  68. public function period(\DateTimeInterface $d, ?string $when = null): string {
  69. $hour = (int)$d->format('H');
  70. if ($hour >= 6 && $hour <= 11)
  71. return ($when == "today" ? "this" : $when ) . " morning";
  72. elseif ($hour == 12)
  73. return "at noon";
  74. elseif ($hour > 12 && $hour < 18)
  75. return ($when == "today" ? "this" : $when) . " afternoon";
  76. elseif ($hour >= 18 && $hour < 24)
  77. return ($when == "today" ? "tonight" : $when . " evening");
  78. elseif ($hour == 0)
  79. return ($when == "tomorrow" ? "at" : ($when == "today" ? "last" : $when)) . " midnight";
  80. else
  81. return ($when == "tomorrow" ? "this" : ($when == "today" ? "last" : $when)) . " night";
  82. }
  83. public function since(\DateTimeInterface $d): string {
  84. }
  85. }
  86. $url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
  87. echo '<pre>';
  88. $forecast = new Forecast($url);
  89. $range = $forecast->getTabular()->getBetween(
  90. $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
  91. );
  92. $tf = new TextForcast($range);
  93. echo $tf->json();
  94. ?>