Tabular.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Yr\Forecast;
  3. use App\Yr\Forecast\Tabular\Statistics;
  4. use App\Yr\Forecast\Tabular\Time;
  5. class Tabular implements \IteratorAggregate {
  6. private $time = [];
  7. private $stats;
  8. public function __construct(?\SimpleXMLElement $xml) {
  9. $this->stats = new Statistics();
  10. if ($xml != null)
  11. foreach ($xml->time as $time)
  12. $this->addTime(new Time($time));
  13. }
  14. protected function addTime(Time $time): self {
  15. $this->time[] = $time;
  16. $this->stats->analyse($time);
  17. return $this;
  18. }
  19. public function getStatistics(): array {
  20. return $this->stats;
  21. }
  22. public function getBetween(\DateTimeInterface $from, \DateTimeInterface $until): self {
  23. $n = new Tabular(null);
  24. foreach ($this as $time)
  25. if ($time->getFrom() >= $from && $time->getUntil() <= $until)
  26. $n->addTime($time);
  27. return $n;
  28. }
  29. public function getIterator(): \Generator {
  30. foreach ($this->time as $time)
  31. yield $time;
  32. }
  33. }