src/JsonApi/Serializer/ItemNormalizer.php
private function getPopulatedRelations($object, ?string $format, array $context, array $relationships): array
    {
        $data = [];

        if (!isset($context['resource_class'])) {
            return $data;
        }

        unset($context['api_included']);
        foreach ($relationships as $relationshipDataArray) {
            $relationshipName = $relationshipDataArray['name'];

            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);

            if ($this->nameConverter) {
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
            }

            if (!$attributeValue) {
                continue;
            }

            $data[$relationshipName] = [
                'data' => [],
            ];

            // Many to one relationship
            if ('one' === $relationshipDataArray['cardinality']) {
                unset($attributeValue['data']['attributes']);
                $data[$relationshipName] = $attributeValue;

                continue;
            }

            // Many to many relationship
            foreach ($attributeValue as $attributeValueElement) {
                if (!isset($attributeValueElement['data'])) {
                    throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
                }
                unset($attributeValueElement['data']['attributes']);
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
            }
        }

        return $data;
    }
private function getPopulatedRelations($object, ?string $format, array $context, array $relationships): array
    {
        $data = [];

        if (!isset($context['resource_class'])) {
            return $data;
        }

        unset($context['api_included']);
        foreach ($relationships as $relationshipDataArray) {
            $relationshipName = $relationshipDataArray['name'];

            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);

            if ($this->nameConverter) {
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
            }

            if (!$attributeValue) {
                continue;
            }

            $data[$relationshipName] = [
                'data' => [],
            ];

            // Many to one relationship
            if ('one' === $relationshipDataArray['cardinality']) {
                unset($attributeValue['data']['attributes']);
                $data[$relationshipName] = $attributeValue;

                continue;
            }

            // Many to many relationship
            foreach ($attributeValue as $attributeValueElement) {
                if (!isset($attributeValueElement['data'])) {
                    throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
                }
                unset($attributeValueElement['data']['attributes']);
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
            }
        }

        return $data;
    }
private function getPopulatedRelations ( $ object , ? string $ format , array $ context , array $ relationships ) : array { $ data = [ ] ; if ( ! isset ( $ context [ 'resource_class' ] ) ) { return $ data ; } unset ( $ context [ 'api_included' ] ) ; foreach ( $ relationships as $ relationshipDataArray ) { $ relationshipName = $ relationshipDataArray [ 'name' ] ; $ attributeValue = $ this -> getAttributeValue ( $ object , $ relationshipName , $ format , $ context ) ; if ( $ this -> nameConverter ) { $ relationshipName = $ this -> nameConverter -> normalize ( $ relationshipName , $ context [ 'resource_class' ] , self :: FORMAT , $ context ) ; } if ( ! $ attributeValue ) { continue ; } $ data [ $ relationshipName ] = [ 'data' => [ ] , ] ; // Many to one relationship if ( 'one' === $ relationshipDataArray [ 'cardinality' ] ) { unset ( $ attributeValue [ 'data' ] [ 'attributes' ] ) ; $ data [ $ relationshipName ] = $ attributeValue ; continue ; } // Many to many relationship foreach ( $ attributeValue as $ attributeValueElement ) { if ( ! isset ( $ attributeValueElement [ 'data' ] ) ) { throw new UnexpectedValueException ( sprintf ( 'The JSON API attribute \'%s\' must contain a "data" key.' , $ relationshipName ) ) ; } unset ( $ attributeValueElement [ 'data' ] [ 'attributes' ] ) ; $ data [ $ relationshipName ] [ 'data' ] [ ] = $ attributeValueElement [ 'data' ] ; } } return $ data ; }
Populates relationships keys.

@param object $object

@throws UnexpectedValueException
Populates relationships keys .