Skip to content

Commit

Permalink
Enhancement: Introduce StoryRequest object (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
silasjoisten authored Jan 17, 2025
1 parent 9098c90 commit a19804b
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 40 deletions.
65 changes: 41 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ In your code you should type-hint to `Storyblok\Api\StoriesApiInterface`
```php
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoriesRequest;

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(locale: 'de');
$response = $storiesApi->all(new StoriesRequest(language: 'de'));
```

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

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

$storiesApi = new StoriesApi($client, Version::Draft);
$response = $storiesApi->bySlug(
locale: 'de',
slug: '/my-story/',
);
$response = $storiesApi->bySlug('/my-story/', new StoryRequest(
language: 'de',
));
```

#### Method Call
Expand All @@ -93,15 +94,15 @@ $response = $storiesApi->bySlug(
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Request\StoryRequest;

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

$storiesApi = new StoriesApi($client, Version::Published);
$response = $storiesApi->bySlug(
locale: 'de',
slug: '/my-story/',
$response = $storiesApi->bySlug('/my-story/', new StoryRequest(
language: 'de',
version: Version::Draft, // This overrides the global "version"
);
));
```

### Pagination
Expand All @@ -110,14 +111,15 @@ $response = $storiesApi->bySlug(
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\Pagination;
use Storyblok\Api\Request\StoriesRequest;

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(
locale: 'de',
$response = $storiesApi->all(new StoriesRequest(
language: 'de',
pagination: new Pagination(page: 1, perPage: 30)
);
));
```

#### Sorting
Expand All @@ -127,14 +129,15 @@ use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\SortBy;
use Storyblok\Api\Domain\Value\Dto\Direction;
use Storyblok\Api\Request\StoriesRequest;

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(
locale: 'de',
$response = $storiesApi->all(new StoriesRequest(
language: 'de',
sortBy: new SortBy(field: 'title', direction: Direction::Desc)
);
));
```

#### Filtering
Expand All @@ -145,16 +148,17 @@ use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Filter\FilterCollection;
use Storyblok\Api\Domain\Value\Dto\Direction;
use Storyblok\Api\Domain\Value\Filter\Filters\InFilter;
use Storyblok\Api\Request\StoriesRequest;

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(
locale: 'de',
$response = $storiesApi->all(new StoriesRequest(
language: 'de',
filters: new FilterCollection([
new InFilter(field: 'single_reference_field', value: 'f2fdb571-a265-4d8a-b7c5-7050d23c2383')
])
);
));
```

#### Available filters
Expand Down Expand Up @@ -310,11 +314,14 @@ new OrFilter(
```php
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoriesRequest;

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->allByContentType('custom_content_type', locale: 'de');
$response = $storiesApi->allByContentType('custom_content_type', new StoriesRequest(
language: 'de',
));
```

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

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

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->byUuid($uuid, locale: 'de');
$response = $storiesApi->byUuid($uuid, new StoryRequest(
language: 'de',
));
```

### Get by slug (`string`)

```php
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoryRequest;

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->bySlug('folder/slug', locale: 'de');
$response = $storiesApi->bySlug('folder/slug', new StoryRequest(
language: 'de',
));
```


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

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

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

$storiesApi = new StoriesApi($client);
$response = $storiesApi->byId($id, locale: 'de');
$response = $storiesApi->byId($id, new StoryRequest(
language: 'de',
));
```


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

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

$linksApi = new LinksApi($client);
$response = $linksApi->all(
$response = $linksApi->all(new LinksRequest(
pagination: new Pagination(page: 1, perPage: 1000)
);
));
```

### Get by parent (`Storyblok\Api\Domain\Value\Id`)
Expand Down
49 changes: 49 additions & 0 deletions src/Request/StoryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* This file is part of Storyblok-Api.
*
* (c) SensioLabs Deutschland <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Storyblok\Api\Request;

use Storyblok\Api\Domain\Value\Dto\Version;
use Webmozart\Assert\Assert;

/**
* @author Silas Joisten <[email protected]>
*/
final readonly class StoryRequest
{
public function __construct(
public string $language = 'default',
public ?Version $version = null,
) {
Assert::stringNotEmpty($language);
}

/**
* @return array{
* language: string,
* version?: string,
* }
*/
public function toArray(): array
{
$array = [
'language' => $this->language,
];

if (null !== $this->version) {
$array['version'] = $this->version->value;
}

return $array;
}
}
26 changes: 14 additions & 12 deletions src/StoriesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Storyblok\Api\Domain\Value\Total;
use Storyblok\Api\Domain\Value\Uuid;
use Storyblok\Api\Request\StoriesRequest;
use Storyblok\Api\Request\StoryRequest;
use Storyblok\Api\Response\StoriesResponse;
use Storyblok\Api\Response\StoryResponse;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -80,44 +81,45 @@ public function allByContentType(string $contentType, ?StoriesRequest $request =
);
}

public function bySlug(string $slug, string $language = 'default', ?Version $version = null): StoryResponse
public function bySlug(string $slug, ?StoryRequest $request = null): StoryResponse
{
Assert::stringNotEmpty($language);
Assert::stringNotEmpty($slug);

$request ??= new StoryRequest();

$response = $this->client->request('GET', \sprintf('%s/%s', self::ENDPOINT, $slug), [
'query' => [
'language' => $language,
'version' => null !== $version ? $version->value : $this->version->value,
...$request->toArray(),
'version' => null !== $request->version ? $request->version->value : $this->version->value,
],
]);

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

public function byUuid(Uuid $uuid, string $language = 'default', ?Version $version = null): StoryResponse
public function byUuid(Uuid $uuid, ?StoryRequest $request = null): StoryResponse
{
Assert::stringNotEmpty($language);
$request ??= new StoryRequest();

$response = $this->client->request('GET', \sprintf('%s/%s', self::ENDPOINT, $uuid->value), [
'query' => [
'language' => $language,
...$request->toArray(),
'find_by' => 'uuid',
'version' => null !== $version ? $version->value : $this->version->value,
'version' => null !== $request->version ? $request->version->value : $this->version->value,
],
]);

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

public function byId(Id $id, string $language = 'default', ?Version $version = null): StoryResponse
public function byId(Id $id, ?StoryRequest $request = null): StoryResponse
{
Assert::stringNotEmpty($language);
$request ??= new StoryRequest();

$response = $this->client->request('GET', \sprintf('/v2/cdn/stories/%s', $id->value), [
'query' => [
'language' => $language,
'version' => null !== $version ? $version->value : $this->version->value,
...$request->toArray(),
'version' => null !== $request->version ? $request->version->value : $this->version->value,
],
]);

Expand Down
8 changes: 4 additions & 4 deletions src/StoriesApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Storyblok\Api;

use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Domain\Value\Id;
use Storyblok\Api\Domain\Value\Uuid;
use Storyblok\Api\Request\StoriesRequest;
use Storyblok\Api\Request\StoryRequest;
use Storyblok\Api\Response\StoriesResponse;
use Storyblok\Api\Response\StoryResponse;

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

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

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

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

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

0 comments on commit a19804b

Please sign in to comment.