Skip to content

Commit bba4a81

Browse files
committed
Refactoring Story classes for better handling of common fields
Refactoring Story classes for better handling of common fields in get single story and get list of stories
1 parent cf6caef commit bba4a81

File tree

5 files changed

+371
-122
lines changed

5 files changed

+371
-122
lines changed

src/Data/Story.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ public function content(): StoryComponent
8484
return StoryComponent::make($contentArray);
8585
}
8686

87-
public function name(): string
88-
{
89-
return $this->getString("name");
90-
}
91-
9287
/**
9388
* Get the folder id for the Story.
9489
*
@@ -99,21 +94,6 @@ public function folderId(): int
9994
return (int) $this->getInt("parent_id", 0);
10095
}
10196

102-
public function createdAt(string $format = "Y-m-d"): null|string
103-
{
104-
return $this->getFormattedDateTime("created_at", "", format: $format);
105-
}
106-
107-
public function publishedAt(string $format = "Y-m-d"): null|string
108-
{
109-
return $this->getFormattedDateTime("published_at", "", format: $format);
110-
}
111-
112-
public function updatedAt(): null|string
113-
{
114-
return $this->getFormattedDateTime("updated_at", "", format: "Y-m-d");
115-
}
116-
11797
public function setContentType(string $componentName): self
11898
{
11999
$this->defaultContentType = $componentName;
@@ -125,32 +105,6 @@ public function defaultContentType(): string
125105
return $this->defaultContentType;
126106
}
127107

128-
public function id(): string
129-
{
130-
return $this->getString("id");
131-
}
132-
133-
public function uuid(): string
134-
{
135-
return $this->getString("uuid");
136-
}
137-
138-
/**
139-
* Validates if the story data contains all required fields and valid values
140-
*/
141-
public function isValid(): bool
142-
{
143-
if (
144-
!$this->hasKey("name") ||
145-
in_array($this->getString("name"), ["", "0"], true)
146-
) {
147-
return false;
148-
}
149-
150-
return $this->hasKey("slug") &&
151-
!in_array($this->getString("slug"), ["", "0"], true);
152-
}
153-
154108
/**
155109
* Set tags for Story, from a `Tags` collection
156110
* @return $this

src/Data/StoryBaseData.php

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,33 @@
44

55
namespace Storyblok\ManagementApi\Data;
66

7-
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
8-
97
abstract class StoryBaseData extends BaseData
108
{
119
public function slug(): string
1210
{
13-
return $this->getString('slug');
11+
return $this->getString("slug");
12+
}
13+
14+
public function name(): string
15+
{
16+
return $this->getString("name");
1417
}
1518

1619
public function hasWorkflowStage(): bool
1720
{
18-
$workflowStageId = $this->getInt('stage.workflow_stage_id', 0);
21+
$workflowStageId = $this->getInt("stage.workflow_stage_id", 0);
1922
return $workflowStageId > 0;
2023
}
2124

2225
public function hasTags(): bool
2326
{
24-
$tags = $this->getArray('tag_list', []);
27+
$tags = $this->getArray("tag_list", []);
2528
return $tags !== [];
2629
}
2730

2831
public function tagListAsString(): string
2932
{
30-
$tags = $this->getArray('tag_list', []);
33+
$tags = $this->getArray("tag_list", []);
3134
return implode(", ", $tags);
3235
}
3336

@@ -38,6 +41,77 @@ public function tagListAsString(): string
3841
*/
3942
public function tagListAsArray(): array
4043
{
41-
return $this->getArray('tag_list', []);
44+
return $this->getArray("tag_list", []);
45+
}
46+
47+
public function id(): string
48+
{
49+
return $this->getString("id");
50+
}
51+
52+
public function uuid(): string
53+
{
54+
return $this->getString("uuid");
55+
}
56+
57+
public function createdAt(string $format = "Y-m-d"): null|string
58+
{
59+
return $this->getFormattedDateTime("created_at", "", format: $format);
60+
}
61+
62+
public function publishedAt(string $format = "Y-m-d"): null|string
63+
{
64+
return $this->getFormattedDateTime("published_at", "", format: $format);
65+
}
66+
67+
public function updatedAt(): null|string
68+
{
69+
return $this->getFormattedDateTime("updated_at", "", format: "Y-m-d");
70+
}
71+
72+
/**
73+
* Validates if the story data contains all required fields and valid values
74+
*/
75+
public function isValid(): bool
76+
{
77+
if (
78+
!$this->hasKey("name") ||
79+
in_array($this->getString("name"), ["", "0"], true)
80+
) {
81+
return false;
82+
}
83+
84+
return $this->hasKey("slug") &&
85+
!in_array($this->getString("slug"), ["", "0"], true);
86+
}
87+
88+
public function fullSlug(): string
89+
{
90+
return $this->getString("full_slug");
91+
}
92+
93+
public function isStartpage(): bool
94+
{
95+
return $this->getBoolean("is_startpage", false);
96+
}
97+
98+
public function parentId(): int
99+
{
100+
return $this->getIntStrict("parent_id", 0);
101+
}
102+
103+
public function groupId(): string
104+
{
105+
return $this->getString("group_id");
106+
}
107+
108+
public function releaseId(): int
109+
{
110+
return $this->getIntStrict("release_id", 0);
111+
}
112+
113+
public function firstPublishedAt(string $format = "Y-m-d"): null|string
114+
{
115+
return $this->getFormattedDateTime("first_published_at", "", format: $format);
42116
}
43117
}

