12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Yr\Forecast\Tabular;
- /**
- * Removes superfluous forecast data in an Time-object
- * only storing changes.
- *
- * @author Joachim M. Giæver (joachim[]giaever.org)
- */
- class Variations {
- private $data = [];
- /**
- * @param array $t Array of Time-objects
- */
- public function __construct(array $t) {
- if (!current($t) instanceof Time)
- return;
- foreach ($t as $time)
- $this->determineVariance($time);
- }
- private function determineVariance(Time $t) {
- $var = [
- 'time' => $t,
- 'entities' => iterator_to_array($t->getIterator())
- ];
- if (empty($this->data))
- $this->data[] = $var;
- else {
- foreach (array_reverse($this->data) as $entry) {
- foreach ($entry['entities'] as $entity) {
- foreach ($var['entities'] as $key => $ventity)
- if ($ventity instanceof $entity && !$ventity->diff($entity))
- unset($var['entities'][$key]);
- }
- }
- if (!empty($var['entities']))
- $this->data[] = $var;
- }
- }
- /**
- * Returns the changes in the forecast
- *
- * @return array
- */
- public function getData(callable $filterFn = null): array {
- return $this->data;
- }
- }
|