Variation.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Yr\Forecast\Tabular\Variation;
  3. use App\Yr\Forecast\Tabular\Time;
  4. use App\Yr\Forecast\Tabular\Time\DiffInterface;
  5. use App\Yr\Forecast\Tabular\TimeInterface;
  6. if (!function_exists('array_key_last')) {
  7. function array_key_last(array $args): ?int {
  8. $keys = array_keys($args);
  9. return array_pop($keys);
  10. }
  11. }
  12. class VariationFn {
  13. private $fn, $v;
  14. public function __construct(callable $fn, Variation $v) {
  15. $this->fn = $fn;
  16. $this->v = $v;
  17. }
  18. public function callable(DiffInterface $e): ?bool {
  19. $callable = $this->fn;
  20. return $callable(
  21. $e,
  22. $this->v->getIntersect($e),
  23. $this->v->getIntersected($e)
  24. );
  25. }
  26. }
  27. class Variation implements TimeInterface, \IteratorAggregate {
  28. private $time;
  29. private $entities = [];
  30. private $intersected = [];
  31. private $intersects = [];
  32. public function __construct(Time $t) {
  33. $this->time = $t;
  34. }
  35. public function addEntity(DiffInterface $entity, ?Variation $intersects): self {
  36. $this->entities[] = $entity;
  37. if ($intersects != null) {
  38. $this->intersects[array_key_last($this->entities)] = $intersects;
  39. $intersects->addIntersected($entity, $this);
  40. }
  41. return $this;
  42. }
  43. public function getTime(): Time {
  44. return $this->time;
  45. }
  46. public function getFrom(): \DateTimeImmutable {
  47. return $this->time->getFrom();
  48. }
  49. public function getUntil(): \DateTimeImmutable {
  50. return $this->time->getUntil();
  51. }
  52. private function getEntityKey(DiffInterface $entity): ?int {
  53. $key = (function() use ($entity) : ?int {
  54. foreach ($this->entities as $key => $ent)
  55. if ($ent instanceof $entity)
  56. return $key;
  57. return null;
  58. })();
  59. return $key;
  60. }
  61. public function getIntersect(DiffInterface $entity): ?Variation {
  62. $key = $this->getEntityKey($entity);
  63. if ($key === null || !isset($this->intersects[$key]))
  64. return null;
  65. return $this->intersects[$key];
  66. }
  67. public function getIntersected(DiffInterface $entity): ?Variation {
  68. $key = $this->getEntityKey($entity);
  69. if ($key === null || !isset($this->intersected[$key]))
  70. return null;
  71. foreach ($this->intersected[$key] as $var)
  72. if ($var->getEntity(get_class($entity)) != null)
  73. return $var;
  74. }
  75. protected function addIntersected(DiffInterface $entity, Variation $var): self {
  76. $key = $this->getEntityKey($entity);
  77. if ($key === null)
  78. return $this;
  79. if (!isset($this->intersected[$key]))
  80. $this->intersected[$key] = [$var];
  81. else
  82. $this->intersected[$key][] = $var;
  83. return $this;
  84. }
  85. public function isEmpty(): bool {
  86. return empty($this->entities);
  87. }
  88. public function filter(callable $filterFn): ?Variation {
  89. $entities = array_filter($this->entities, [
  90. new VariationFn($filterFn, $this), 'callable',
  91. ]);
  92. if (count($entities) == 0)
  93. return null;
  94. $var = new Variation($this->time);
  95. foreach ($entities as $entity)
  96. $var->addEntity($entity, null);
  97. return $var;
  98. }
  99. public function operate(callable $opFn): void {
  100. array_walk($this->entities, [
  101. new VariationFn($opFn, $this), 'callable'
  102. ]);
  103. }
  104. public function numEntities(): int {
  105. return sizeof($this->entities);
  106. }
  107. public function getEntity(string $class): ?DiffInterface {
  108. foreach ($this as $entity)
  109. if ($entity instanceof $class)
  110. return $entity;
  111. return null;
  112. }
  113. public function getIterator(): \Generator {
  114. foreach ($this->entities as $entity)
  115. yield $entity;
  116. return null;
  117. }
  118. }