Skip to content

Commit 2e60039

Browse files
committed
Refactoring Story / Stories , StoryResponse, StoriesResponse
1 parent 2b7736f commit 2e60039

File tree

10 files changed

+224
-108
lines changed

10 files changed

+224
-108
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Changelog
22

33
## 0.1.0 - WIP
4-
- Refactor exception handling: Bubble up the exception
5-
- Refactor Space, Spaces and SpaceResponse
4+
- Refactoring exception handling: Bubble up the exception
5+
- Refactoring Space, Spaces and SpaceResponse
66
- Adding some helper method to Space like region() and planLevel()
7+
- Refactoring Story / Stories , StoryResponse, StoriesResponse
78

89
## 0.0.7 - 2025-02-09
910

README.md

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ To handle Stories, get stories, get a single story, create a story, update a sto
196196

197197
```php
198198
use Storyblok\ManagementApi\ManagementApiClient;
199+
use Storyblok\ManagementApi\Endpoints\StoryApi;
200+
199201
$spaceId= "1234";
200202
$client = new ManagementApiClient($storyblokPersonalAccessToken);
201203
$storyApi = new StoryApi($client, $spaceId);
@@ -205,6 +207,22 @@ $storyApi = new StoryApi($client, $spaceId);
205207

206208
### Getting a page of stories
207209

210+
When retrieving a list of stories from a space, the Management API provides paginated results.
211+
By default, each page contains 25 stories, but you can customize the number of stories per page and specify which page to retrieve.
212+
213+
To fetch the first page of stories:
214+
215+
```php
216+
use Storyblok\ManagementApi\Endpoints\StoryApi;
217+
218+
$storyApi = new StoryApi($client, $spaceId);
219+
$stories = $storyApi->page()->data();
220+
foreach ($stories as $story) {
221+
echo $story->id() . " - " . $story->name() . PHP_EOL;
222+
}
223+
```
224+
225+
In the case you need to retrieve the response to access to some additional information you can obtain a `StoriesResponse` via the `page()` method:
208226

209227
```php
210228
$response = $storyApi->page();
@@ -214,25 +232,30 @@ echo "LAST URL : " . $response->getLastCalledUrl() . PHP_EOL;
214232
echo "TOTAL : " . $response->total() . PHP_EOL;
215233
echo "PAR PAGE : " . $response->perPage() . PHP_EOL;
216234

217-
$data = $response->data();
218-
echo "Stories found with the page: " . $data->howManyStories() . PHP_EOL;
219-
foreach ($data as $key => $story) {
220-
echo $story->get("id") . " " .
235+
$stories = $response->data();
236+
echo "Stories found with the page: " . $stories->howManyStories() . PHP_EOL;
237+
foreach ($stories as $key => $story) {
238+
echo $story->id() . " " .
221239
$story->getName() . PHP_EOL;
222240
}
223241
```
224242

225243
### Filtering stories
226244
You can filter stories using `StoriesParams`.
227245

246+
> The `StoriesParams` attributes are documented in the `Query Parameters` of [Retrieving multiple stories](https://www.storyblok.com/docs/api/management/core-resources/stories/retrieve-multiple-stories)
247+
228248
```php
229249
use Storyblok\ManagementApi\Endpoints\StoryApi;
230250

231251
$storyApi = new StoryApi($client, $spaceId);
232252
$stories = $storyApi->page(
233-
new StoriesParams(containComponent: "feature"),
234-
new PaginationParams(1, 1000)
235-
);
253+
new StoriesParams(containComponent: "hero-section"),
254+
page: new PaginationParams(2, 10)
255+
)->data();
256+
echo "Stories found: " . $stories->count();
257+
echo " STORY : " . $stories->get("0.name") . PHP_EOL;
258+
236259
```
237260

238261
### Filtering stories via query filters
@@ -244,17 +267,17 @@ Besides using query parameters to filter stories, you can leverage more powerful
244267
In this example, you will retrieve all stories where the "title" field is empty. (Stories with content types that do not include a "title" field in their schema will be skipped.)
245268

246269
```php
247-
use Storyblok\ManagementApi\QueryParameters\Filters\Filter;
248270
use Storyblok\ManagementApi\Endpoints\StoryBulkApi;
271+
use Storyblok\ManagementApi\QueryParameters\Filters\Filter;
249272
use Storyblok\ManagementApi\QueryParameters\Filters\QueryFilters;
250273

251274
$storyBulkApi = new StoryBulkApi($client, $spaceId);
252275
$stories = $storyBulkApi->all(
253276
filters: (new QueryFilters())->add(
254277
new Filter(
255-
"title",
278+
"headline",
256279
"like",
257-
""
280+
"%mars%"
258281
)
259282
)
260283
);
@@ -294,31 +317,29 @@ echo "STATUS CODE : " . $response->getResponseStatusCode() . PHP_EOL;
294317
echo "LAST URL : " . $response->getLastCalledUrl() . PHP_EOL;
295318

