Tabular.php 3.1 KB

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