You will only see the public (readable) repos here until you login.
Browse Source

Add TimeInterface and make new Variation(s) in filter

Joachim M. Giæver 1 year ago
parent
commit
aeb334ff0c

+ 1 - 1
index.php

@@ -92,7 +92,7 @@ class TextForcast {
         elseif ($hour >= 18 && $hour < 24)
             return ($when == "today" ? "tonight" : $when . " evening");
         elseif ($hour == 0)
-            return ($when == "tomorrow" ? "at" : ($when == "today" ? "last" : $when)) . " midning";
+            return ($when == "tomorrow" ? "at" : ($when == "today" ? "last" : $when)) . " midnight";
         else
             return ($when == "tomorrow" ? "this" : ($when == "today" ? "last" : $when)) . " night";
     }

+ 1 - 1
src/Yr/Forecast/Tabular/Time.php

@@ -14,7 +14,7 @@ use App\Yr\Forecast\Tabular\Time\WindSpeed;
  * @author Joachim M. Giæver (joachim[]giaever.org)
  */
 
-final class Time implements \IteratorAggregate {
+final class Time implements TimeInterface, \IteratorAggregate {
 
     private $from;
     private $until;

+ 11 - 0
src/Yr/Forecast/Tabular/TimeInterface.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Yr\Forecast\Tabular;
+
+interface TimeInterface {
+    function getFrom(): \DateTimeImmutable;
+    function getUntil(): \DateTimeImmutable;
+    function getIterator(): \Generator;
+}
+
+?>

+ 12 - 3
src/Yr/Forecast/Tabular/Variation/Variation.php

@@ -4,6 +4,7 @@ namespace App\Yr\Forecast\Tabular\Variation;
 
 use App\Yr\Forecast\Tabular\Time;
 use App\Yr\Forecast\Tabular\Time\DiffInterface;
+use App\Yr\Forecast\Tabular\TimeInterface;
 
 if (!function_exists('array_key_last')) {
     function array_key_last(array $args): ?int {
@@ -28,7 +29,7 @@ class VariationFn {
     }
 }
 
-class Variation implements \IteratorAggregate {
+class Variation implements TimeInterface, \IteratorAggregate {
 
     private $time;
     private $entities = [];
@@ -54,6 +55,14 @@ class Variation implements \IteratorAggregate {
         return $this->time;
     }
 
+    public function getFrom(): \DateTimeImmutable {
+        return $this->time->getFrom();
+    }
+
+    public function getUntil(): \DateTimeImmutable {
+        return $this->time->getUntil();
+    }
+
     private function getEntityKey(DiffInterface $entity): ?int {
         $key = (function() use ($entity) : ?int {
             foreach ($this->entities as $key => $ent)
@@ -115,7 +124,7 @@ class Variation implements \IteratorAggregate {
         $var = new Variation($this->time);
 
         foreach ($entities as $entity)
-            $var->addEntity($entity, $this->getIntersect($entity));
+            $var->addEntity($entity, null);
 
         return $var;
     }
@@ -137,7 +146,7 @@ class Variation implements \IteratorAggregate {
         return null;
     }
 
-    public function getIterator(): ?\Generator {
+    public function getIterator(): \Generator {
         foreach ($this->entities as $entity)
             yield $entity;
 

+ 21 - 5
src/Yr/Forecast/Tabular/Variations.php

@@ -11,7 +11,7 @@ use App\Yr\Forecast\Tabular\Variation\Variation;
  *
  * @author Joachim M. Giæver (joachim[]giaever.org)
  */
-class Variations implements \IteratorAggregate {
+class Variations implements TimeInterface, \IteratorAggregate {
 
     private $time;
     private $data = [];
@@ -34,7 +34,7 @@ class Variations implements \IteratorAggregate {
         $this->data[] = $var;
 
         foreach ($t as $time) {
-            $var = new Variation($time);
+            $var = new Variation($time instanceof Variation ? $time->getTime() : $time);
 
             foreach($time as $entity)
                 $this->match($var, $entity);
@@ -63,6 +63,19 @@ class Variations implements \IteratorAggregate {
         return $this->time;
     }
 
+    public function getFrom(): \DateTimeImmutable {
+        return $this->time->getFrom();
+    }
+
+    public function getUntil(): \DateTimeImmutable {
+        $last = array_shift(array_reverse($this->data));
+
+        if ($last)
+            return $last->getUntil();
+
+        return $this->time->getUntil();
+    }
+
     /**
      * Filter on types, example usage
      * ```
@@ -73,12 +86,15 @@ class Variations implements \IteratorAggregate {
      *
      * @return \Generator
      */
-    public function filter(callable $filterFn): \Generator {
+    public function filter(callable $filterFn): Variations {
+
+        $time = [$this->getTime()];
+
         foreach ($this as $data)
             if (($match = $data->filter($filterFn)) != null)
-                yield $match;
+                $time[] = $match;
 
-        return null;
+        return new Variations($time);
     }
 
     /**