trait JsonSchemaReflectionTrait
Hierarchy
- trait \Drupal\serialization\Normalizer\JsonSchemaReflectionTrait
1 file declares its use of JsonSchemaReflectionTrait
- FieldItemNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ FieldItemNormalizer.php
File
-
core/
modules/ serialization/ src/ Normalizer/ JsonSchemaReflectionTrait.php, line 9
Namespace
Drupal\serialization\NormalizerView source
trait JsonSchemaReflectionTrait {
/**
* Get a JSON Schema based on method reflection.
*
* @param object $object
* Object to reflect.
* @param string $method
* Method to reflect.
* @param array $fallback
* Fallback. Defaults to an empty array, which is a matches-all schema.
* @param bool $nullable
* If a schema is returned from reflection, whether to add a null option.
*
* @return array
* JSON Schema.
*/
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,
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
JsonSchemaReflectionTrait::getJsonSchemaForMethod | protected | function | Get a JSON Schema based on method reflection. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.