|
@@ -1,41 +1,121 @@
|
|
|
<?php
|
|
|
|
|
|
+ini_set('display_errors', 1);
|
|
|
+ini_set('display_startup_errors', 1);
|
|
|
+error_reporting(E_ALL);
|
|
|
+
|
|
|
+use App\Yr\Forecast;
|
|
|
+use App\Yr\Forecast\Tabular;
|
|
|
+use App\Yr\Forecast\Tabular\Time\Symbol;
|
|
|
+use App\Yr\Forecast\Tabular\Time\Temperature;
|
|
|
+use App\Yr\Forecast\Tabular\Time\WindDirection;
|
|
|
+use App\Yr\Forecast\Tabular\Time\WindSpeed;
|
|
|
+
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
-$url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
|
|
|
+class TextForcast {
|
|
|
|
|
|
-$forecast = new App\Yr\Forecast($url);
|
|
|
+ private $forecast;
|
|
|
|
|
|
-$range = $forecast->getTabular()->getBetween(
|
|
|
- $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
|
|
|
-);
|
|
|
+ public function __construct(Tabular $t) {
|
|
|
+ $this->forecast = $t;
|
|
|
+ }
|
|
|
|
|
|
-echo '<pre>';
|
|
|
-echo sprintf("Forecast from %s until %s, updated %s:\n",
|
|
|
- $range->getFrom()->format('d.m H:i'),
|
|
|
- $range->getUntil()->format('d.m H:i'),
|
|
|
- $forecast->getLastUpdate()->format('H:i')
|
|
|
-);
|
|
|
-foreach ($range->getVariations()->getData() as $data) {
|
|
|
- echo $data['time']->getFrom()->format("H:i");
|
|
|
+
|
|
|
+ * «Tonight Southeasterly light breeze, cloudy, temperature -8 to -9 degrees»
|
|
|
+ * @author Helge Tangen
|
|
|
+ */
|
|
|
+ public function start(): string {
|
|
|
+ $time = $this->forecast->getVariations()->getTime();
|
|
|
+ return sprintf(
|
|
|
+ "%s from %s (%s) %s",
|
|
|
+ $this->when($time->getFrom()),
|
|
|
+ $time->getFrom()->format('H'),
|
|
|
+ $time->getFrom()->format('h a'),
|
|
|
+ (function() use ($time){
|
|
|
+ $str = [];
|
|
|
+
|
|
|
+ $prev = null;
|
|
|
+ foreach ($time as $data) {
|
|
|
+ switch (get_class($data)) {
|
|
|
+ case Symbol::class:
|
|
|
+ $str[] = ($prev == null ? ' ' : ', ') . $data->getName();
|
|
|
+ break;
|
|
|
+ case WindSpeed::class:
|
|
|
+ $str[] = ($prev == null || WindSpeed::class ? " " : ", ") . $data->getName();
|
|
|
+ break;
|
|
|
+ case WindDirection::class:
|
|
|
+ $str[] = ($prev == null ? ' ' : ', ') . $data->getName();
|
|
|
+ break;
|
|
|
+ case Temperature::class:
|
|
|
+ $str[] = ($prev == null ? ' ' : ', ') . sprintf('%d %s', $data->getValue(), $data->getDegree());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+
|
|
|
+ }
|
|
|
+ $prev = $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ $last = array_pop($str);
|
|
|
+
|
|
|
+ if (substr($last, 0, 1) == ',')
|
|
|
+ $last = substr($last, 1);
|
|
|
+
|
|
|
+ if (sizeof($str) == 0)
|
|
|
+ return strtolower($last);
|
|
|
+
|
|
|
+ return strtolower(sprintf(
|
|
|
+ "%s and %s",
|
|
|
+ join($str), $last
|
|
|
+ ));
|
|
|
+ })()
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function when(\DateTimeInterface $d): string {
|
|
|
+ $diff = $d->diff(new \DateTime());
|
|
|
+
|
|
|
+ if ($diff->invert && $diff->days == 1)
|
|
|
+ return sprintf("yesterday %s", $this->period($d));
|
|
|
+ elseif (!$diff->invert && $diff->days == 1)
|
|
|
+ return sprintf("tomorrow %s", $this->period($d));
|
|
|
+ elseif ($diff->days == 0)
|
|
|
+ return sprintf("This %s", $this->period($d));
|
|
|
+
|
|
|
+ return sprintf("the %s at %s of %s %y ",
|
|
|
+ $this->period($d),
|
|
|
+ $d->format('dd'), $d->format('m'), $d->format('YY')
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function period(\DateTimeInterface $d): string {
|
|
|
+ $hour = (int)$d->format('H');
|
|
|
+ if ($hour >= 6 && $hour <= 11)
|
|
|
+ return "morning";
|
|
|
+ elseif ($hour == 12)
|
|
|
+ return "noon";
|
|
|
+ elseif ($hour > 12 && $hour < 18)
|
|
|
+ return "afternoon";
|
|
|
+ elseif ($hour >= 18 && $hour < 24)
|
|
|
+ return "evening";
|
|
|
+ elseif ($hour == 12)
|
|
|
+ return "midning";
|
|
|
+ else
|
|
|
+ return "night";
|
|
|
+ }
|
|
|
+
|
|
|
+ public function since(\DateTimeInterface $d): string {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
|
|
|
-$f = current(iterator_to_array($range->getIterator()));
|
|
|
-echo $f->getWindSpeed()->convertTo('ft/s');
|
|
|
+$url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
|
|
|
|
|
|
-echo "\n";
|
|
|
-$stat = $range->getStatistics();
|
|
|
+$forecast = new Forecast($url);
|
|
|
|
|
|
-echo "\n";
|
|
|
-echo $stat->getAverageTemperature();
|
|
|
-echo "\n";
|
|
|
-echo $stat->getAverageWindSpeed();
|
|
|
+$range = $forecast->getTabular()->getBetween(
|
|
|
+ $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
|
|
|
+);
|
|
|
|
|
|
-echo "\n";
|
|
|
-var_dump($stat->getAverageSymbols());
|
|
|
-var_dump($stat->getMostCommonSymbol());
|
|
|
-echo "\n";
|
|
|
-foreach ($forecast->getCredit() as $c)
|
|
|
- echo $c . "\n";
|
|
|
+echo (new TextForcast($range))->start();
|
|
|
?>
|