123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Yr\Forecast\Tabular\Variation;
- use App\Yr\Forecast\Tabular\Time;
- use App\Yr\Forecast\Tabular\Time\DiffInterface;
- if (!function_exists('array_key_last')) {
- function array_key_last(array $args): ?int {
- $keys = array_keys($args);
- return array_pop($keys);
- }
- }
- class Variation implements \IteratorAggregate {
- private $time;
- private $entities = [];
- private $intersects = [];
- public function __construct(Time $t) {
- $this->time = $t;
- }
- public function addEntity(DiffInterface $entity, ?DiffInterface $intersects): self {
- $this->entities[] = $entity;
- if ($intersects != null)
- $this->intersects[array_key_last($this->entities)] = $intersects;
- return $this;
- }
- public function getTime(): Time {
- return $this->time;
- }
- public function getIntersection(DiffInterface $entity): ?DiffInterface {
- $key = (function() use ($entity) : int {
- foreach ($this->entities as $key => $ent)
- if ($ent == $entity)
- return $key;
- })();
- if (isset($this->intersects[$key]))
- return $this->intersects[$key];
- return null;
- }
- public function isEmpty(): bool {
- return empty($this->entities);
- }
- public function getIterator(): \Generator {
- foreach ($this->entities as $entity)
- yield $entity;
- }
- }
|