1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00
Arsse/lib/Context/UnionContext.php

52 lines
1.3 KiB
PHP
Raw Normal View History

2022-04-29 02:32:10 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2022-04-29 02:32:10 +00:00
namespace JKingWeb\Arsse\Context;
class UnionContext extends RootContext implements \ArrayAccess, \Countable, \IteratorAggregate {
protected $contexts = [];
2022-04-30 17:50:35 +00:00
#[\ReturnTypeWillChange]
public function offsetExists($offset) {
2022-04-29 20:35:46 +00:00
return isset($this->contexts[$offset]);
2022-04-29 02:32:10 +00:00
}
2022-04-30 17:50:35 +00:00
#[\ReturnTypeWillChange]
public function offsetGet($offset) {
2022-04-29 02:32:10 +00:00
return $this->contexts[$offset] ?? null;
}
2022-04-30 17:50:35 +00:00
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value) {
2022-04-29 20:35:46 +00:00
assert($value instanceof RootContext, new \Exception("Union contexts may only contain other non-exclusion contexts"));
if (isset($offset)) {
$this->contexts[$offset] = $value;
} else {
$this->contexts[] = $value;
}
2022-04-29 02:32:10 +00:00
}
2022-04-30 17:50:35 +00:00
#[\ReturnTypeWillChange]
public function offsetUnset($offset) {
2022-04-29 02:32:10 +00:00
unset($this->contexts[$offset]);
}
public function count(): int {
return count($this->contexts);
}
public function getIterator(): \Traversable {
foreach ($this->contexts as $k => $c) {
yield $k => $c;
}
}
2022-04-29 20:35:46 +00:00
public function __construct(RootContext ...$context) {
2022-04-29 02:32:10 +00:00
$this->contexts = $context;
}
}