Skip to content

Commit 5d8b0da

Browse files
committed
Welcome WorkflowApi
1 parent 7a35091 commit 5d8b0da

File tree

11 files changed

+301
-3
lines changed

11 files changed

+301
-3
lines changed

CHANGELOG.md

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

3-
## 0.0.3 - WIP
3+
## 0.0.3 - 2025-02-03
44

55
- Adding helper methods for SpaceData (id(), updatedAt())
6+
- Adding WorkflowApi class for handling workflows
67

78
## 0.0.2 - 2025-02-01
89

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,35 @@ if ($response->isOk()) {
525525
```
526526

527527

528+
## Handling Workflows
528529

530+
If you need to handle workflows (retrieving workflows or create new custome workflow - in the case you have a proper plan - ) you can use the `WorkflowApi` class.
531+
532+
### Retrieving workflows
533+
534+
```php
535+
$workflowApi = $client->workflowApi($spaceId);
536+
$response = $workflowApi->list();
537+
/** @var WorkflowsData $workflows */
538+
$workflows = $response->data();
539+
540+
foreach ($workflows as $key => $workflow) {
541+
echo $workflow->name() . PHP_EOL;
542+
echo print_r($workflow->contentTypes(), true) . PHP_EOL;
543+
echo $workflow->isDefault() . PHP_EOL;
544+
echo "---" . PHP_EOL;
545+
}
546+
547+
```
548+
549+
### Creating a new custom workflow
550+
551+
```php
552+
$workflowApi = $client->workflowApi($spaceId);
553+
$workflowData = new WorkflowData();
554+
$workflowData->setName("Name");
555+
$response = $workflowApi->create($workflowData);
556+
```
529557

530558

531559
## Using the `ManagementApi` class

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
typeDeclarations: true,
1818
// naming: true,
1919
instanceOf: true,
20+
2021
earlyReturn: true,
2122
strictBooleans: true,
2223
carbon: true,

src/Data/StoryblokData.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Storyblok\ManagementApi\Data;
66

77
use ArrayAccess;
8+
use ArrayObject;
89
use Countable;
910
use Iterator;
1011

@@ -121,7 +122,7 @@ public function getInt(mixed $key, int|null $defaultValue = null, string $charNe
121122

122123
public function getBoolean(mixed $key, bool $defaultValue = false, string $charNestedKey = "."): bool
123124
{
124-
$returnValue = $this->get($key, false, $charNestedKey);
125+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
125126

126127
if (is_scalar($returnValue)) {
127128
return boolval($returnValue);
@@ -130,6 +131,25 @@ public function getBoolean(mixed $key, bool $defaultValue = false, string $charN
130131
return $defaultValue;
131132
}
132133

134+
/**
135+
* @param mixed[] $defaultValue
136+
* @return mixed[]
137+
*/
138+
public function getArray(mixed $key, array $defaultValue = [], string $charNestedKey = "."): array
139+
{
140+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
141+
142+
if (is_scalar($returnValue)) {
143+
return [strval($returnValue) ];
144+
}
145+
146+
if ($returnValue instanceof StoryblokData) {
147+
return $returnValue->toArray();
148+
}
149+
150+
return $defaultValue;
151+
}
152+
133153
public function getFormattedDateTime(
134154
mixed $key,
135155
string $defaultValue = "",

src/Data/WorkflowData.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
use Storyblok\ManagementApi\Data\StoryblokData;
8+
use Storyblok\ManagementApi\StoryblokUtils;
9+
10+
class WorkflowData extends StoryblokData
11+
{
12+
/**
13+
* @param array<string, array<mixed>> $data
14+
*/
15+
public static function makeFromResponse(array $data = []): self
16+
{
17+
return new self($data["workflow"] ?? []);
18+
}
19+
20+
#[\Override]
21+
public static function make(array $data = []): self
22+
{
23+
return new self($data);
24+
}
25+
26+
public function setName(string $name): void
27+
{
28+
$this->set('name', $name);
29+
}
30+
31+
32+
33+
public function name(): string
34+
{
35+
return $this->getString('name', "");
36+
}
37+
38+
public function id(): string
39+
{
40+
return $this->getString('id', "");
41+
}
42+
43+
public function isDefault(): bool
44+
{
45+
return $this->getBoolean('is_default', false);
46+
}
47+
48+
/**
49+
* @return mixed[]|null
50+
*/
51+
public function contentTypes(): null|array
52+
{
53+
return $this->getArray('content_types', []);
54+
}
55+
56+
}

src/Data/WorkflowsData.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
class WorkflowsData extends StoryblokData
8+
{
9+
#[\Override]
10+
public function getDataClass(): string
11+
{
12+
return WorkflowData::class;
13+
}
14+
15+
#[\Override]
16+
public static function make(array $data = []): self
17+
{
18+
return new self($data);
19+
}
20+
21+
/**
22+
* @param array<string, array<mixed>> $data
23+
*/
24+
public static function makeFromResponse(array $data = []): self
25+
{
26+
return new self($data["workflows"] ?? []);
27+
}
28+
29+
30+
public function howManyWorkflows(): int
31+
{
32+
return $this->count();
33+
}
34+
35+
}

src/Endpoints/TagApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function update(string $name, string $newName): StoryblokResponseInterfac
7272
{
7373
return $this->makeRequest(
7474
"POST",
75-
"/v1/spaces/" . $this->spaceId . '/tags' . $name,
75+
"/v1/spaces/" . $this->spaceId . '/tags/' . $name,
7676
[
7777
"body" => [
7878
"name" => $newName,

src/Endpoints/WorkflowApi.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Endpoints;
6+
7+
use Storyblok\ManagementApi\Data\StoryblokData;
8+
use Storyblok\ManagementApi\Data\WorkflowData;
9+
use Storyblok\ManagementApi\Data\WorkflowsData;
10+
use Storyblok\ManagementApi\StoryblokResponseInterface;
11+
12+
/**
13+
*
14+
*/
15+
class WorkflowApi extends EndpointSpace
16+
{
17+
/**
18+
* @param string|string[]|null $contentType
19+
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
20+
*/
21+
public function list(
22+
string|array|null $contentType = null,
23+
): StoryblokResponseInterface {
24+
$options = [];
25+
if (null !== $contentType) {
26+
if (is_array($contentType)) {
27+
$contentType = implode(',', $contentType);
28+
}
29+
30+
$options = [
31+
'query' => [
32+
'content_type' => $contentType,
33+
],
34+
];
35+
36+
}
37+
38+
return $this->makeRequest(
39+
"GET",
40+
'/v1/spaces/' . $this->spaceId . '/workflows',
41+
options: $options,
42+
dataClass: WorkflowsData::class,
43+
);
44+
}
45+
46+
public function get(string|int $workflowId): StoryblokResponseInterface
47+
{
48+
49+
return $this->makeRequest(
50+
"GET",
51+
'/v1/spaces/' . $this->spaceId . '/workflows/' . $workflowId,
52+
dataClass: WorkflowData::class,
53+
);
54+
}
55+
56+
57+
/**
58+
* @param string|int $workflowId the workflow identifier
59+
*/
60+
public function delete(string|int $workflowId): StoryblokResponseInterface
61+
{
62+
return $this->makeRequest(
63+
"DELETE",
64+
'/v1/spaces/' . $this->spaceId . '/workflows/' . $workflowId,
65+
);
66+
}
67+
68+
public function create(StoryblokData $storyblokData): StoryblokResponseInterface
69+
{
70+
return $this->makeRequest(
71+
"POST",
72+
"/v1/spaces/" . $this->spaceId . '/workflows',
73+
[
74+
"body" => [
75+
"workflow" => $storyblokData->toArray(),
76+
],
77+
],
78+
dataClass: WorkflowData::class,
79+
);
80+
}
81+
82+
public function update(string|int $workflowId, StoryblokData $storyblokData): StoryblokResponseInterface
83+
{
84+
return $this->makeRequest(
85+
"POST",
86+
"/v1/spaces/" . $this->spaceId . '/workflows/' . $workflowId,
87+
[
88+
"body" => $storyblokData->toArray(),
89+
],
90+
dataClass: WorkflowData::class,
91+
);
92+
}
93+
94+
95+
96+
97+
98+
}

src/ManagementApiClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Storyblok\ManagementApi\Endpoints\StoryApi;
1212
use Storyblok\ManagementApi\Endpoints\TagApi;
1313
use Storyblok\ManagementApi\Endpoints\UserApi;
14+
use Storyblok\ManagementApi\Endpoints\WorkflowApi;
1415
use Symfony\Component\HttpClient\HttpClient;
1516
use Symfony\Contracts\HttpClient\HttpClientInterface;
1617
use Psr\Log\LoggerInterface;
@@ -99,6 +100,11 @@ public function tagApi(string|int $spaceId): TagApi
99100
return new TagApi($this->httpClient, $spaceId);
100101
}
101102

103+
public function workflowApi(string|int $spaceId): WorkflowApi
104+
{
105+
return new WorkflowApi($this->httpClient, $spaceId);
106+
}
107+
102108
public function managementApi(): ManagementApi
103109
{
104110
return new ManagementApi($this->httpClient);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"workflows": [
3+
{
4+
"id": 12345,
5+
"content_types": [
6+
"article"
7+
],
8+
"is_default": false,
9+
"name": "Article "
10+
},
11+
{
12+
"id": 12346,
13+
"content_types": [
14+
],
15+
"is_default": true,
16+
"name": "Default one"
17+
}
18+
]
19+
}

0 commit comments

Comments
 (0)