|
@@ -6,6 +6,7 @@ error_reporting(E_ALL);
|
|
|
|
|
|
use App\Yr\Forecast;
|
|
|
use App\Yr\Forecast\Tabular;
|
|
|
+use App\Yr\Forecast\Tabular\Time\AbstractUnit;
|
|
|
use App\Yr\Forecast\Tabular\Time\Symbol;
|
|
|
use App\Yr\Forecast\Tabular\Time\Temperature;
|
|
|
use App\Yr\Forecast\Tabular\Time\WindDirection;
|
|
@@ -14,6 +15,7 @@ use App\Yr\Forecast\Tabular\Time\WindSpeed;
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
class TextForcast {
|
|
|
+ const HOUR_FORMAT = 'H (h a)';
|
|
|
|
|
|
private $forecast;
|
|
|
|
|
@@ -25,63 +27,53 @@ class TextForcast {
|
|
|
* «Tonight Southeasterly light breeze, cloudy, temperature -8 to -9 degrees»
|
|
|
* @author Helge Tangen
|
|
|
*/
|
|
|
- public function start(): string {
|
|
|
+ public function json(): 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:
|
|
|
- $data = $data->convertTo(Temperature::UNIT_KELVIN);
|
|
|
- $str[] = ($prev == null ? ' ' : ', ') . sprintf('%d %s', $data->getValue(), $data->getDegree());
|
|
|
- break;
|
|
|
- default:
|
|
|
- //$str []= '``' . get_class($data) . '`` missing';
|
|
|
- }
|
|
|
- $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
|
|
|
- ));
|
|
|
- })()
|
|
|
- );
|
|
|
+
|
|
|
+ $arr = [
|
|
|
+ "from" => $this->forecast->getFrom()->format('Y-m-d H:i'),
|
|
|
+ "until" => $this->forecast->getUntil()->format("Y-m-d H:i"),
|
|
|
+ "initial" => [],
|
|
|
+ "changes" => [],
|
|
|
+ "credit" => $this->forecast->getForecast()->getCredit()
|
|
|
+ ];
|
|
|
+ foreach ($time as $ent) {
|
|
|
+ $arr['initial'][] = (string)$ent;
|
|
|
+ }
|
|
|
+
|
|
|
+ $vars = $this->forecast->getVariations()->filter(function($e, $p, $n) {
|
|
|
+ return ($p == null ? true : $e->thresholdDiff($p->getEntity(get_class($e))));
|
|
|
+ });
|
|
|
+
|
|
|
+ foreach ($vars as $var) {
|
|
|
+ $changes = [
|
|
|
+ "at" => [
|
|
|
+ "time" => $var->getTime()->getFrom()->format('Y-m-d H:i'),
|
|
|
+ "period" => $this->when($var->getTime()->getFrom())
|
|
|
+ ],
|
|
|
+ "to" => [],
|
|
|
+ ];
|
|
|
+ $var->operate(function($e, $p, $n) use (&$changes) {
|
|
|
+ $changes['to'][] = (string)$e;
|
|
|
+ });
|
|
|
+
|
|
|
+ $arr['changes'][] = $changes;
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_encode($arr);
|
|
|
}
|
|
|
|
|
|
public function when(\DateTimeInterface $d): string {
|
|
|
- $diff = $d->diff(new \DateTime());
|
|
|
+ $a = DateTime::createFromFormat('d/m/Y', $d->format('d/m/Y'));
|
|
|
+ $b = DateTime::createFromFormat('d/m/Y', (new \DateTime())->format('d/m/Y'));
|
|
|
+ $diff = $b->diff($a);
|
|
|
|
|
|
if ($diff->invert && $diff->days == 1)
|
|
|
- return sprintf("yesterday %s", $this->period($d));
|
|
|
+ return $this->period($d, "yesterday");
|
|
|
elseif (!$diff->invert && $diff->days == 1)
|
|
|
- return sprintf("tomorrow %s", $this->period($d));
|
|
|
+ return $this->period($d, "tomorrow");
|
|
|
elseif ($diff->days == 0)
|
|
|
- return sprintf("This %s", $this->period($d));
|
|
|
+ return $this->period($d, "today");
|
|
|
|
|
|
return sprintf("the %s at %s of %s %y ",
|
|
|
$this->period($d),
|
|
@@ -89,20 +81,20 @@ class TextForcast {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- public function period(\DateTimeInterface $d): string {
|
|
|
+ public function period(\DateTimeInterface $d, ?string $when = null): string {
|
|
|
$hour = (int)$d->format('H');
|
|
|
if ($hour >= 6 && $hour <= 11)
|
|
|
- return "morning";
|
|
|
+ return ($when == "today" ? "this" : $when ) . " morning";
|
|
|
elseif ($hour == 12)
|
|
|
- return "noon";
|
|
|
+ return "at noon";
|
|
|
elseif ($hour > 12 && $hour < 18)
|
|
|
- return "afternoon";
|
|
|
+ return ($when == "today" ? "this" : $when) . " afternoon";
|
|
|
elseif ($hour >= 18 && $hour < 24)
|
|
|
- return "evening";
|
|
|
- elseif ($hour == 12)
|
|
|
- return "midning";
|
|
|
+ return ($when == "today" ? "tonight" : $when . " evening");
|
|
|
+ elseif ($hour == 0)
|
|
|
+ return ($when == "tomorrow" ? "at" : ($when == "today" ? "last" : $when)) . " midning";
|
|
|
else
|
|
|
- return "night";
|
|
|
+ return ($when == "tomorrow" ? "this" : ($when == "today" ? "last" : $when)) . " night";
|
|
|
}
|
|
|
|
|
|
public function since(\DateTimeInterface $d): string {
|
|
@@ -112,11 +104,13 @@ class TextForcast {
|
|
|
|
|
|
$url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
|
|
|
|
|
|
+echo '<pre>';
|
|
|
$forecast = new Forecast($url);
|
|
|
|
|
|
$range = $forecast->getTabular()->getBetween(
|
|
|
$forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
|
|
|
);
|
|
|
|
|
|
-echo (new TextForcast($range))->start();
|
|
|
+$tf = new TextForcast($range);
|
|
|
+echo $tf->json();
|
|
|
?>
|