Skip to content

Commit 6318fa8

Browse files
Fixing #10 (#12)
* Fixing #10 Fixing #10, `content` missing from Stories collection responses but is expected * renaming StoryItem into StoryCollectionItem * Add doc for StoryCollectionItem
1 parent a00d3f2 commit 6318fa8

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## 1.0.1 - WIP
4+
- Fixing getting list of Stories. Issue #10
45
- Adding new plans in `StoryblokUtils::getPlanDescription()` method
56
- Reviewing README
67
- Adding dependecies license checker

src/Data/Stories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Stories extends StoryblokData
1111
#[\Override]
1212
public function getDataClass(): string
1313
{
14-
return Story::class;
14+
return StoryCollectionItem::class;
1515
}
1616

1717
#[\Override]

src/Data/StoryCollectionItem.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

tests/Feature/Data/list-stories.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
"published_at": "2024-02-08T16:27:05.705Z",
77
"id": 440448565,
88
"uuid": "e656e146-f4ed-44a2-8017-013e5a9d9395",
9-
"content": {
10-
"_uid": "cfe0ff0b-3211-4bb7-8128-66e6ad262a56",
11-
"component": "page"
12-
},
139
"slug": "my-third-post",
1410
"full_slug": "posts/my-third-post",
1511
"sort_by_date": null,
@@ -56,10 +52,6 @@
5652
"published_at": "2024-02-08T16:27:05.705Z",
5753
"id": 440448565,
5854
"uuid": "e656e146-f4ed-44a2-8017-013e5a9d9395",
59-
"content": {
60-
"_uid": "cfe0ff0b-3211-4bb7-8128-66e6ad262a56",
61-
"component": "page"
62-
},
6355
"slug": "my-third-post",
6456
"full_slug": "posts/my-third-post",
6557
"sort_by_date": null,

0 commit comments

Comments
 (0)