src/Model/Relationship.php
public static function getIncludedData(
        $primaryData,
        $include = [],
        Fields $fields = null,
        $additionalResourceParameters = []
    ) {
        /**
         * Store relationshipKeys as key and ids of their related data as value
         * @example
         * ```php
         * (object) [
         *     'author'  => [1],
         *     'comment' => [1, 2, 3, 4]
         * ]
         * ```
         */
        $tempRelationshipIds = new \stdClass();

        //check if relationship exists
        foreach ($include as $relationshipKey) {
            if (!static::relationshipExists($relationshipKey)) {
                throw new RequestException(sprintf(
                    'Relationship "%s" not found',
                    $relationshipKey
                ));
            }

            //Will hold ids of related data
            $tempRelationshipIds->{$relationshipKey} = [];
        }

        if (empty($include) || empty($primaryData)) {
            return [];
        }

        //iterate all primary data

        //if a single resource convert it to array
        //so it can be iterated in the same way
        if (!is_array($primaryData)) {
            $primaryData = [$primaryData];
        }

        foreach ($primaryData as $resource) {
            //Ignore resource if it's relationships are not set or empty
            if (empty($resource->relationships)) {
                continue;
            }

            foreach ($include as $relationshipKey) {
                //ignore if requested relationship is not set
                if (!isset($resource->relationships->{$relationshipKey})) {
                    continue;
                }

                //ignore if requested relationship data are not set
                if (!isset($resource->relationships->{$relationshipKey}->data)) {
                    continue;
                }

                $relationshipData = $resource->relationships->{$relationshipKey}->data;

                if (!$relationshipData || empty($relationshipData)) {
                    continue;
                }

                //if single relationship resource convert it to array
                //so it can be iterated in the same way
                if (!is_array($relationshipData)) {
                    $relationshipData = [$relationshipData];
                }

                //Push relationship id for this requested relationship
                foreach ($relationshipData as $primaryKeyAndType) {
                    //push primary key (use type? $primaryKeyAndType->type)
                    $tempRelationshipIds->{$relationshipKey}[] = $primaryKeyAndType->id;
                }
            }
        }

        $included = [];

        foreach ($include as $relationshipKey) {
            $relationship = static::getRelationship($relationshipKey);

            $relationshipModelClass = $relationship->modelClass;

            $ids = array_unique($tempRelationshipIds->{$relationshipKey});

            $additionalArgument = (
                isset($additionalResourceParameters[$relationshipKey])
                ? $additionalResourceParameters[$relationshipKey]
                : []
            );

            $resources = $relationshipModelClass::getById(
                $ids,
                $fields,
                ...$additionalArgument
            );

            foreach ($resources as $key => $resource) {
                if ($resource === null) {
                    continue;
                }

                $included[] = $resource;
            }
        }

        return $included;
    }
public static function getIncludedData(
        $primaryData,
        $include = [],
        Fields $fields = null,
        $additionalResourceParameters = []
    ) {
        /**
         * Store relationshipKeys as key and ids of their related data as value
         * @example
         * ```php
         * (object) [
         *     'author'  => [1],
         *     'comment' => [1, 2, 3, 4]
         * ]
         * ```
         */
        $tempRelationshipIds = new \stdClass();

        //check if relationship exists
        foreach ($include as $relationshipKey) {
            if (!static::relationshipExists($relationshipKey)) {
                throw new RequestException(sprintf(
                    'Relationship "%s" not found',
                    $relationshipKey
                ));
            }

            //Will hold ids of related data
            $tempRelationshipIds->{$relationshipKey} = [];
        }

        if (empty($include) || empty($primaryData)) {
            return [];
        }

        //iterate all primary data

        //if a single resource convert it to array
        //so it can be iterated in the same way
        if (!is_array($primaryData)) {
            $primaryData = [$primaryData];
        }

        foreach ($primaryData as $resource) {
            //Ignore resource if it's relationships are not set or empty
            if (empty($resource->relationships)) {
                continue;
            }

            foreach ($include as $relationshipKey) {
                //ignore if requested relationship is not set
                if (!isset($resource->relationships->{$relationshipKey})) {
                    continue;
                }

                //ignore if requested relationship data are not set
                if (!isset($resource->relationships->{$relationshipKey}->data)) {
                    continue;
                }

                $relationshipData = $resource->relationships->{$relationshipKey}->data;

                if (!$relationshipData || empty($relationshipData)) {
                    continue;
                }

                //if single relationship resource convert it to array
                //so it can be iterated in the same way
                if (!is_array($relationshipData)) {
                    $relationshipData = [$relationshipData];
                }

                //Push relationship id for this requested relationship
                foreach ($relationshipData as $primaryKeyAndType) {
                    //push primary key (use type? $primaryKeyAndType->type)
                    $tempRelationshipIds->{$relationshipKey}[] = $primaryKeyAndType->id;
                }
            }
        }

        $included = [];

        foreach ($include as $relationshipKey) {
            $relationship = static::getRelationship($relationshipKey);

            $relationshipModelClass = $relationship->modelClass;

            $ids = array_unique($tempRelationshipIds->{$relationshipKey});

            $additionalArgument = (
                isset($additionalResourceParameters[$relationshipKey])
                ? $additionalResourceParameters[$relationshipKey]
                : []
            );

            $resources = $relationshipModelClass::getById(
                $ids,
                $fields,
                ...$additionalArgument
            );

            foreach ($resources as $key => $resource) {
                if ($resource === null) {
                    continue;
                }

                $included[] = $resource;
            }
        }

        return $included;
    }
