Skip to content

Commit

Permalink
Merge pull request #83 from roberto-butti/fix/resolverelations
Browse files Browse the repository at this point in the history
  • Loading branch information
joaokamun authored Feb 14, 2023
2 parents d67a198 + f7b60fc commit e8f1743
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/Storyblok/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ public function getStories($options = [])
}

/**
* Sets global reference.
* Sets the list of the relations to be resolved.
*
* eg. global.global_referece
* eg. 'article-page.author'
*
* @param mixed $reference
* @param string $reference
*
* @return $this
*/
Expand All @@ -446,7 +446,10 @@ public function resolveRelations($reference)
$this->_relationsList = [];
foreach (explode(',', $this->resolveRelations) as $relation) {
$relationVars = explode('.', $relation);
$this->_relationsList[$relationVars[0]] = $relationVars[1];
if (!\array_key_exists($relationVars[0], $this->_relationsList)) {
$this->_relationsList[$relationVars[0]] = [];
}
$this->_relationsList[$relationVars[0]][] = $relationVars[1];
}

return $this;
Expand Down Expand Up @@ -681,6 +684,15 @@ function getResolvedRelations($data, $queryString)
}
}

public function getResolvedRelationByUuid($uuid)
{
if (\array_key_exists($uuid, $this->resolvedRelations)) {
return $this->resolvedRelations[$uuid];
}

return false;
}

/**
* Retrieve or resolve the Links.
*
Expand Down Expand Up @@ -728,8 +740,6 @@ function getResolvedLinks($data, array $queryString)
public function enrichContent($data)
{
$enrichedContent = $data;

// if (\is_array($data) && isset($data['component'])) {
if (isset($data['component'])) {
if (!isset($data['_stopResolving'])) {
foreach ($data as $fieldName => $fieldValue) {
Expand All @@ -739,8 +749,14 @@ public function enrichContent($data)
}
}
} elseif (\is_array($data)) {
foreach ($data as $key => $value) {
$enrichedContent[$key] = $this->enrichContent($value);
if (!isset($data['_stopResolving'])) {
foreach ($data as $key => $value) {
if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations)) {
$enrichedContent[$key] = $this->resolvedRelations[$value];
} else {
$enrichedContent[$key] = $this->enrichContent($value);
}
}
}
}

Expand Down Expand Up @@ -855,6 +871,9 @@ private function enrichStories($data, $queryString)
$this->getResolvedLinks($data, $queryString);

if (isset($data['story'])) {
if (isset($enrichedData['rel_uuids'])) {
$enrichedData['rels'] = $this->resolvedRelations;
}
$enrichedData['story']['content'] = $this->enrichContent($data['story']['content']);
} elseif (isset($data['stories'])) {
$stories = [];
Expand All @@ -879,7 +898,8 @@ private function enrichStories($data, $queryString)
private function insertRelations($component, $field, $value)
{
$filteredNode = $value;
if (isset($this->_relationsList[$component]) && $field === $this->_relationsList[$component]) {

if (isset($this->_relationsList[$component]) && \in_array($field, $this->_relationsList[$component], true)) {
if (\is_string($value)) {
if (isset($this->resolvedRelations[$value])) {
$filteredNode = $this->resolvedRelations[$value];
Expand Down
1 change: 1 addition & 0 deletions tests/Storyblok/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
$this->assertArrayHasKey('rels', $story);
// With the dataset provided the right order should be
// ["997cfee2-cec3-4cad-9e06-7ef3d72f5b7d", "ba307a05-2b91-4ce8-ab24-bf328da1adb4", "c3a321a5-1163-4281-8643-2c79c13e22a3"]

$this->assertEquals(
'997cfee2-cec3-4cad-9e06-7ef3d72f5b7d',
$story['story']['content']['body'][1]['articles'][0]['uuid'],
Expand Down
66 changes: 66 additions & 0 deletions tests/Storyblok/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,69 @@
$identifier1 = $responses->getHeaders()['X-Request-Id'];
expect($identifier1[0])->not()->toEqual($identifier3[0]);
})->group('integration');

test('Integration: get one story with Resolved relations 1', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');
$slug = 'home';
$key = 'stories/' . $slug;
$client->editMode();
$options = $client->getApiParameters();
$client->resolveRelations(
'page.RelatedProducts,page.MainProduct,page.FeaturedCategoryProducts,ProductCategory.Products,Product.ProductVariants'
);
$responses = $client->getStoryBySlug($slug);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']);
expect($body['story']['content']['FeaturedCategoryProducts'])->toBeArray();
expect($body['story']['content']['FeaturedCategoryProducts']['0']['name'])->toEqual('Category 001');
expect($body['story']['content']['FeaturedCategoryProducts']['1']['name'])->toEqual('Category A');
expect($body['story']['content']['FeaturedCategoryProducts']['2']['name'])->toEqual('Category B');
expect($body['story']['content']['FeaturedCategoryProducts'])->toHaveLength(3);
expect($body['story']['content']['MainProduct'])->toBeArray();
expect($body['story']['content']['MainProduct']['name'])->toEqual('Bike 001');
expect($body['story']['content']['MainProduct']['content']['ProductVariants'])->toBeArray()->toHaveLength(2);
expect($body['rels'])->toHaveLength(51);
})->group('integration');

test('Integration: get one story with Resolved relations 2', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');
$slug = 'home';
$key = 'stories/' . $slug;
$client->editMode();
$options = $client->getApiParameters();
$client->resolveRelations(
'page.MainProduct,ProductCategory.Products,Product.ProductVariants'
);
$responses = $client->getStoryBySlug($slug);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']);
expect($body['story']['content']['FeaturedCategoryProducts'])->toBeArray();
expect($body['story']['content']['FeaturedCategoryProducts'])->toHaveLength(3);
expect($body['story']['content']['FeaturedCategoryProducts']['0'])->toBeString();
expect($body['story']['content']['FeaturedCategoryProducts']['1'])->toBeString();
expect($body['story']['content']['FeaturedCategoryProducts']['2'])->toBeString();
expect($body['story']['content']['MainProduct'])->toBeArray();
expect($body['story']['content']['MainProduct']['name'])->toEqual('Bike 001');
expect($body['story']['content']['MainProduct']['content']['ProductVariants'])->toBeArray()->toHaveLength(2);
})->group('integration');

test('Integration: get one story with Resolved relations 3', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');
$slug = 'categories/products/bike-001';
$key = 'stories/' . $slug;
$client->editMode();
$options = $client->getApiParameters();
$client->resolveRelations(
'Product.ProductVariants'
);
$responses = $client->getStoryBySlug($slug);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']);
expect($body['story']['content']['ProductVariants'])->toBeArray();
expect($body['story']['content']['ProductVariants'])->toHaveLength(2);
expect($body['story']['content']['ProductVariants']['0']['name'])->toBeString();
expect($body['story']['content']['ProductVariants']['1']['name'])->toBeString();
})->group('integration');

0 comments on commit e8f1743

Please sign in to comment.