*/ final readonly class ContentAreaCollection implements ContainerInterface, \IteratorAggregate { public function __construct( /** @var ContentAreaClosure[]|ContentArea[] $contentAreas */ private array $contentAreas, private ?ServerRequestInterface $request = null, ) {} public function withRequest(ServerRequestInterface $request): self { return new self($this->contentAreas, $request); } public function get(string $id): ContentArea { if (!$this->has($id)) { throw new ContentAreaNotFoundException('No content area found for identifier: ' . $id, 1726479567); } $area = $this->contentAreas[$id]; if ($area instanceof ContentAreaClosure) { if ($this->request === null) { throw new \LogicException('Cannot instantiate ContentAreaClosure without a request. Call withRequest() first.', 1776158770); } return $area->instantiate($this->request); } return $area; } public function has(string $id): bool { return array_key_exists($id, $this->contentAreas); } /** * @internal Only for AfterContentHasBeenFetchedEvent */ public function getGroupedRecords(ServerRequestInterface $request): array { $areas = []; foreach ($this->contentAreas as $area) { $area = $area instanceof ContentAreaClosure ? $area->instantiate($request) : $area; $areas[$area->getIdentifier()] = [ 'name' => $area->getName(), 'colPos' => $area->getColPos(), 'identifier' => $area->getIdentifier(), 'allowedContentTypes' => $area->getAllowedContentTypes(), 'records' => $area->getRecords(), 'area' => $area, ]; } return $areas; } /** * @internal Only for AfterContentHasBeenFetchedEvent */ public function withUpdatedRecords(array $groupedRecords): self { $areas = []; foreach ($groupedRecords as $identifier => $data) { $areas[$identifier] = $data['area']->withRecords($data['records']); } return new self($areas, $this->request); } /** * @return \Traversable */ public function getIterator(): \Traversable { $result = []; foreach (array_keys($this->contentAreas) as $key) { $result[$key] = $this->get($key); } return new \ArrayIterator($result); } }