Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: storyblok/storyblok-php-client
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.5.0
Choose a base ref
...
head repository: storyblok/storyblok-php-client
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.6.0
Choose a head ref
  • 18 commits
  • 4 files changed
  • 2 contributors

Commits on Feb 14, 2023

  1. Merge pull request #1 from storyblok/master

    Merge from the main repo
    roberto-butti authored Feb 14, 2023
    Copy the full SHA
    c30a1ec View commit details
  2. Updating Changelog

    roberto-butti committed Feb 14, 2023
    Copy the full SHA
    c50147a View commit details
  3. Merge pull request #85 from roberto-butti/feature/update-changelog

    Feature/update changelog
    joaokamun authored Feb 14, 2023
    Copy the full SHA
    5ad38c9 View commit details

Commits on Apr 6, 2023

  1. fix: resolve relations

    Use case:
    fixing resolving relation for nested stories with rels and not rel_uuids
    roberto-butti committed Apr 6, 2023
    Copy the full SHA
    af59b35 View commit details
  2. Update ClientTest.php

    roberto-butti committed Apr 6, 2023
    Copy the full SHA
    fe30964 View commit details

Commits on Apr 7, 2023

  1. Copy the full SHA
    bb60345 View commit details
  2. Copy the full SHA
    a3c82ad View commit details
  3. Update ClientTest.php

    roberto-butti committed Apr 7, 2023
    Copy the full SHA
    167b523 View commit details
  4. Copy the full SHA
    faea8ac View commit details
  5. Copy the full SHA
    ed0be33 View commit details
  6. Copy the full SHA
    aea2265 View commit details

Commits on Apr 8, 2023

  1. Resolving nested relations, 2nd level

    - introducing level management for "enrich content"
    - adding tests: for < 50 rels (nested rels)
    - adding tests: (nested rels)
    - adding tests: stop resolving loop
    roberto-butti committed Apr 8, 2023
    Copy the full SHA
    8e39c9d View commit details

Commits on Apr 11, 2023

  1. Copy the full SHA
    cda63f3 View commit details

Commits on Apr 12, 2023

  1. syntax fix

    joaokamun committed Apr 12, 2023
    Copy the full SHA
    215af72 View commit details
  2. Copy the full SHA
    c5d37dd View commit details

Commits on Apr 13, 2023

  1. adjust on singleOption

    joaokamun committed Apr 13, 2023
    Copy the full SHA
    9e383fe View commit details
  2. adjusted tests and syntax

    joaokamun committed Apr 13, 2023
    Copy the full SHA
    33d17fa View commit details
  3. Merge pull request #87 from storyblok/fix/INT-859-level

    Fix/int 859 level
    joaokamun authored Apr 13, 2023
    Copy the full SHA
    f681de0 View commit details
