Tabular.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Yr\Forecast;
  3. use App\Yr\Forecast;
  4. use App\Yr\Forecast\Tabular\Statistics;
  5. use App\Yr\Forecast\Tabular\Time;
  6. use App\Yr\Forecast\Tabular\Variations;
  7. /**
  8. * Holds the forecast data in Time-objects.
  9. *
  10. * Use the ```getBetween``` option to limit the results.
  11. *
  12. * Class also makes a simple statistic on the forecast
  13. * for the periode and a variation that will exclude
  14. * repetitive forecast data.
  15. *
  16. * @author Joachim M. Giæver (joachim[]giaver.org)
  17. */
  18. class Tabular implements \IteratorAggregate {
  19. private $time = [];
  20. private $stats;
  21. private $variations;
  22. private $forecast;
  23. /**
  24. * @param \SimpleXMLElement $xml The xml part holding the time objects, can be null
  25. */
  26. public function __construct($forecast, ?\SimpleXMLElement $xml) {
  27. $this->forecast = $forecast;
  28. $this->stats = new Statistics();
  29. if ($xml != null) {
  30. foreach ($xml->time as $time)
  31. $this->addTime(new Time($time));
  32. $this->makeVariations($time);
  33. }
  34. }
  35. public function getForecast(): Forecast {
  36. return $this->forecast;
  37. }
  38. /**
  39. * Add a Time-object to the tabular
  40. *
  41. * @return Tabular
  42. */
  43. protected function addTime(Time $time): self {
  44. $this->time[] = $time;
  45. $this->stats->analyse($time);
  46. return $this;
  47. }
  48. /**
  49. * Get statistics for the Time-object collection
  50. *
  51. * @return Statistics
  52. */
  53. public function getStatistics(): Statistics {
  54. return $this->stats;
  55. }
  56. protected function makeVariations(): self {
  57. $this->variations = new Variations($this->time);
  58. return $this;
  59. }
  60. /**
  61. * Remove superfluous weather data.
  62. *
  63. * Checks if the data in the Time-object differs from
  64. * the current Time-object and returns the unique data
  65. *
  66. * @return Variations
  67. */
  68. public function getVariations(): Variations {
  69. return $this->variations;
  70. }
  71. /**
  72. * Filter data between a certain periode, e.g
  73. * ```
  74. * $forcast->between(
  75. * $forcast->getSunset(),
  76. * $forecast->getSunrise()->add(
  77. * new \DateInterval('P1D')
  78. * )
  79. * );
  80. * ```
  81. * to only show from sunset today unti sunrise tomorrow
  82. *
  83. * @return Tabular with new collection
  84. */
  85. public function getBetween(\DateTimeInterface $from, \DateTimeInterface $until): self {
  86. $n = new Tabular($this->forecast, null);
  87. foreach ($this as $time)
  88. if ($time->getFrom() >= $from && $time->getUntil() <= $until)
  89. $n->addTime($time);
  90. $n->makeVariations();
  91. return $n;
  92. }
  93. /**
  94. * Return the time this tabular is from
  95. *
  96. * @return \DateTimeInterface The from time
  97. */
  98. public function getFrom(): \DateTimeInterface {
  99. return current($this->time)->getFrom();
  100. }
  101. /**
  102. * Return the time this tabular is until
  103. *
  104. * @return \DateTimeInterface The until time
  105. */
  106. public function getUntil(): \DateTimeInterface {
  107. return current(array_reverse($this->time))->getUntil();
  108. }
  109. /**
  110. * {@inheritDoc}
  111. */
  112. public function getIterator(): ?\Generator {
  113. foreach ($this->time as $time)
  114. yield $time;
  115. return null;
  116. }
  117. }