296319
$story = $response->data();
297-
echo $story->getName() . PHP_EOL;
320+
echo $story->name() . PHP_EOL;
298321
```
299322

300323
### Creating a Story
301324

302325
To create a story, you can call the `create()` method provided by `StoryApi` and use the `StoryData` class. The `StoryData` class is specific for storing and handling story information. It also provides some nice methods for accessing some relevant Story fields.
303326

304327
```php
305-
$story = new StoryData();
306-
$story->setName("A Story");
307-
$story->setSlug("a-story");
308-
$story->setContentType("page");
309-
$response = $storyApi->create($story);
310-
311-
echo $response->getLastCalledUrl() . PHP_EOL;
312-
echo $response->asJson() . PHP_EOL;
313-
echo $response->getResponseStatusCode() . PHP_EOL;
314-
if ($response->isOk()) {
315-
$storyCreated = $response->data();
316-
echo "Story created, ID: " . $storyCreated->id() . PHP_EOL;
317-
echo " UUID: " . $storyCreated->uuid() . PHP_EOL;
318-
echo " SLUG: " . $storyCreated->slug() . PHP_EOL;
319-
} else {
320-
echo $response->getErrorMessage();
328+
$story = new Story(
329+
name: "A Story",
330+
slug: "a-story",
331+
contentType: "article-page"
332+
);
333+
try{
334+
$storyCreated = $storyApi->create($story)->data();
335+
echo "Created Story: " . $storyCreated->id() . " - " . $storyCreated->name() . PHP_EOL;
336+
} catch (ClientException $e) {
337+
echo "Error while creating story: " . PHP_EOL;
338+
echo $e->getResponse()->getContent(false);
339+
echo PHP_EOL;
340+
echo $e->getMessage();
321341
}
342+
echo PHP_EOL;
322343
```
323344

324345
### Publishing a story
@@ -351,10 +372,7 @@ $file->setCsvControl(separator: ";");
351372
$stories = [];
352373
foreach ($file as $row) {
353374
list($slug, $name, $contentType) = $row;
354-
$story = new StoryData();
355-
$story->setName($name);
356-
$story->setSlug($slug);
357-
$story->setContentType($contentType);
375+
$story = new Story($name, $slug, $contentType);
358376
$stories[] = $story;
359377
}
360378
$createdStories = iterator_to_array($storyBulkApi->createStories($stories));
@@ -540,7 +558,7 @@ Now we want to upload a new image, and then create a new simple story that inclu
540558

541559
```php
542560
use Storyblok\ManagementApi\Data\StoryblokData;
543-
use Storyblok\ManagementApi\Data\StoryData;
561+
use Storyblok\ManagementApi\Data\Story;
544562
use Storyblok\ManagementApi\ManagementApiClient;
545563

546564
$client = new ManagementApiClient($storyblokPersonalAccessToken);
@@ -564,7 +582,7 @@ $content->set("image.id", $assetCreated->id());
564582
$content->set("image.fieldtype", "asset");
565583
$content->set("image.filename",$assetCreated->filename());
566584

567-
$story = new StoryData();
585+
$story = new Story();
568586
$story->setName("An Article");
569587
$story->setSlug("an-article-" . random_int(10000, 99999));
570588
$story->setContent($content->toArray());
@@ -576,7 +594,7 @@ echo $response->getLastCalledUrl() . PHP_EOL;
576594
echo $response->asJson() . PHP_EOL;
577595
echo $response->getResponseStatusCode() . PHP_EOL;
578596
if ($response->isOk()) {
579-
/** @var StoryData $storyCreated */
597+
/** @var Story $storyCreated */
580598
$storyCreated = $response->data();
581599
echo "Story created, ID: " . $storyCreated->id() . PHP_EOL;
582600
echo " UUID: " . $storyCreated->uuid() . PHP_EOL;

src/Data/StoriesData.php renamed to src/Data/Stories.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
use Storyblok\ManagementApi\Data\StoryblokData;
88

9-
class StoriesData extends StoryblokData
9+
class Stories extends StoryblokData
1010
{
1111
#[\Override]
1212
public function getDataClass(): string
1313
{
14-
return StoryData::class;
14+
return Story::class;
1515
}
1616

1717
#[\Override]

src/Data/StoryData.php renamed to src/Data/Story.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,51 @@
44

55
namespace Storyblok\ManagementApi\Data;
66

7-
use Storyblok\ManagementApi\Data\StoryblokData;
7+
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
88

9-
class StoryData extends StoryblokData
9+
class Story extends BaseData
1010
{
1111
private string $defaultContentType = "";
1212

1313
/**
14-
* @param array<string, array<mixed>> $data
14+
* @param string $name the space name
1515
*/
16-
public static function makeFromResponse(array $data = []): self
17-
{
18-
return new self($data["story"] ?? []);
16+
public function __construct(
17+
string $name,
18+
string $slug,
19+
string $contentType
20+
) {
21+
$this->data = [];
22+
$this->data['name'] = $name;
23+
$this->data['slug'] = $slug;
24+
$this->data['content'] = [];
25+
$this->data['content']['component'] = $contentType;
1926
}
2027

21-
#[\Override]
28+
/**
29+
* @param mixed[] $data
30+
* @throws StoryblokFormatException
31+
*/
2232
public static function make(array $data = []): self
2333
{
24-
return new self($data);
34+
$dataObject = new StoryblokData($data);
35+
if (!($dataObject->hasKey('name') && $dataObject->hasKey('slug') && $dataObject->hasKey('content') && $dataObject->hasKey('content.component'))) {
36+
// is not valid
37+
}
38+
39+
$story = new Story(
40+
$dataObject->getString("name"),
41+
$dataObject->getString("slug"),
42+
$dataObject->getString("content.component")
43+
);
44+
$story->setData($dataObject->toArray());
45+
// validate
46+
if (! $story->isValid()) {
47+
throw new StoryblokFormatException("Story is not valid");
48+
}
49+
50+
return $story;
51+
2552
}
2653

2754
public function setName(string $name): void

0 commit comments

Comments
 (0)