function JsonSchemaReflectionTrait::getJsonSchemaForMethod

Get a JSON Schema based on method reflection.

Parameters

object $object: Object to reflect.

string $method: Method to reflect.

array $fallback: Fallback. Defaults to an empty array, which is a matches-all schema.

bool $nullable: If a schema is returned from reflection, whether to add a null option.

Return value

array JSON Schema.

6 calls to JsonSchemaReflectionTrait::getJsonSchemaForMethod()
FieldItemNormalizer::getNormalizationSchema in core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php
Retrieve JSON Schema for the normalization.
MarkupNormalizer::getNormalizationSchema in core/modules/serialization/src/Normalizer/MarkupNormalizer.php
Retrieve JSON Schema for the normalization.
PrimitiveDataNormalizer::getNormalizationSchema in core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php
Retrieve JSON Schema for the normalization.
SchematicNormalizerHelperTrait::getNormalizationSchema in core/modules/serialization/src/Normalizer/SchematicNormalizerHelperTrait.php
Retrieve JSON Schema for the normalization.
SchematicNormalizerTrait::getNormalizationSchema in core/modules/serialization/src/Normalizer/SchematicNormalizerTrait.php
Retrieve JSON Schema for the normalization.

... See full list

File

core/modules/serialization/src/Normalizer/JsonSchemaReflectionTrait.php, line 29

Class

JsonSchemaReflectionTrait
Interface for using reflection with the JSON object.

Namespace

Drupal\serialization\Normalizer

Code

protected function getJsonSchemaForMethod(mixed $object, string $method, array $fallback = [], bool $nullable = FALSE) : array {
  $schemas = [];
  if ((is_object($object) || class_exists($object)) && method_exists($object, $method)) {
    $reflection = new \ReflectionMethod($object, $method);
    $schemas = $reflection->getAttributes(JsonSchema::class);
  }
  if (count($schemas) === 0) {
    return $fallback;
  }
  $schemas = array_values(array_filter([
    array_map(fn($schema) => $schema->newInstance()
      ->getJsonSchema(), $schemas),
    $nullable ? [
      'type' => 'null',
    ] : NULL,
  ]));
  return count($schemas) === 1 ? current($schemas) : [
    'oneOf' => $schemas,
  ];
}

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