Showing with 178 additions and 19 deletions.
  1. +2 −2 CHANGELOG.md
  2. +1 −1 composer.json
  3. +51 −14 src/Storyblok/Client.php
  4. +124 −2 tests/Storyblok/Integration/ClientTest.php
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## 2.5.0/3.0.0 - WIP
Release: N/A
## 2.5.0 - 2023-02-14
Release: [v2.5.0](https://github.com/storyblok/php-client/releases/tag/v2.5.0)

### Added
- Add **getAll()** method function for retrieving all the entries (automatic pagination resolution) thanks to @web-dev-passion
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "storyblok/php-client",
"description": "Storyblok Client to easily call the publishing api",
"type": "library",
"keywords": ["storyblok"],
"keywords": ["storyblok", "api", "integration", "cms"],
"license": "MIT",
"minimum-stability": "stable",
"require": {
65 changes: 51 additions & 14 deletions src/Storyblok/Client.php
Original file line number Diff line number Diff line change
@@ -657,6 +657,7 @@ function getResolvedRelations($data, $queryString)
$relations = $data['rels'];
} elseif (isset($data['rel_uuids'])) {
$relSize = \count($data['rel_uuids']);

$chunks = [];
$chunkSize = 50;

@@ -734,27 +735,35 @@ function getResolvedLinks($data, array $queryString)
* Enrich the Stories with resolved links and stories.
*
* @param array|\stdClass|string $data
* @param mixed $level
*
* @return array|string
*/
public function enrichContent($data)
public function enrichContent($data, $level = 0)
{
$enrichedContent = $data;

if (isset($data['component'])) {
if (!isset($data['_stopResolving'])) {
if (!$this->isStopResolving($level)) {
foreach ($data as $fieldName => $fieldValue) {
if (isset($fieldValue['_stopResolving']) && $fieldValue['_stopResolving']) {
continue;
}

$enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue);
$enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]);
$enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName]);
$enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName], $level + 1);
}
}
} elseif (\is_array($data)) {
if (!isset($data['_stopResolving'])) {
if (!$this->isStopResolving($level)) {
foreach ($data as $key => $value) {
if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations)) {
$enrichedContent[$key] = $this->resolvedRelations[$value];
if ('uuid' !== $key) {
$enrichedContent[$key] = $this->resolvedRelations[$value];
}
} else {
$enrichedContent[$key] = $this->enrichContent($value);
$enrichedContent[$key] = $this->enrichContent($value, $level + 1);
}
}
}
@@ -871,9 +880,9 @@ private function enrichStories($data, $queryString)
$this->getResolvedLinks($data, $queryString);

if (isset($data['story'])) {
if (isset($enrichedData['rel_uuids'])) {
$enrichedData['rels'] = $this->resolvedRelations;
}
// if (isset($enrichedData['rel_uuids'])) {
// $enrichedData['rels'] = $this->resolvedRelations;
// }
$enrichedData['story']['content'] = $this->enrichContent($data['story']['content']);
} elseif (isset($data['stories'])) {
$stories = [];
@@ -885,6 +894,18 @@ private function enrichStories($data, $queryString)
$enrichedData['stories'] = $stories;
}

if (!empty($data['rels'])) {
foreach ($data['rels'] as $index => $rel) {
$enrichedData['rels'][$index] = $this->enrichContent($rel, -1);
}
}

if (!empty($data['links'])) {
foreach ($data['links'] as $index => $rel) {
$enrichedData['links'][$index] = $this->enrichContent($rel, -1);
}
}

return $enrichedData;
}

@@ -898,22 +919,28 @@ private function enrichStories($data, $queryString)
private function insertRelations($component, $field, $value)
{
$filteredNode = $value;

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];
$filteredNode['_stopResolving'] = true;
$this->settingStopResolving($filteredNode);
}
} elseif (\is_array($value)) {
$filteredNode = [];
$filteredNodeTemp = [];
$resolved = false;

foreach ($value as $item) {
if (\is_string($item) && isset($this->resolvedRelations[$item])) {
$resolved = true;
$story = $this->resolvedRelations[$item];
$story['_stopResolving'] = true;
$filteredNode[] = $story;
$this->settingStopResolving($story);
$filteredNodeTemp[] = $story;
}
}

if ($resolved) {
$filteredNode = $filteredNodeTemp;
}
}
}

@@ -1072,4 +1099,14 @@ private function _getCacheKey($key = '')
{
return hash('sha256', $key);
}

private function settingStopResolving(&$data)
{
$data['_stopResolving'] = true;
}

private function isStopResolving($level)
{
return $level > 4;
}
}
126 changes: 124 additions & 2 deletions tests/Storyblok/Integration/ClientTest.php
Original file line number Diff line number Diff line change
@@ -142,7 +142,7 @@
);
$responses = $client->getStoryBySlug($slug);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']);
expect($body)->toHaveKeys(['rel_uuids', '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');
@@ -151,7 +151,53 @@
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);
expect($body['rel_uuids'])->toHaveLength(51);
})->group('integration');

