|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Storyblok\ManagementApi\Data; |
| 6 | + |
| 7 | +use Storyblok\ManagementApi\Exceptions\StoryblokFormatException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Represents a lightweight story item as returned by the Storyblok "multiple stories" API endpoint. |
| 11 | + * |
| 12 | + * Unlike the full `Story` object returned by the single story endpoint, this class does not contain |
| 13 | + * the actual content of the story. Instead, it holds metadata and system-related information such as |
| 14 | + * the story's name, slug, ID, UUID, and timestamps for creation, update, and publication. |
| 15 | + * |
| 16 | + * `StoryCollectionItem` is typically used when retrieving a list of stories, for purposes like displaying |
| 17 | + * overviews or selecting stories for further processing. Since it does not support content data or modification |
| 18 | + * operations, it is read-only in nature and optimized for listing or filtering operations. |
| 19 | + * |
| 20 | + * For creating or updating stories, or when full content access is required, the full `Story` class should be used instead. |
| 21 | + */ |
| 22 | +class StoryCollectionItem extends BaseData |
| 23 | +{ |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + $this->data = []; |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param mixed[] $data |
| 32 | + * @throws StoryblokFormatException |
| 33 | + */ |
| 34 | + public static function make(array $data = []): self |
| 35 | + { |
| 36 | + $dataObject = new StoryblokData($data); |
| 37 | + if (!($dataObject->hasKey('name') && $dataObject->hasKey('slug'))) { |
| 38 | + // is not valid |
| 39 | + } |
| 40 | + |
| 41 | + $storyItem = new StoryCollectionItem(); |
| 42 | + $storyItem->setData($dataObject->toArray()); |
| 43 | + // validate |
| 44 | + if (! $storyItem->isValid()) { |
| 45 | + throw new StoryblokFormatException("Story is not valid"); |
| 46 | + } |
| 47 | + |
| 48 | + return $storyItem; |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public function slug(): string |
| 53 | + { |
| 54 | + return $this->getString('slug'); |
| 55 | + } |
| 56 | + |
| 57 | + public function name(): string |
| 58 | + { |
| 59 | + return $this->getString('name'); |
| 60 | + } |
| 61 | + |
| 62 | + public function createdAt(string $format = "Y-m-d"): null|string |
| 63 | + { |
| 64 | + return $this->getFormattedDateTime('created_at', "", format: $format); |
| 65 | + } |
| 66 | + |
| 67 | + public function publishedAt(string $format = "Y-m-d"): null|string |
| 68 | + { |
| 69 | + return $this->getFormattedDateTime('published_at', "", format: $format); |
| 70 | + } |
| 71 | + |
| 72 | + public function updatedAt(): null|string |
| 73 | + { |
| 74 | + return $this->getFormattedDateTime('updated_at', "", format: "Y-m-d"); |
| 75 | + } |
| 76 | + |
| 77 | + public function id(): string |
| 78 | + { |
| 79 | + return $this->getString('id'); |
| 80 | + } |
| 81 | + |
| 82 | + public function uuid(): string |
| 83 | + { |
| 84 | + return $this->getString('uuid'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Validates if the story data contains all required fields and valid values |
| 89 | + */ |
| 90 | + public function isValid(): bool |
| 91 | + { |
| 92 | + if (!$this->hasKey('name') || in_array($this->getString('name'), ['', '0'], true)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + return $this->hasKey('slug') && !in_array($this->getString('slug'), ['', '0'], true); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Set tags for Story, from a `Tags` collection |
| 101 | + * @return $this |
| 102 | + */ |
| 103 | + public function setTags(Tags $tags): self |
| 104 | + { |
| 105 | + |
| 106 | + return $this->setTagsFromArray($tags->getTagsArray()); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Set tags for Story, from a string of arrays like ["tag1", "tag2"] |
| 112 | + * @param string[] $tagsArray |
| 113 | + * @return $this |
| 114 | + */ |
| 115 | + public function setTagsFromArray(array $tagsArray): self |
| 116 | + { |
| 117 | + $this->set("tag_list", $tagsArray); |
| 118 | + return $this; |
| 119 | + } |
| 120 | +} |
0 commit comments