Skip to content

Commit a19804b

Browse files
authored
Enhancement: Introduce StoryRequest object (#9)
1 parent 9098c90 commit a19804b

File tree

5 files changed

+178
-40
lines changed

5 files changed

+178
-40
lines changed

README.md

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ In your code you should type-hint to `Storyblok\Api\StoriesApiInterface`
6262
```php
6363
use Storyblok\Api\StoriesApi;
6464
use Storyblok\Api\StoryblokClient;
65+
use Storyblok\Api\Request\StoriesRequest;
6566

6667
$client = new StoryblokClient(/* ... */);
6768

6869
$storiesApi = new StoriesApi($client);
69-
$response = $storiesApi->all(locale: 'de');
70+
$response = $storiesApi->all(new StoriesRequest(language: 'de'));
7071
```
7172

7273
### Fetch by Version (`draft`, `published`)
@@ -77,14 +78,14 @@ $response = $storiesApi->all(locale: 'de');
7778
use Storyblok\Api\StoriesApi;
7879
use Storyblok\Api\StoryblokClient;
7980
use Storyblok\Api\Domain\Value\Dto\Version;
81+
use Storyblok\Api\Request\StoryRequest;
8082

8183
$client = new StoryblokClient(/* ... */);
8284

8385
$storiesApi = new StoriesApi($client, Version::Draft);
84-
$response = $storiesApi->bySlug(
85-
locale: 'de',
86-
slug: '/my-story/',
87-
);
86+
$response = $storiesApi->bySlug('/my-story/', new StoryRequest(
87+
language: 'de',
88+
));
8889
```
8990

9091
#### Method Call
@@ -93,15 +94,15 @@ $response = $storiesApi->bySlug(
9394
use Storyblok\Api\StoriesApi;
9495
use Storyblok\Api\StoryblokClient;
9596
use Storyblok\Api\Domain\Value\Dto\Version;
97+
use Storyblok\Api\Request\StoryRequest;
9698

9799
$client = new StoryblokClient(/* ... */);
98100

99101
$storiesApi = new StoriesApi($client, Version::Published);
100-
$response = $storiesApi->bySlug(
101-
locale: 'de',
102-
slug: '/my-story/',
102+
$response = $storiesApi->bySlug('/my-story/', new StoryRequest(
103+
language: 'de',
103104
version: Version::Draft, // This overrides the global "version"
104-
);
105+
));
105106
```
106107

107108
### Pagination
@@ -110,14 +111,15 @@ $response = $storiesApi->bySlug(
110111
use Storyblok\Api\StoriesApi;
111112
use Storyblok\Api\StoryblokClient;
112113
use Storyblok\Api\Domain\Value\Dto\Pagination;
114+
use Storyblok\Api\Request\StoriesRequest;
113115

114116
$client = new StoryblokClient(/* ... */);
115117

116118
$storiesApi = new StoriesApi($client);
117-
$response = $storiesApi->all(
118-
locale: 'de',
119+
$response = $storiesApi->all(new StoriesRequest(
120+
language: 'de',
119121
pagination: new Pagination(page: 1, perPage: 30)
120-
);
122+
));
121123
```
122124

123125
#### Sorting
@@ -127,14 +129,15 @@ use Storyblok\Api\StoriesApi;
127129
use Storyblok\Api\StoryblokClient;
128130
use Storyblok\Api\Domain\Value\Dto\SortBy;
129131
use Storyblok\Api\Domain\Value\Dto\Direction;
132+
use Storyblok\Api\Request\StoriesRequest;
130133

131134
$client = new StoryblokClient(/* ... */);
132135

133136
$storiesApi = new StoriesApi($client);
134-
$response = $storiesApi->all(
135-
locale: 'de',
137+
$response = $storiesApi->all(new StoriesRequest(
138+
language: 'de',
136139
sortBy: new SortBy(field: 'title', direction: Direction::Desc)
137-
);
140+
));
138141
```
139142

140143
#### Filtering
@@ -145,16 +148,17 @@ use Storyblok\Api\StoryblokClient;
145148
use Storyblok\Api\Domain\Value\Filter\FilterCollection;
146149
use Storyblok\Api\Domain\Value\Dto\Direction;
147150
use Storyblok\Api\Domain\Value\Filter\Filters\InFilter;
151+
use Storyblok\Api\Request\StoriesRequest;
148152

149153
$client = new StoryblokClient(/* ... */);
150154

151155
$storiesApi = new StoriesApi($client);
152-
$response = $storiesApi->all(
153-
locale: 'de',
156+
$response = $storiesApi->all(new StoriesRequest(
157+
language: 'de',
154158
filters: new FilterCollection([
155159
new InFilter(field: 'single_reference_field', value: 'f2fdb571-a265-4d8a-b7c5-7050d23c2383')
156160
])
157-
);
161+
));
158162
```
159163

160164
#### Available filters
@@ -310,11 +314,14 @@ new OrFilter(
310314
```php
311315
use Storyblok\Api\StoriesApi;
312316
use Storyblok\Api\StoryblokClient;
317+
use Storyblok\Api\Request\StoriesRequest;
313318

314319
$client = new StoryblokClient(/* ... */);
315320

316321
$storiesApi = new StoriesApi($client);
317-
$response = $storiesApi->allByContentType('custom_content_type', locale: 'de');
322+
$response = $storiesApi->allByContentType('custom_content_type', new StoriesRequest(
323+
language: 'de',
324+
));
318325
```
319326

320327
### Get by uuid (`Storyblok\Api\Domain\Value\Uuid`)
@@ -323,25 +330,31 @@ $response = $storiesApi->allByContentType('custom_content_type', locale: 'de');
323330
use Storyblok\Api\StoriesApi;
324331
use Storyblok\Api\StoryblokClient;
325332
use Storyblok\Api\Domain\Value\Uuid;
333+
use Storyblok\Api\Request\StoryRequest;
326334

327335
$uuid = new Uuid(/** ... */);
328336

329337
$client = new StoryblokClient(/* ... */);
330338

331339
$storiesApi = new StoriesApi($client);
332-
$response = $storiesApi->byUuid($uuid, locale: 'de');
340+
$response = $storiesApi->byUuid($uuid, new StoryRequest(
341+
language: 'de',
342+
));
333343
```
334344

335345
### Get by slug (`string`)
336346

337347
```php
338348
use Storyblok\Api\StoriesApi;
339349
use Storyblok\Api\StoryblokClient;
350+
use Storyblok\Api\Request\StoryRequest;
340351

341352
$client = new StoryblokClient(/* ... */);
342353

343354
$storiesApi = new StoriesApi($client);
344-
$response = $storiesApi->bySlug('folder/slug', locale: 'de');
355+
$response = $storiesApi->bySlug('folder/slug', new StoryRequest(
356+
language: 'de',
357+
));
345358
```
346359

347360

@@ -351,13 +364,16 @@ $response = $storiesApi->bySlug('folder/slug', locale: 'de');
351364
use Storyblok\Api\StoriesApi;
352365
use Storyblok\Api\StoryblokClient;
353366
use Storyblok\Api\Domain\Value\Id;
367+
use Storyblok\Api\Request\StoryRequest;
354368

355369
$id = new Id(/** ... */);
356370

357371
$client = new StoryblokClient(/* ... */);
358372

359373
$storiesApi = new StoriesApi($client);
360-
$response = $storiesApi->byId($id, locale: 'de');
374+
$response = $storiesApi->byId($id, new StoryRequest(
375+
language: 'de',
376+
));
361377
```
362378

363379

@@ -383,13 +399,14 @@ $response = $linksApi->all();
383399
use Storyblok\Api\LinksApi;
384400
use Storyblok\Api\StoryblokClient;
385401
use Storyblok\Api\Domain\Value\Dto\Pagination;
402+
use Storyblok\Api\Request\LinksRequest;
386403

387404
$client = new StoryblokClient(/* ... */);
388405

389406
$linksApi = new LinksApi($client);
390-
$response = $linksApi->all(
407+
$response = $linksApi->all(new LinksRequest(
391408
pagination: new Pagination(page: 1, perPage: 1000)
392-
);
409+
));
393410
```
394411

395412
### Get by parent (`Storyblok\Api\Domain\Value\Id`)

src/Request/StoryRequest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of Storyblok-Api.
7+
*
8+
* (c) SensioLabs Deutschland <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Storyblok\Api\Request;
15+
16+
use Storyblok\Api\Domain\Value\Dto\Version;
17+
use Webmozart\Assert\Assert;
18+
19+
/**
20+
* @author Silas Joisten <[email protected]>
21+
*/
22+
final readonly class StoryRequest
23+
{
24+
public function __construct(
25+
public string $language = 'default',
26+
public ?Version $version = null,
27+
) {
28+
Assert::stringNotEmpty($language);
29+
}
30+
31+
/**
32+
* @return array{
33+
* language: string,
34+
* version?: string,
35+
* }
36+
*/
37+
public function toArray(): array
38+
{
39+
$array = [
40+
'language' => $this->language,
41+
];
42+
43+
if (null !== $this->version) {
44+
$array['version'] = $this->version->value;
45+
}
46+
47+
return $array;
48+
}
49+
}

src/StoriesApi.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Storyblok\Api\Domain\Value\Total;
1919
use Storyblok\Api\Domain\Value\Uuid;
2020
use Storyblok\Api\Request\StoriesRequest;
21+
use Storyblok\Api\Request\StoryRequest;
2122
use Storyblok\Api\Response\StoriesResponse;
2223
use Storyblok\Api\Response\StoryResponse;
2324
use Webmozart\Assert\Assert;
@@ -80,44 +81,45 @@ public function allByContentType(string $contentType, ?StoriesRequest $request =
8081
);
8182
}
8283

83-
public function bySlug(string $slug, string $language = 'default', ?Version $version = null): StoryResponse
84+
public function bySlug(string $slug, ?StoryRequest $request = null): StoryResponse
8485
{
85-
Assert::stringNotEmpty($language);
8686
Assert::stringNotEmpty($slug);
8787

88+
$request ??= new StoryRequest();
89+
8890
$response = $this->client->request('GET', \sprintf('%s/%s', self::ENDPOINT, $slug), [
8991
'query' => [
90-
'language' => $language,
91-
'version' => null !== $version ? $version->value : $this->version->value,
92+
...$request->toArray(),
93+
'version' => null !== $request->version ? $request->version->value : $this->version->value,
9294
],
9395
]);
9496

9597
return new StoryResponse($response->toArray());
9698
}
9799

98-
public function byUuid(Uuid $uuid, string $language = 'default', ?Version $version = null): StoryResponse
100+
public function byUuid(Uuid $uuid, ?StoryRequest $request = null): StoryResponse
99101
{
100-
Assert::stringNotEmpty($language);
102+
$request ??= new StoryRequest();
101103

102104
$response = $this->client->request('GET', \sprintf('%s/%s', self::ENDPOINT, $uuid->value), [
103105
'query' => [
104-
'language' => $language,
106+
...$request->toArray(),
105107
'find_by' => 'uuid',
106-
'version' => null !== $version ? $version->value : $this->version->value,
108+
'version' => null !== $request->version ? $request->version->value : $this->version->value,
107109
],
108110
]);
109111

110112
return new StoryResponse($response->toArray());
111113
}
112114

113-
public function byId(Id $id, string $language = 'default', ?Version $version = null): StoryResponse
115+
public function byId(Id $id, ?StoryRequest $request = null): StoryResponse
114116
{
115-
Assert::stringNotEmpty($language);
117+
$request ??= new StoryRequest();
116118

117119
$response = $this->client->request('GET', \sprintf('/v2/cdn/stories/%s', $id->value), [
118120
'query' => [
119-
'language' => $language,
120-
'version' => null !== $version ? $version->value : $this->version->value,
121+
...$request->toArray(),
122+
'version' => null !== $request->version ? $request->version->value : $this->version->value,
121123
],
122124
]);
123125

src/StoriesApiInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
namespace Storyblok\Api;
1515

16-
use Storyblok\Api\Domain\Value\Dto\Version;
1716
use Storyblok\Api\Domain\Value\Id;
1817
use Storyblok\Api\Domain\Value\Uuid;
1918
use Storyblok\Api\Request\StoriesRequest;
19+
use Storyblok\Api\Request\StoryRequest;
2020
use Storyblok\Api\Response\StoriesResponse;
2121
use Storyblok\Api\Response\StoryResponse;
2222

@@ -31,9 +31,9 @@ public function all(?StoriesRequest $request = null): StoriesResponse;
3131

3232
public function allByContentType(string $contentType, ?StoriesRequest $request = null): StoriesResponse;
3333

34-
public function bySlug(string $slug, string $language = 'default', ?Version $version = null): StoryResponse;
34+
public function bySlug(string $slug, ?StoryRequest $request = null): StoryResponse;
3535

36-
public function byUuid(Uuid $uuid, string $language = 'default', ?Version $version = null): StoryResponse;
36+
public function byUuid(Uuid $uuid, ?StoryRequest $request = null): StoryResponse;
3737

38-
public function byId(Id $id, string $language = 'default', ?Version $version = null): StoryResponse;
38+
public function byId(Id $id, ?StoryRequest $request = null): StoryResponse;
3939
}

0 commit comments

Comments
 (0)