test('Integration: get one story with few resolved relations', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');
$slug = 'categories/category-shoe-001';
$key = 'stories/' . $slug;
$client->editMode();
$options = $client->getApiParameters();
$client->resolveRelations(
'ProductCategory.Products,Product.ProductVariants'
);
$responses = $client->getStoryBySlug($slug);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']);
expect($body['story']['content']['Products'])->toBeArray();
expect($body['story']['content']['Products'])->toHaveLength(1);
expect($body['story']['content']['Products'][0]['content']['productname'])->toEqual('Shoe 001');
expect($body['story']['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3);
expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0'])->toBeArray();
expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0']['name'])->toEqual('Shoe 001 Blue');
expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0']['content'])->toBeArray();
expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue');
expect($body['rels'])->toHaveLength(4);
})->group('integration');

test('Integration: get one story from Product', 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']);
// print_r($body);
expect($body['story']['content']['productname'])->toEqual('Bike 001');
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']['0']['name'])->toEqual('Bike 001 L');
expect($body['story']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Bike 001 L');
})->group('integration');

test('Integration: get one story with Resolved relations 2', function () {
@@ -175,6 +221,60 @@
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['story']['content']['MainProduct']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Bike 001 L');
})->group('integration');

test('Integration: get list of stories story with Resolved relations 2', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');

$key = 'stories/';
$params = [
'starts_with' => 'categories/category-shoe',
'content_type' => 'ProductCategory',
];
$client->editMode();
$options = $client->getApiParameters();
$client->resolveRelations(
'ProductCategory.Products,Product.ProductVariants'
);
$responses = $client->getStories($params);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'stories', 'cv', 'links']);
expect($body['stories'][0]['name'])->toEqual('Category Shoe 001');

expect($body['stories'][0]['content']['Products'])->toBeArray();
expect($body['stories'][0]['content']['Products'])->toHaveLength(1);
expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001');
expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3);
expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue');
})->group('integration');

test('Integration: get list of stories -translations- story with Resolved relations 2', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');

$key = 'stories/';
$params = [
'starts_with' => 'categories/category-shoe',
'content_type' => 'ProductCategory',
];
$client->editMode();
$client->language('it');
$options = $client->getApiParameters();
$client->resolveRelations(
'ProductCategory.Products,Product.ProductVariants'
);
$responses = $client->getStories($params);
$body = $responses->getBody();
expect($body)->toHaveKeys(['rels', 'stories', 'cv', 'links']);
expect($body['stories'][0]['name'])->toEqual('Category Shoe 001');

expect($body['stories'][0]['content']['Products'])->toBeArray();
expect($body['stories'][0]['content']['Products'])->toHaveLength(1);
expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001');
expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3);
expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Scarpa 001 Blu');
})->group('integration');

test('Integration: get one story with Resolved relations 3', function () {
@@ -195,3 +295,25 @@
expect($body['story']['content']['ProductVariants']['0']['name'])->toBeString();
expect($body['story']['content']['ProductVariants']['1']['name'])->toBeString();
})->group('integration');

test('Integration: test stop resolving loop', function () {
unset($_GET['_storyblok_published']);
$client = new Client('HMqBPn2a92FjXYI3tQGDVQtt');
$slug = 'testlevel';
$key = 'stories/' . $slug;
$client->editMode();
$options = $client->getApiParameters();
$client->resolveRelations(
'level-1.related,level-2.related'
);
$responses = $client->getStoryBySlug($slug);
$body = $responses->getBody();

expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']);
expect($body['story']['content']['related'])->toBeArray();
expect($body['story']['content']['related']['name'])->toEqual('TestLevel2');
expect($body['story']['content']['related']['_stopResolving'])->toEqual(1);
expect($body['story']['content']['related']['content']['related']['name'])->toEqual('TestLevel');
expect($body['story']['content']['related']['content']['related']['content']['related'])->toBeArray();
expect($body['story']['content']['related']['content']['related']['content']['related']['content']['related'])->toBeString();
})->group('integration');