diff --git a/src/Entity/AbstractContent.php b/src/Entity/AbstractContent.php index bb3bbe3..8ac08ee 100644 --- a/src/Entity/AbstractContent.php +++ b/src/Entity/AbstractContent.php @@ -268,10 +268,10 @@ public function addAncestor(int $id): self /** * @param mixed[] $data - * @return AbstractContent|ContentPage|ContentComment + * @return AbstractContent|ContentPage|ContentComment|ContentAttachment * @throws HydrationException */ - public static function load(array $data): ContentComment|AbstractContent|ContentPage + public static function load(array $data): ContentComment|AbstractContent|ContentPage|ContentAttachment { /* handle older content versions */ if(isset($data['content'], $data['when'])) { @@ -287,6 +287,7 @@ public static function load(array $data): ContentComment|AbstractContent|Content $content = match ($data['type']) { Content::CONTENT_TYPE_PAGE => new ContentPage(), Content::CONTENT_TYPE_COMMENT => new ContentComment(), + Content::CONTENT_TYPE_ATTACHMENT => new ContentAttachment(), default => throw new HydrationException('Invalid content type: ' . $data['type']), }; diff --git a/src/Entity/ContentAttachment.php b/src/Entity/ContentAttachment.php new file mode 100644 index 0000000..ccdf8d1 --- /dev/null +++ b/src/Entity/ContentAttachment.php @@ -0,0 +1,13 @@ +getId()); } + public function testLoadAttachment(): void + { + $attachment = AbstractContent::load(self::attachmentPayload()); + + self::assertInstanceOf(ContentAttachment::class, $attachment); + self::assertSame('attachment', $attachment->getType()); + } + + public function testSearchResultLoadsAttachment(): void + { + $searchResult = ContentSearchResult::load([ + 'size' => 1, + 'results' => [self::attachmentPayload()], + ]); + + self::assertInstanceOf(ContentAttachment::class, $searchResult->getResultAt(0)); + } + + /** + * @return mixed[] + */ + private static function attachmentPayload(): array + { + return [ + 'id' => '123456', + 'type' => 'attachment', + 'title' => 'example.pdf', + '_links' => [ + 'self' => 'https://example.test/rest/api/content/123456', + ], + ]; + } + }