SchemaResolver
Resolves a schema class name from a target object or array.
The resolver inspects a given target (object or array), extracts a type value using the provided key, and maps it to a schema class name.
If the target is not an object or array, or if the resolved value is not a
non-empty string, null is returned.
Typical usage includes dynamic schema resolution in data mappers, document models, or hydration pipelines.
Table of Contents
Properties
Methods
- __construct() : mixed
- Creates a new SchemaResolver instance.
- __invoke() : string|null
- Resolves the schema class name for the given target.
- resolveFromList() : string|null
- Resolves the schema class from a list of candidate types, honouring the declaration order of {@see $map} as the resolution priority — so a document carrying several types resolves deterministically, regardless of the order the types appear in the document. Falls back to {@see $default} when none of the candidates is mapped (including an empty list).
Properties
$default
private
string|null
$default
= null
$key
private
string
$key
$map
private
array<string|int, mixed>
$map
Methods
__construct()
Creates a new SchemaResolver instance.
public
__construct(string $key, array<string|int, mixed> $map[, string|null $default = null ]) : mixed
Parameters
- $key : string
-
Key used to extract the schema type from the target (e.g. Prop::ADDITIONAL_TYPE).
- $map : array<string|int, mixed>
-
Associative map of schema types to class names. Keys are schema type identifiers, values must be fully-qualified class names.
- $default : string|null = null
-
Default schema class name to use when no mapping matches. Must be a non-empty string or null.
__invoke()
Resolves the schema class name for the given target.
public
__invoke(mixed $target) : string|null
The discriminator value (read at $key) may be a single string or an array of types. When it is an array (a multi-typed document), the class is resolved by $map declaration order — see resolveFromList().
Parameters
- $target : mixed
-
Object or array containing a schema type value.
Return values
string|null —Fully-qualified schema class name, or null if the target is not resolvable or no valid mapping exists.
resolveFromList()
Resolves the schema class from a list of candidate types, honouring the declaration order of {@see $map} as the resolution priority — so a document carrying several types resolves deterministically, regardless of the order the types appear in the document. Falls back to {@see $default} when none of the candidates is mapped (including an empty list).
private
resolveFromList(array<int, mixed> $types) : string|null
Parameters
- $types : array<int, mixed>
-
The candidate type values.
Return values
string|null —The mapped class, the default, or null.