|null $selection Variantenschlüssel, die für diese Fassung gelten; null = alle * @return list */ public function expand(ContentElementVersion|CompositionVersion $node, string $path = '', array $stack = [], ?array $selection = null): array { if ($node instanceof ContentElementVersion) { $node->loadMissing('element'); return [[ 'version_id' => $node->id, 'role' => $path, 'path' => $path !== '' ? $path : $node->element->working_title, 'requirement' => CompositionMember::REQUIRED, 'variant' => null, ], ...$this->embeddedImages($node, $path !== '' ? $path : $node->element->working_title, null)]; } if (in_array($node->composition_id, $stack, true)) { throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.'); } $stack[] = $node->composition_id; $rows = []; foreach ($node->members()->with('variant')->orderBy('position')->get() as $member) { $variantKey = $member->variant?->key; if ($variantKey !== null && $selection !== null && ! in_array($variantKey, $selection, true)) { continue; } $segment = $path === '' ? $member->role : $path.' / '.$member->role; if ($member->content_element_version_id !== null) { $rows[] = [ 'version_id' => $member->content_element_version_id, 'role' => $member->role, 'path' => $segment, 'requirement' => $member->requirement ?: CompositionMember::REQUIRED, 'variant' => $variantKey, ]; foreach ($this->embeddedImages($member->contentElementVersion, $segment, $variantKey) as $image) { $rows[] = $image; } continue; } $nested = $member->nestedCompositionVersion; if ($nested === null) { throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.'); } // Bestandteil einer Art, die dieser Programmstand nicht kennt: nicht auflösen (Rückfallschutz). // Veröffentlichen verweigern die aufrufenden Stellen über ForeignCompositions. $nested->loadMissing('composition'); if ($nested->composition === null) { continue; } $rows = array_merge($rows, $this->expand( $nested, $segment, $stack, is_array($member->variant_selection) ? array_values(array_map('strval', $member->variant_selection)) : null, )); } return $rows; } /** * Bilder, auf die ein Text verweist, gehören zum Inhalt des Stands: Nur was im Stand steht, darf ein Mitglied sehen * (`DecideAccess`), und eine Sperre des Bildes wirkt über denselben Weg. Ein fehlendes Bild macht den Text nicht * unbrauchbar, daher „ergänzend“. * * @return list */ private function embeddedImages(?ContentElementVersion $text, string $path, ?string $variantKey): array { if ($text === null || trim((string) $text->body) === '' || ! str_contains((string) $text->body, 'img')) { return []; } $rows = []; foreach (FormattedText::imageIds($text->body) as $id) { $rows[] = [ 'version_id' => $id, 'role' => 'Bild', 'path' => $path.' / Bild', 'requirement' => CompositionMember::SUPPLEMENTARY, 'variant' => $variantKey, ]; } return $rows; } public function resolvedHash(ContentElementVersion|CompositionVersion $node): string { $ids = array_values(array_unique(array_column($this->expand($node), 'version_id'))); sort($ids); return hash('sha256', json_encode($ids, JSON_THROW_ON_ERROR)); } }