Skip to content

Commit d448dba

Browse files
committed
Welcome to StoryComponent class 🚀
1 parent 3dd958f commit d448dba

File tree

7 files changed

+158
-20
lines changed

7 files changed

+158
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Refactoring Space, Spaces and SpaceResponse
66
- Adding some helper method to Space like region() and planLevel()
77
- Refactoring Story / Stories , StoryResponse, StoriesResponse
8+
- Adding StoryComponent class
9+
- Refactoring Asset, Assets and AssetResponse
810

911
## 0.0.7 - 2025-02-09
1012

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The *Storyblok Management API PHP Client* library simplifies the integration wit
2525
Install the package via Composer:
2626

2727
```shell
28-
composer require storyblok/php-management-api-client
28+
composer require storyblok/php-management-api-client:dev-main
2929
```
3030

3131
Below is an example showcasing how to use the library to interact with the Management API.
@@ -325,10 +325,15 @@ echo $story->name() . PHP_EOL;
325325
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.
326326

327327
```php
328+
$content = new StoryComponent("article-page");
329+
$content->set("title", "My New Article");
330+
$content->set("body", "This is the content");
331+
// $content->setAsset("image", $assetCreated);
332+
328333
$story = new Story(
329334
name: "A Story",
330335
slug: "a-story",
331-
contentType: "article-page"
336+
content: $content
332337
);
333338
try{
334339
$storyCreated = $storyApi->create($story)->data();

src/Data/Asset.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,50 @@
44

55
namespace Storyblok\ManagementApi\Data;
66

7-
class Asset extends StoryblokData
7+
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
8+
9+
class Asset extends BaseData
810
{
9-
/*
11+
public function __construct(
12+
string $filename,
13+
) {
14+
$this->data = [];
15+
$this->data['filename'] = $filename;
16+
$this->data['fieldtype'] = "asset";
17+
}
18+
19+
/**
1020
* The Asset data response payload doesn't have the typical
1121
* "asset" attribute (like the story, the space etc)
12-
* This is the reason why the makeFromResponse is not implemented here
22+
* @param mixed[] $data
23+
* @throws StoryblokFormatException
1324
*/
14-
15-
#[\Override]
1625
public static function make(array $data = []): self
1726
{
18-
return new self($data);
27+
$dataObject = new StoryblokData($data);
28+
if (! $dataObject->hasKey('fieldtype')) {
29+
$dataObject->set("fieldtype", "asset");
30+
}
31+
32+
if (!($dataObject->hasKey('filename') && $dataObject->hasKey('fieldtype'))) {
33+
// is not valid
34+
}
35+
36+
$asset = new Asset(
37+
$dataObject->getString("filename")
38+
);
39+
$asset->setData($dataObject->toArray());
40+
// validate
41+
if (! $asset->isValid()) {
42+
throw new StoryblokFormatException("Asset is not valid");
43+
}
44+
45+
return $asset;
46+
}
47+
48+
public function isValid(): bool
49+
{
50+
return $this->hasKey('filename');
1951
}
2052

2153
public function id(): string

src/Data/BaseData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ public function toJson(): string|false
248248
return json_encode($this->data, JSON_PRETTY_PRINT);
249249
}
250250

251+
public function dump(): void
252+
{
253+
echo $this->toJson();
254+
}
255+
251256
/**
252257
* Retrieves a value from the data by key. Supports dot notation for nested keys.
253258
*

src/Data/Story.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class Story extends BaseData
1616
public function __construct(
1717
string $name,
1818
string $slug,
19-
string $contentType
19+
StoryComponent $content,
2020
) {
2121
$this->data = [];
2222
$this->data['name'] = $name;
2323
$this->data['slug'] = $slug;
24-
$this->data['content'] = [];
25-
$this->data['content']['component'] = $contentType;
24+
$this->data['content'] = $content->toArray();
25+
2626
}
2727

2828
/**
@@ -36,10 +36,12 @@ public static function make(array $data = []): self
3636
// is not valid
3737
}
3838

39+
$content = StoryComponent::make($dataObject->getArray("content"));
40+
3941
$story = new Story(
4042
$dataObject->getString("name"),
4143
$dataObject->getString("slug"),
42-
$dataObject->getString("content.component")
44+
$content
4345
);
4446
$story->setData($dataObject->toArray());
4547
// validate
@@ -71,12 +73,15 @@ public function setCreatedAt(string $createdAt): void
7173
$this->set('created_at', $createdAt);
7274
}
7375

74-
/**
75-
* @param array<mixed> $content
76-
*/
77-
public function setContent(array $content): void
76+
public function setContent(StoryComponent $content): void
77+
{
78+
$this->set('content', $content->toArray());
79+
}
80+
81+
public function content(): StoryComponent
7882
{
79-
$this->set('content', $content);
83+
$contentArray = $this->getArray('content');
84+
return StoryComponent::make($contentArray);
8085
}
8186

8287
public function name(): string

src/Data/StoryComponent.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
use Storyblok\ManagementApi\Data\BaseData;
8+
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
9+
10+
/**
11+
* Class StoryComponent
12+
*
13+
* Represents the content section within a Story object retrieved from the
14+
* Storyblok API.
15+
*
16+
* The content section is an object containing field data associated with the
17+
* specific story type's content structure.
18+
* It also includes a `component` property, which holds the technical name of
19+
* the story type.
20+
*
21+
* Example structure of content property in a story:
22+
* ```json
23+
* {
24+
* "component": "page",
25+
* "title": "Homepage",
26+
* "body": [
27+
* { "component": "text", "text": "Welcome to our website" }
28+
* ]
29+
* }
30+
* ```
31+
*/
32+
class StoryComponent extends BaseData
33+
{
34+
public function __construct(
35+
string $contentType,
36+
) {
37+
$this->data = [];
38+
$this->data['component'] = $contentType;
39+
}
40+
41+
/**
42+
* @param mixed[] $data
43+
* @throws StoryblokFormatException
44+
*/
45+
public static function make(array $data = []): self
46+
{
47+
if (! array_key_exists("component", $data)) {
48+
throw new StoryblokFormatException(
49+
"Story `content` is not valid, `component` property is missing."
50+
);
51+
}
52+
53+
$storyContent = new StoryComponent(
54+
$data["component"],
55+
);
56+
$storyContent->setData($data);
57+
// validate
58+
if (! $storyContent->isValid()) {
59+
throw new StoryblokFormatException("Story content is not valid");
60+
}
61+
62+
return $storyContent;
63+
64+
}
65+
66+
public function setComponent(string $component): self
67+
{
68+
$this->set('component', $component);
69+
return $this;
70+
}
71+
72+
public function component(): string
73+
{
74+
return $this->getString('component');
75+
}
76+
77+
/**
78+
* Validates if the story content contains all required fields and valid values
79+
*/
80+
public function isValid(): bool
81+
{
82+
return $this->hasKey('component');
83+
}
84+
85+
public function setAsset(string $field, Asset $asset): self
86+
{
87+
$this->set($field, $asset->toArray());
88+
return $this;
89+
}
90+
}

src/Endpoints/StoryApi.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\Log\NullLogger;
99
use Storyblok\ManagementApi\Data\Stories;
1010
use Storyblok\ManagementApi\Data\Story;
11+
use Storyblok\ManagementApi\Data\StoryComponent;
1112
use Storyblok\ManagementApi\Exceptions\InvalidStoryDataException;
1213
use Storyblok\ManagementApi\Exceptions\StoryblokApiException;
1314
use Storyblok\ManagementApi\ManagementApiClient;
@@ -106,9 +107,7 @@ public function create(Story $storyData): StoryResponse
106107
$this->validateStoryData($storyData);
107108

108109
if (!$storyData->hasKey("content")) {
109-
$storyData->setContent([
110-
"component" => $storyData->defaultContentType(),
111-
]);
110+
$storyData->setContent(new StoryComponent($storyData->defaultContentType()));
112111
}
113112

114113
try {

0 commit comments

Comments
 (0)