Skip to content

Commit 0f2312e

Browse files
committed
Refactoring Tag, Tags and TagResponse
1 parent 48257fc commit 0f2312e

11 files changed

+246
-136
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Refactoring Story / Stories , StoryResponse, StoriesResponse
88
- Adding StoryComponent class
99
- Refactoring Asset, Assets and AssetResponse
10+
- Refactoring Tag, Tags and TagResponse
1011

1112
## 0.0.7 - 2025-02-09
1213

README.md

+27-36
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ $spaces->forEach( function (Space $space) {
159159
Retrieve detailed information about a specific space using its ID:
160160

161161
```php
162-
$spaceID = "12345"
163-
$spaceId = "321388";
162+
$spaceId = "12345";
164163
$spaceApi = new SpaceApi($clientEU);
165164
$space = $spaceApi->get($spaceId)->data();
166165

@@ -313,18 +312,15 @@ foreach ($stories as $story) {
313312

314313
```php
315314
$storyId= "1234";
316-
$response = $storyApi->get($storyId);
317-
318-
echo "STATUS CODE : " . $response->getResponseStatusCode() . PHP_EOL;
319-
echo "LAST URL : " . $response->getLastCalledUrl() . PHP_EOL;
320-
321-
$story = $response->data();
315+
$story = $storyApi->get($storyId)->data();
322316
echo $story->name() . PHP_EOL;
317+
323318
```
324319

325320
### Creating a Story
326321

327-
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.
322+
To create a story, you can call the `create()` method provided by `StoryApi` and use the `Story` class. The `Story` class is specific for storing and handling story information. It also provides some nice methods for accessing some relevant Story fields.
323+
And because a story stores also the content, you can use the StoryComponent class to handle the fields in the content.
328324

329325
```php
330326
$content = new StoryComponent("article-page");
@@ -372,14 +368,25 @@ myslug-003;My Story 3 BULK;page
372368
Next, you can implement a script to load and parse the CSV file. In this case, we use `SplFileObject` and then call the `createStories` method to process the data:
373369

374370
```php
371+
372+
use Storyblok\ManagementApi\Data\Story;
373+
use Storyblok\ManagementApi\Data\StoryComponent;
374+
use Storyblok\ManagementApi\Endpoints\StoryBulkApi;
375+
use Storyblok\ManagementApi\ManagementApiClient;
376+
377+
$client = new ManagementApiClient($storyblokPersonalAccessToken);
375378
$storyBulkApi = new StoryBulkApi($client, $spaceId);
376379
$file = new SplFileObject("stories.csv");
377380
$file->setFlags(SplFileObject::READ_CSV);
378381
$file->setCsvControl(separator: ";");
379382
$stories = [];
380383
foreach ($file as $row) {
381384
list($slug, $name, $contentType) = $row;
382-
$story = new Story($name, $slug, $contentType);
385+
$story = new Story(
386+
$name,
387+
$slug,
388+
new StoryComponent($contentType)
389+
);
383390
$stories[] = $story;
384391
}
385392
$createdStories = iterator_to_array($storyBulkApi->createStories($stories));
@@ -438,8 +445,7 @@ To get the assets list you can use the `assetApi` and the `AssetsData`.
438445

439446
```php
440447
$assetApi = new AssetApi($client, $spaceId);
441-
$response = $assetApi->page();
442-
$assets = $response->data();
448+
$assets = $assetApi->page()->data();
443449

444450
foreach ($assets as $key => $asset) {
445451
echo $asset->id() . PHP_EOL;
@@ -463,7 +469,7 @@ $assets = $assetApi->page(
463469
search: "something"
464470
),
465471
new PaginationParams(1,1000)
466-
);
472+
)->data();
467473
```
468474

469475
In the example above, we are filtering the deleted assets (`inFolder : -1`) and with the filename that contains the term `something`.
@@ -474,8 +480,7 @@ Additional info: using `PaginationParams` you can retrieve a specific page. The
474480
To get a specific asset, you can use the `AssetApi` and the `AssetData` classes.
475481

476482
```php
477-
$response = $assetApi->get($assetId);
478-
$asset = $response->data();
483+
$asset = $assetApi->get($assetId)->data();
479484
echo $asset->id() . PHP_EOL;
480485
echo $asset->contentType() . PHP_EOL;
481486
echo $asset->contentLength() . PHP_EOL;
@@ -489,13 +494,8 @@ echo "---" . PHP_EOL;
489494
To upload an asset, you can use the `upload()` method:
490495

491496
```php
492-
$response = $assetApi->upload("image.png");
493-
494-
echo $response->getLastCalledUrl() . PHP_EOL;
495-
echo $response->asJson() . PHP_EOL;
496-
echo $response->getResponseStatusCode() . PHP_EOL;
497+
$assetCreated = $assetApi->upload("image.png")->data();
497498

498-
$assetCreated = $response->data();
499499
echo "Asset created, ID: " . $assetCreated->id() . PHP_EOL;
500500
echo " filename: " . $assetCreated->filename() . PHP_EOL;
501501
echo " filename CDN: " . $assetCreated->filenameCDN() . PHP_EOL;
@@ -508,8 +508,7 @@ To delete an asset, you can use the `delete()` method. The `delete()` method req
508508
```php
509509
$assetApi = new AssetApi($client, $spaceId);
510510
echo "DELETING " . $assetId . PHP_EOL;
511-
$response = $assetApi->delete($assetId);
512-
$deletedAsset = $response->data();
511+
$deletedAsset = $assetApi->delete($assetId)-data();
513512
echo "DELETED ASSET, ID : " . $deletedAsset->id() . PHP_EOL;
514513
```
515514

@@ -593,22 +592,14 @@ $story = new Story(
593592
"an-article-" . random_int(10000, 99999),
594593
$content
595594
);
595+
$story->setTagsFromArray(["aaa", "bbb", "CCC"]);
596596

597597
echo "CREATING STORY..." . PHP_EOL;
598-
$response = $storyApi->create($story);
598+
$storyCreated = $storyApi->create($story)->data();
599+
echo "Story created, ID: " . $storyCreated->id() . PHP_EOL;
600+
echo " UUID: " . $storyCreated->uuid() . PHP_EOL;
601+
echo " SLUG: " . $storyCreated->slug() . PHP_EOL;
599602

600-
echo $response->getLastCalledUrl() . PHP_EOL;
601-
echo $response->asJson() . PHP_EOL;
602-
echo $response->getResponseStatusCode() . PHP_EOL;
603-
if ($response->isOk()) {
604-
/** @var Story $storyCreated */
605-
$storyCreated = $response->data();
606-
echo "Story created, ID: " . $storyCreated->id() . PHP_EOL;
607-
echo " UUID: " . $storyCreated->uuid() . PHP_EOL;
608-
echo " SLUG: " . $storyCreated->slug() . PHP_EOL;
609-
} else {
610-
echo $response->getErrorMessage();
611-
}
612603
```
613604

614605

src/Data/Stories.php

-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ public static function make(array $data = []): self
2020
return new self($data);
2121
}
2222