src/Data/StoryCollectionItem.php

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class StoryCollectionItem extends StoryBaseData
2424
public function __construct()
2525
{
2626
$this->data = [];
27-
2827
}
2928

3029
/**
@@ -34,61 +33,18 @@ public function __construct()
3433
public static function make(array $data = []): self
3534
{
3635
$dataObject = new StoryblokData($data);
37-
if (!($dataObject->hasKey('name') && $dataObject->hasKey('slug'))) {
36+
if (!($dataObject->hasKey("name") && $dataObject->hasKey("slug"))) {
3837
// is not valid
3938
}
4039

4140
$storyItem = new StoryCollectionItem();
4241
$storyItem->setData($dataObject->toArray());
4342
// validate
44-
if (! $storyItem->isValid()) {
43+
if (!$storyItem->isValid()) {
4544
throw new StoryblokFormatException("Story is not valid");
4645
}
4746

4847
return $storyItem;
49-
50-
}
51-
52-
public function name(): string
53-
{
54-
return $this->getString('name');
55-
}
56-
57-
public function createdAt(string $format = "Y-m-d"): null|string
58-
{
59-
return $this->getFormattedDateTime('created_at', "", format: $format);
60-
}
61-
62-
public function publishedAt(string $format = "Y-m-d"): null|string
63-
{
64-
return $this->getFormattedDateTime('published_at', "", format: $format);
65-
}
66-
67-
public function updatedAt(): null|string
68-
{
69-
return $this->getFormattedDateTime('updated_at', "", format: "Y-m-d");
70-
}
71-
72-
public function id(): string
73-
{
74-
return $this->getString('id');
75-
}
76-
77-
public function uuid(): string
78-
{
79-
return $this->getString('uuid');
80-
}
81-
82-
/**
83-
* Validates if the story data contains all required fields and valid values
84-
*/
85-
public function isValid(): bool
86-
{
87-
if (!$this->hasKey('name') || in_array($this->getString('name'), ['', '0'], true)) {
88-
return false;
89-
}
90-
91-
return $this->hasKey('slug') && !in_array($this->getString('slug'), ['', '0'], true);
9248
}
9349

