storyblok / storyblok-php-client

Storyblok - PHP Client
https://www.storyblok.com
MIT License
33 stars 37 forks source link

Fix the resolveRelations() method for the case with relations with n… #83

Closed roberto-butti closed 1 year ago

roberto-butti commented 1 year ago

…ame conflict

Pull request type

Jira Link: [INT-766(url)

How to test this PR

This issue is in the resolveRelations() method in the Client class.

The test case is this one:

$resolveRelations = "Menus.item,item.singleMenuEntry,singleMenuEntry.ctaLink,singleMenuEntry.mainSection,singleMenuEntry.secondarySection";
$result = $client
    ->editMode()
    ->language('it')
    ->resolveRelations($resolveRelations)
    ->getStoryBySlug('header-and-footer/headercareers')->getBody();

The current implementation of resolve relations create an array using as key the first part of the relations. In the test case are:

If you are using as key the first part (before the dot), you have “conflict for:

Now for preserving all the relations, the resolveRelations method now creates an array of array like this one:

Array
(
    [Menus] => Array
        (
            [0] => item
        )

    [item] => Array
        (
            [0] => singleMenuEntry
        )

    [singleMenuEntry] => Array
        (
            [0] => ctaLink
            [1] => mainSection
            [2] => secondarySection
        )

)

Now in the insertRelations() method, instead of checking if the field is equal to the element of the array, I modified the "if condition" to check if the field is in the array.

roberto-butti commented 1 year ago

@joaokamun and @ademarCardoso I also added 3 test cases for testing nested relations and more than 50 relations that allow the APIs to reply with rel_uuids. If you agree, we can merge this PR.

You can see the tests:

vendor/bin/pest --filter "Integration: get one story with Resolved relations"
joaokamun commented 1 year ago

Looks great!

roberto-butti commented 1 year ago

Ehi @ademarCardoso , removed comments : https://github.com/storyblok/php-client/pull/83/commits/f7b60fc0c33f081694c309ea09553b1958bb790d