public static function getIncludedData ( $ primaryData , $ include = [ ] , Fields $ fields = null , $ additionalResourceParameters = [ ] ) { /**
         * Store relationshipKeys as key and ids of their related data as value
         * @example
         * ```php
         * (object) [
         *     'author'  => [1],
         *     'comment' => [1, 2, 3, 4]
         * ]
         * ```
         */ $ tempRelationshipIds = new \ stdClass ( ) ; //check if relationship exists foreach ( $ include as $ relationshipKey ) { if ( ! static :: relationshipExists ( $ relationshipKey ) ) { throw new RequestException ( sprintf ( 'Relationship "%s" not found' , $ relationshipKey ) ) ; } //Will hold ids of related data $ tempRelationshipIds -> { $ relationshipKey } = [ ] ; } if ( empty ( $ include ) || empty ( $ primaryData ) ) { return [ ] ; } //iterate all primary data //if a single resource convert it to array //so it can be iterated in the same way if ( ! is_array ( $ primaryData ) ) { $ primaryData = [ $ primaryData ] ; } foreach ( $ primaryData as $ resource ) { //Ignore resource if it's relationships are not set or empty if ( empty ( $ resource -> relationships ) ) { continue ; } foreach ( $ include as $ relationshipKey ) { //ignore if requested relationship is not set if ( ! isset ( $ resource -> relationships -> { $ relationshipKey } ) ) { continue ; } //ignore if requested relationship data are not set if ( ! isset ( $ resource -> relationships -> { $ relationshipKey } -> data ) ) { continue ; } $ relationshipData = $ resource -> relationships -> { $ relationshipKey } -> data ; if ( ! $ relationshipData || empty ( $ relationshipData ) ) { continue ; } //if single relationship resource convert it to array //so it can be iterated in the same way if ( ! is_array ( $ relationshipData ) ) { $ relationshipData = [ $ relationshipData ] ; } //Push relationship id for this requested relationship foreach ( $ relationshipData as $ primaryKeyAndType ) { //push primary key (use type? $primaryKeyAndType->type) $ tempRelationshipIds -> { $ relationshipKey } [ ] = $ primaryKeyAndType -> id ; } } } $ included = [ ] ; foreach ( $ include as $ relationshipKey ) { $ relationship = static :: getRelationship ( $ relationshipKey ) ; $ relationshipModelClass = $ relationship -> modelClass ; $ ids = array_unique ( $ tempRelationshipIds -> { $ relationshipKey } ) ; $ additionalArgument = ( isset ( $ additionalResourceParameters [ $ relationshipKey ] ) ? $ additionalResourceParameters [ $ relationshipKey ] : [ ] ) ; $ resources = $ relationshipModelClass :: getById ( $ ids , $ fields , ... $ additionalArgument ) ; foreach ( $ resources as $ key => $ resource ) { if ( $ resource === null ) { continue ; } $ included [ ] = $ resource ; } } return $ included ; }
Get jsonapi's included object, selected by include argument,
using id's of relationship's data from resources in primary data object
@param Resource|Resource[]  $primaryData Primary data resource or resources
@param string[]             $include     An array with the keys of relationships to include
@param Fields|null $fields
@param array $additionalResourceParameters *[Optional]*
@return Resource[]           An array with all included related data
@throws \Phramework\Exceptions\RequestException When a relationship is not found
@throws \Phramework\Exceptions\ServerException
@todo handle Relationship resource cannot be accessed
@todo include second level relationships
@example
```php
Relationship::getIncludedData(
Article::get(),
['tag', 'author']
);
```
Get jsonapi s included object selected by include argument using id s of relationship s data from resources in primary data object