23-
/**
24-
* @param array<string, array<mixed>> $data
25-
*/
26-
public static function makeFromResponse(array $data = []): self
27-
{
28-
return new self($data["stories"] ?? []);
29-
}
30-
3123
public function howManyStories(): int
3224
{
3325
return $this->count();

src/Data/Story.php

+22
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,26 @@ public function isValid(): bool
131131

132132
return $this->hasKey('slug') && !in_array($this->getString('slug'), ['', '0'], true);
133133
}
134+
135+
/**
136+
* Set tags for Story, from a `Tags` collection
137+
* @return $this
138+
*/
139+
public function setTags(Tags $tags): self
140+
{
141+
142+
return $this->setTagsFromArray($tags->getTagsArray());
143+
144+
}
145+
146+
/**
147+
* Set tags for Story, from a string of arrays like ["tag1", "tag2"]
148+
* @param string[] $tagsArray
149+
* @return $this
150+
*/
151+
public function setTagsFromArray(array $tagsArray): self
152+
{
153+
$this->set("tag_list", $tagsArray);
154+
return $this;
155+
}
134156
}

src/Data/Tag.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
8+
9+
class Tag extends BaseData
10+
{
11+
public function __construct(
12+
string $name
13+
) {
14+
$this->data = [];
15+
$this->data['name'] = $name;
16+
}
17+
18+
/**
19+
* @param mixed[] $data
20+
* @throws StoryblokFormatException
21+
*/
22+
public static function make(array $data = []): self
23+
{
24+
$dataObject = new StoryblokData($data);
25+
if (!($dataObject->hasKey('name'))) {
26+
// is not valid
27+
}
28+
29+
$tag = new Tag(
30+
$dataObject->getString("name")
31+
);
32+
$tag->setData($dataObject->toArray());
33+
// validate
34+
if (! $tag->isValid()) {
35+
throw new StoryblokFormatException("Tag is not valid");
36+
}
37+
38+
return $tag;
39+
40+
}
41+
42+
public function isValid(): bool
43+
{
44+
return $this->hasKey('name');
45+
}
46+
47+
public function name(): string
48+
{
49+
return $this->getString('name');
50+
}
51+
52+
public function id(): string
53+
{
54+
return $this->getString('id');
55+
}
56+
57+
public function taggingsCount(): int|null
58+
{
59+
return $this->getInt('taggings_count', 0);
60+
}
61+
62+
public function tagOnStories(): int|null
63+
{
64+
return $this->getInt('tag_on_stories', 0);
65+
}
66+
}

src/Data/TagData.php

-42
This file was deleted.

src/Data/Tags.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
class Tags extends StoryblokData
8+
{
9+
#[\Override]
10+
public function getDataClass(): string
11+
{
12+
return Tag::class;
13+
}
14+
15+
/**
16+
* @param mixed[] $data
17+
*/
18+
#[\Override]
19+
public static function make(array $data = []): self
20+
{
21+
return new self($data);
22+
}
23+
24+
/**
25+
* @return string[]
26+
*/
27+
public function getTagsArray(): array
28+
{
29+
/** @var string[] $tagsArray */
30+
$tagsArray = [];
31+
32+
foreach ($this->data as $tag) {
33+
if (is_array($tag) && array_key_exists("name", $tag)) {
34+
$tagsArray[] = $tag["name"];
35+
}
36+
37+
}
38+
39+
return $tagsArray;
40+
}
41+
}

src/Data/TagsData.php

-28
This file was deleted.

0 commit comments

Comments
 (0)