123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Yr\Forecast;
- use App\Yr\Forecast\Tabular\Statistics;
- use App\Yr\Forecast\Tabular\Time;
- use App\Yr\Forecast\Tabular\Variations;
- /**
- * Holds the forecast data in Time-objects.
- *
- * Use the ```getBetween``` option to limit the results.
- *
- * Class also makes a simple statistic on the forecast
- * for the periode and a variation that will exclude
- * repetitive forecast data.
- *
- * @author Joachim M. Giæver (joachim[]giaver.org)
- */
- class Tabular implements \IteratorAggregate {
- private $time = [];
- private $stats;
- /**
- * @param \SimpleXMLElement $xml The xml part holding the time objects, can be null
- */
- public function __construct(?\SimpleXMLElement $xml) {
- $this->stats = new Statistics();
- if ($xml != null)
- foreach ($xml->time as $time)
- $this->addTime(new Time($time));
- }
- /**
- * Add a Time-object to the tabular
- *
- * @return Tabular
- */
- protected function addTime(Time $time): self {
- $this->time[] = $time;
- $this->stats->analyse($time);
- return $this;
- }
- /**
- * Get statistics for the Time-object collection
- *
- * @return Statistics
- */
- public function getStatistics(): Statistics {
- return $this->stats;
- }
- /**
- * Remove superfluous weather data.
- *
- * Checks if the data in the Time-object differs from
- * the current Time-object and returns the unique data
- *
- * @return Variations
- */
- public function getVariations(): Variations {
- return new Variations($this->time);
- }
- /**
- * Filter data between a certain periode, e.g
- * ```
- * $forcast->between(
- * $forcast->getSunset(),
- * $forecast->getSunrise()->add(
- * new \DateInterval('P1D')
- * )
- * );
- * ```
- * to only show from sunset today unti sunrise tomorrow
- *
- * @return Tabular with new collection
- */
- public function getBetween(\DateTimeInterface $from, \DateTimeInterface $until): self {
- $n = new Tabular(null);
- foreach ($this as $time)
- if ($time->getFrom() >= $from && $time->getUntil() <= $until)
- $n->addTime($time);
- return $n;
- }
- /**
- * {@inheritDoc}
- */
- public function getIterator(): \Generator {
- foreach ($this->time as $time)
- yield $time;
- }
- }
|