Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Automatic Relation Resolution #8

Merged
merged 10 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/Bridge/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
/**
* @author Silas Joisten <[email protected]>
*
* @method array assetResponse(array $overrides = [])
* @method array datasourceDimensionResponse(array $overrides = [])
* @method array datasourceEntriesResponse(array $overrides = [])
* @method array datasourceEntryResponse(array $overrides = [])
* @method array datasourceResponse(array $overrides = [])
* @method array datasourcesResponse(array $overrides = [])
* @method array linkAlternateResponse(array $overrides = [])
* @method array linkResponse(array $overrides = [])
* @method array linksResponse(array $overrides = [])
* @method array spaceResponse(array $overrides = [])
* @method array storiesResponse(array $overrides = [])
* @method array storyResponse(array $overrides = [])
* @method array tagsResponse(array $overrides = [])
* @method array assetResponse(array $overrides = [])
* @method array datasourceDimensionResponse(array $overrides = [])
* @method array datasourceEntriesResponse(array $overrides = [])
* @method array datasourceEntryResponse(array $overrides = [])
* @method array datasourceResponse(array $overrides = [])
* @method array datasourcesResponse(array $overrides = [])
* @method array linkAlternateResponse(array $overrides = [])
* @method array linkResponse(array $overrides = [])
* @method array linksResponse(array $overrides = [])
* @method string relation()
* @method array spaceResponse(array $overrides = [])
* @method array storiesResponse(array $overrides = [])
* @method array storyResponse(array $overrides = [])
* @method array tagsResponse(array $overrides = [])
*/
final class Generator extends BaseGenerator
{
Expand Down
9 changes: 9 additions & 0 deletions src/Bridge/Faker/Provider/StoryblokProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,13 @@ public function assetResponse(array $overrides = []): array
$overrides,
);
}

public function relation(): string
{
return \sprintf(
'%s.%s',
$this->generator->word(),
$this->generator->word(),
);
}
}
30 changes: 30 additions & 0 deletions src/Domain/Value/Resolver/Relation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Domain\Value\Resolver;

use OskarStark\Value\TrimmedNonEmptyString;
use Webmozart\Assert\Assert;

/**
* @author Silas Joisten <[email protected]>
*/
final readonly class Relation
{
public function __construct(
public string $value,
) {
TrimmedNonEmptyString::fromString($value);
Assert::regex($value, '/^([a-zA-Z].+)\.([a-zA-Z].+)$/');
}
}
87 changes: 87 additions & 0 deletions src/Domain/Value/Resolver/RelationCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Domain\Value\Resolver;

/**
* @author Silas Joisten <[email protected]>
*
* @implements \IteratorAggregate<int, Relation>
*/
final class RelationCollection implements \Countable, \IteratorAggregate
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @var list<Relation>
*/
private array $items = [];

/**
* @param list<Relation> $items
*/
public function __construct(
array $items = [],
) {
foreach ($items as $item) {
$this->add($item);
}
}
OskarStark marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return \Traversable<int, Relation>
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->items);
}

public function count(): int
{
return \count($this->items);
}

public function add(Relation $relation): void
{
if ($this->has($relation)) {
return;
}

$this->items[] = $relation;
}

public function has(Relation $relation): bool
{
foreach ($this->items as $item) {
if ($item->value === $relation->value) {
return true;
}
}

return false;
}

public function remove(Relation $relation): void
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
{
foreach ($this->items as $key => $item) {
if ($item->value === $relation->value) {
unset($this->items[$key]);

break;
}
}
}

public function toString(): string
{
return implode(',', array_map(static fn (Relation $relation): string => $relation->value, $this->items));
}
}
6 changes: 6 additions & 0 deletions src/Request/StoriesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Storyblok\Api\Domain\Value\Field\FieldCollection;
use Storyblok\Api\Domain\Value\Filter\FilterCollection;
use Storyblok\Api\Domain\Value\IdCollection;
use Storyblok\Api\Domain\Value\Resolver\RelationCollection;
use Storyblok\Api\Domain\Value\Tag\TagCollection;
use Webmozart\Assert\Assert;

Expand All @@ -38,6 +39,7 @@ public function __construct(
public ?FieldCollection $excludeFields = null,
public ?TagCollection $withTags = null,
public ?IdCollection $excludeIds = null,
public ?RelationCollection $withRelations = null,
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
public ?Version $version = null,
) {
Assert::stringNotEmpty($language);
Expand Down Expand Up @@ -85,6 +87,10 @@ public function toArray(): array
$array['excluding_ids'] = $this->excludeIds->toString();
}

if (null !== $this->withRelations && $this->withRelations->count() > 0) {
$array['resolve_relations'] = $this->withRelations->toString();
}

if (null !== $this->version) {
$array['version'] = $this->version->value;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Request/StoryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Storyblok\Api\Request;

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

/**
Expand All @@ -24,6 +25,7 @@
public function __construct(
public string $language = 'default',
public ?Version $version = null,
public ?RelationCollection $withRelations = null,
) {
Assert::stringNotEmpty($language);
}
Expand All @@ -32,6 +34,7 @@ public function __construct(
* @return array{
* language: string,
* version?: string,
* resolve_relations?: string,
* }
*/
public function toArray(): array
Expand All @@ -44,6 +47,10 @@ public function toArray(): array
$array['version'] = $this->version->value;
}

if (null !== $this->withRelations && $this->withRelations->count() > 0) {
$array['resolve_relations'] = $this->withRelations->toString();
}

return $array;
}
}
30 changes: 30 additions & 0 deletions src/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Resolver;

/**
* @author Silas Joisten <[email protected]>
*/
interface ResolverInterface
{
/**
* Resolves relations in the target content using the given relations collection.
*
* @param array<string, mixed> $target the target story content containing UUIDs to resolve
* @param array<int, array<string, mixed>> $relations the target story content containing UUIDs to resolve
*
* @return array<string, mixed>
*/
public function resolve(array $target, array $relations): array;
}
53 changes: 53 additions & 0 deletions src/Resolver/StoryResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Resolver;

use Webmozart\Assert\Assert;

/**
* @author Silas Joisten <[email protected]>
*/
final readonly class StoryResolver implements ResolverInterface
{
public function resolve(array $target, array $relations): array
silasjoisten marked this conversation as resolved.
Show resolved Hide resolved
{
$relationMap = [];

foreach ($relations as $key => $relation) {
Assert::keyExists($relation, 'uuid');
Assert::uuid($relation['uuid']);
$relationMap[$relation['uuid']] = $relation;

// There is a limit of possible resolvable relations.
// @see https://www.storyblok.com/docs/api/content-delivery/v2/stories/retrieve-a-single-story
if (50 === $key) {
break;
}
}

foreach ($target as &$value) {
if (\is_string($value) && \array_key_exists($value, $relationMap)) {
$value = $relationMap[$value];

continue;
}

if (\is_array($value)) {
$value = $this->resolve($value, $relations);
}
}

return $target;
}
}
2 changes: 1 addition & 1 deletion src/Response/StoriesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public int $cv;

/**
* @var list<string>
* @var list<array<string, mixed>>
*/
public array $rels;

Expand Down
2 changes: 1 addition & 1 deletion src/Response/StoryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public int $cv;

/**
* @var list<array<mixed>>
* @var list<array<string, mixed>>
*/
public array $rels;

Expand Down
Loading
Loading