123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?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';
- class TextForcast {
- private $forecast;
- public function __construct(Tabular $t) {
- $this->forecast = $t;
- }
- /**
- * «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:
- $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
- ));
- })()
- );
- }
- 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 {
-
- }
- }
- $url = 'https://www.yr.no/place/Norway/Troms/Tromsø/Tromsø/forecast_hour_by_hour.xml';
- $forecast = new Forecast($url);
- $range = $forecast->getTabular()->getBetween(
- $forecast->getSunset(), $forecast->getSunrise()->add(new \DateInterval('P1D'))
- );
- echo (new TextForcast($range))->start();
- ?>
|