src/Model/Relationship.php
public static function getRelationshipData(
        $relationshipKey,
        $id,
        Fields $fields = null,
        $primaryDataParameters = [],
        $relationshipParameters = []
    ) {
        if (!static::relationshipExists($relationshipKey)) {
            throw new \Phramework\Exceptions\ServerException(sprintf(
                '"%s" is not a valid relationship key',
                $relationshipKey
            ));
        }

        $relationship = static::getRelationship($relationshipKey);

        switch ($relationship->type) {
            case \Phramework\JSONAPI\Relationship::TYPE_TO_ONE:
                $resource = $callMethod = static::getById(
                    $id,
                    $fields,
                    ...$primaryDataParameters
                );

                if (!$resource) {
                    return null;
                }

                //And use it's relationships data for this relationship
                return (
                    isset($resource->relationships->{$relationshipKey}->data)
                    ? $resource->relationships->{$relationshipKey}->data
                    : null
                );
            case \Phramework\JSONAPI\Relationship::TYPE_TO_MANY:
            default:
                if (!isset($relationship->callbacks->{Phramework::METHOD_GET})) {
                    return [];
                }

                $callMethod = $relationship->callbacks->{Phramework::METHOD_GET};

                if (!is_callable($callMethod)) {
                    throw new \Phramework\Exceptions\ServerException(
                        $callMethod[0] . '::' . $callMethod[1]
                        . ' is not implemented'
                    );
                }

                //also we could attempt to use getById like the above TO_ONE
                //to use relationships data

                return call_user_func(
                    $callMethod,
                    $id,
                    $fields,
                    ...$relationshipParameters
                );
        }
    }
public static function getRelationshipData(
        $relationshipKey,
        $id,
        Fields $fields = null,
        $primaryDataParameters = [],
        $relationshipParameters = []
    ) {
        if (!static::relationshipExists($relationshipKey)) {
            throw new \Phramework\Exceptions\ServerException(sprintf(
                '"%s" is not a valid relationship key',
                $relationshipKey
            ));
        }

        $relationship = static::getRelationship($relationshipKey);

        switch ($relationship->type) {
            case \Phramework\JSONAPI\Relationship::TYPE_TO_ONE:
                $resource = $callMethod = static::getById(
                    $id,
                    $fields,
                    ...$primaryDataParameters
                );

                if (!$resource) {
                    return null;
                }

                //And use it's relationships data for this relationship
                return (
                    isset($resource->relationships->{$relationshipKey}->data)
                    ? $resource->relationships->{$relationshipKey}->data
                    : null
                );
            case \Phramework\JSONAPI\Relationship::TYPE_TO_MANY:
            default:
                if (!isset($relationship->callbacks->{Phramework::METHOD_GET})) {
                    return [];
                }

                $callMethod = $relationship->callbacks->{Phramework::METHOD_GET};

                if (!is_callable($callMethod)) {
                    throw new \Phramework\Exceptions\ServerException(
                        $callMethod[0] . '::' . $callMethod[1]
                        . ' is not implemented'
                    );
                }

                //also we could attempt to use getById like the above TO_ONE
                //to use relationships data

                return call_user_func(
                    $callMethod,
                    $id,
                    $fields,
                    ...$relationshipParameters
                );
        }
    }
public static function getRelationshipData ( $ relationshipKey , $ id , Fields $ fields = null , $ primaryDataParameters = [ ] , $ relationshipParameters = [ ] ) { if ( ! static :: relationshipExists ( $ relationshipKey ) ) { throw new \ Phramework \ Exceptions \ ServerException ( sprintf ( '"%s" is not a valid relationship key' , $ relationshipKey ) ) ; } $ relationship = static :: getRelationship ( $ relationshipKey ) ; switch ( $ relationship -> type ) { case \ Phramework \ JSONAPI \ Relationship :: TYPE_TO_ONE : $ resource = $ callMethod = static :: getById ( $ id , $ fields , ... $ primaryDataParameters ) ; if ( ! $ resource ) { return null ; } //And use it's relationships data for this relationship return ( isset ( $ resource -> relationships -> { $ relationshipKey } -> data ) ? $ resource -> relationships -> { $ relationshipKey } -> data : null ) ; case \ Phramework \ JSONAPI \ Relationship :: TYPE_TO_MANY : default : if ( ! isset ( $ relationship -> callbacks -> { Phramework :: METHOD_GET } ) ) { return [ ] ; } $ callMethod = $ relationship -> callbacks -> { Phramework :: METHOD_GET } ; if ( ! is_callable ( $ callMethod ) ) { throw new \ Phramework \ Exceptions \ ServerException ( $ callMethod [ 0 ] . '::' . $ callMethod [ 1 ] . ' is not implemented' ) ; } //also we could attempt to use getById like the above TO_ONE //to use relationships data return call_user_func ( $ callMethod , $ id , $ fields , ... $ relationshipParameters ) ; } }
Get records from a relationship link
@param string $relationshipKey
@param string $id
@param Fields|null $fields
@return RelationshipResource|RelationshipResource[]
@throws \Phramework\Exceptions\ServerException If relationship doesn't exist
@throws \Phramework\Exceptions\ServerException If relationship's class method is
not defined
Get records from a relationship link