function ResourceObjectNormalizer::getNormalizationSchema

Overrides SchematicNormalizerTrait::getNormalizationSchema

File

core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php, line 265

Class

ResourceObjectNormalizer
Converts the JSON:API module ResourceObject into a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

public function getNormalizationSchema(mixed $object, array $context = []) : array {
    if (is_string($object)) {
        // Without a true object we can only provide a generic schema.
        return [
            '$ref' => JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#/definitions/resource',
        ];
    }
    // ResourceObject is usually instantiated from a specific entity, however
    // a placeholder can be created for purposes of schema generation which
    // would provide access to the resource type, but not contain any "live"
    // data.
    assert($object instanceof ResourceObject);
    $attributes_schema = [];
    $relationships_schema = [];
    $this->entityTypeManager
        ->getDefinition($object->getResourceType()
        ->getEntityTypeId())
        ->entityClassImplements(FieldableEntityInterface::class) ? $this->processContentEntitySchema($object, $context, $attributes_schema, $relationships_schema) : $this->processConfigEntitySchema($object->getResourceType(), $context, $attributes_schema);
    return [
        'allOf' => [
            [
                '$ref' => JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#/definitions/resourceIdentification',
            ],
        ],
        'properties' => [
            'meta' => [
                '$ref' => JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#/definitions/meta',
            ],
            'links' => [
                '$ref' => JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#/definitions/resourceLinks',
            ],
            // If the array is empty we must return an object so it won't encode as
            // an array; we don't cast the value here so the returned value is still
            // traversable as an array when accessed in PHP.
'attributes' => $attributes_schema ?: new \ArrayObject(),
            'relationships' => $relationships_schema ?: new \ArrayObject(),
        ],
        'unevaluatedProperties' => FALSE,
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.