Variations.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Yr\Forecast\Tabular;
  3. /**
  4. * Removes superfluous forecast data in an Time-object
  5. * only storing changes.
  6. *
  7. * @author Joachim M. Giæver (joachim[]giaever.org)
  8. */
  9. class Variations {
  10. private $data = [];
  11. /**
  12. * @param array $t Array of Time-objects
  13. */
  14. public function __construct(array $t) {
  15. if (!current($t) instanceof Time)
  16. return;
  17. foreach ($t as $time)
  18. $this->determineVariance($time);
  19. }
  20. private function determineVariance(Time $t) {
  21. $var = [
  22. 'time' => $t,
  23. 'entities' => iterator_to_array($t->getIterator())
  24. ];
  25. if (empty($this->data))
  26. $this->data[] = $var;
  27. else {
  28. foreach (array_reverse($this->data) as $entry) {
  29. foreach ($entry['entities'] as $entity) {
  30. foreach ($var['entities'] as $key => $ventity)
  31. if ($ventity instanceof $entity && !$ventity->diff($entity))
  32. unset($var['entities'][$key]);
  33. }
  34. }
  35. if (!empty($var['entities']))
  36. $this->data[] = $var;
  37. }
  38. }
  39. /**
  40. * Returns the changes in the forecast
  41. *
  42. * @return array
  43. */
  44. public function getData(callable $filterFn = null): array {
  45. return $this->data;
  46. }
  47. }