9450
/**
@@ -97,9 +53,7 @@ public function isValid(): bool
9753
*/
9854
public function setTags(Tags $tags): self
9955
{
100-
10156
return $this->setTagsFromArray($tags->getTagsArray());
102-
10357
}
10458

10559
/**

tests/Feature/Data/one-story.json

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
{
22
"story": {
33
"name": "My third post",
4-
"created_at": "2024-02-08T16:26:24.425Z",
5-
"published_at": "2024-02-08T16:27:05.705Z",
6-
"id": 440448565,
7-
"uuid": "e656e146-f4ed-44a2-8017-013e5a9d9395",
8-
"content": {
9-
"_uid": "cfe0ff0b-3211-4bb7-8128-66e6ad262a56",
10-
"component": "page"
11-
},
12-
"slug": "my-third-post",
13-
"full_slug": "posts/my-third-post",
14-
"sort_by_date": null,
15-
"position": 0,
16-
"tag_list": ["tag1","tag2"],
17-
"is_startpage": false,
184
"parent_id": 440448337,
19-
"meta_data": null,
205
"group_id": "b913a671-f1e9-436a-bc5d-2795d2740198",
21-
"first_published_at": "2024-02-08T16:27:05.705Z",
22-
"release_id": null,
23-
"lang": "default",
24-
"path": null,
256
"alternates": [
267
{
278
"id": 440452827,
@@ -33,7 +14,47 @@
3314
"parent_id": 440452826
3415
}
3516
],
36-
"default_full_slug": "posts/my-third-post",
17+
"created_at": "2024-02-08T16:26:24.425Z",
18+
"deleted_at": null,
19+
"sort_by_date": null,
20+
"tag_list": ["tag1", "tag2"],
21+
"updated_at": "2024-02-08T16:27:10.000Z",
22+
"published_at": "2024-02-08T16:27:05.705Z",
23+
"id": 440448565,
24+
"uuid": "e656e146-f4ed-44a2-8017-013e5a9d9395",
25+
"is_folder": false,
26+
"content": {
27+
"_uid": "cfe0ff0b-3211-4bb7-8128-66e6ad262a56",
28+
"component": "page"
29+
},
30+
"published": true,
31+
"slug": "my-third-post",
32+
"path": null,
33+
"full_slug": "posts/my-third-post",
34+
"default_root": null,
35+
"disable_fe_editor": false,
36+
"parent": null,
37+
"is_startpage": false,
38+
"unpublished_changes": false,
39+
"meta_data": null,
40+
"imported_at": null,
41+
"preview_token": {
42+
"token": "abc123preview",
43+
"timestamp": "1707407225"
44+
},
45+
"pinned": false,
46+
"breadcrumbs": [],
47+
"publish_at": null,
48+
"expire_at": null,
49+
"first_published_at": "2024-02-08T16:27:05.705Z",
50+
"last_author": {
51+
"id": 123456,
52+
"userid": "user@example.com",
53+
"friendly_name": "Test User"
54+
},
55+
"last_author_id": 123456,
56+
"user_ids": [],
57+
"space_role_ids": [],
3758
"translated_slugs": [
3859
{
3960
"path": "posts/my-third-post",
@@ -47,6 +68,25 @@
4768
"lang": "de",
4869
"published": true
4970
}
50-
]
71+
],
72+
"localized_paths": [],
73+
"position": 0,
74+
"translated_stories": [],
75+
"can_not_view": false,
76+
"is_scheduled": null,
77+
"scheduled_dates": null,
78+
"ideas": [],
79+
"current_version_id": 0,
80+
"stage": {
81+
"workflow_stage_id": null,
82+
"workflow_id": null,
83+
"workflow_stage_name": null,
84+
"workflow_stage_color": null,
85+
"workflow_name": null
86+
},
87+
"favourite_for_user_ids": [],
88+
"default_full_slug": "posts/my-third-post",
89+
"release_id": null,
90+
"lang": "default"
5191
}
5292
}

0 commit comments

Comments
 (0)