class Deserializer

Constants

protected TYPE_SPECIFIC_KEYWORDS

The type-specific keywords supported by concrete schema types.

protected UNSUPPORTED_ASSERTION_KEYWORDS

The JSON Schema 2020-12 assertions this builder cannot represent.

protected MAX_NODES

The maximum number of schema fragments that may be expanded.

protected MAX_REFERENCE_DEPTH

The maximum number of distinct references on one active path.

Properties

protected int $nodes

The number of schema fragments expanded so far.

protected array<string, array<string, mixed>> $refCache

The cache of resolved local "$ref" targets, keyed by reference.

Methods

__construct(array $root)

Create a new deserializer instance.

static Type
deserialize(array $schema)

Deserialize the supported JSON Schema subset into a type.

build(array $schema, array $refs = [])

Build a type from the given schema fragment.

array|null
prepareComposition(array $schema, array $refs = [])

Prepare the branches of an anyOf or oneOf composition.

AnyOfType|null
buildAnyOfComposition(array $schema, array $refs = [], array|null $composition = null)

Build an anyOf composition unless it is the existing nullable single-schema form.

buildObject(array $schema, array $refs = [])

Build an object type from the given schema fragment.

buildArray(array $schema, array $refs = [])

Build an array type from the given schema fragment.

buildString(array $schema)

Build a string type from the given schema fragment.

buildInteger(array $schema)

Build an integer type from the given schema fragment.

buildNumber(array $schema)

Build a number type from the given schema fragment.

applyNumericBounds(IntegerType|NumberType $type, array $schema, callable|null $cast = null)

Apply the numeric bound keywords to the given integer or number type.

applyIntegerBounds(ArrayType|StringType $type, array $schema, string $minimumKeyword, string $maximumKeyword)

Apply integer-valued minimum and maximum keywords to an array or string type.

void
applyCommon(Type $type, array $schema)

Apply the keywords shared by every type to the given instance.

array
resolveType(array $schema)

Resolve the base type name and whether the schema is nullable.

string|null
inferType(array $schema)

Infer the type name when "type" is absent but the shape is unambiguous.

string|null
inferEnumType(array $enum)

Infer the scalar type shared by a homogeneous enum of scalars.

void
ensureUnionConstraintsAreSupported(array $schema)

Ensure a multi-type union carries no type-specific constraint keywords.

void
ensureAssertionsAreSupported(array $schema)

Ensure the schema carries no standard assertions this builder cannot represent.

string
describeBranch(string $keyword)

Describe a composition branch.

array
ensureSchemaFragmentIsArray(mixed $fragment, string $context)

Ensure the given schema fragment is an array.

array
mergeSchemaFragments(array $base, array $overlay, string $context)

Merge two schema fragments without weakening represented assertions.

void
ensureAnyOfConstraintsAreSupported(array $schema)

Ensure a general anyOf composition carries no competing structural constraints.

array
normalizeUnions(array $schema, array $refs = [], array|null $composition = null)

Collapse "anyOf" / "oneOf" null branches into a single effective schema.

bool
isNullBranch(array $branch)

Determine if the given schema branch describes only the "null" type.

bool
mayAcceptNull(array $branch)

Determine if the schema branch may accept null.

bool
hasBranchOnlyEnumExcludingNull(array $schema, array $branch)

Determine if the branch alone constrains an enum that excludes null.

array
resolveRef(array $schema, array $refs = [])

Resolve a local "$ref" against the root schema, merging sibling keys.

array
lookupRef(string $ref)

Look up a local JSON pointer reference within the root schema.

int|float|null
toNumber(mixed $value)

Normalize the given value to an integer or float, or null when not numeric.

int
toInteger(int|float $value)

Normalize the given number to a PHP integer.

void
countNode()

Count an expanded schema fragment.

Details

at line 68
protected __construct(array $root)

Create a new deserializer instance.

Parameters

array $root

at line 79
static Type deserialize(array $schema)

Deserialize the supported JSON Schema subset into a type.

Parameters

array $schema

Return Value

Type

Exceptions

InvalidArgumentException

at line 92
protected Type build(array $schema, array $refs = [])

Build a type from the given schema fragment.

Parameters

array $schema
array $refs

Return Value

Type

Exceptions

InvalidArgumentException

at line 158
protected array|null prepareComposition(array $schema, array $refs = [])

Prepare the branches of an anyOf or oneOf composition.

Parameters

array $schema
array $refs

Return Value

array|null

Exceptions

InvalidArgumentException

at line 212
protected AnyOfType|null buildAnyOfComposition(array $schema, array $refs = [], array|null $composition = null)

Build an anyOf composition unless it is the existing nullable single-schema form.

Parameters

array $schema
array $refs
array|null $composition

Return Value

AnyOfType|null

Exceptions

InvalidArgumentException

at line 250
protected ObjectType buildObject(array $schema, array $refs = [])

Build an object type from the given schema fragment.

Parameters

array $schema
array $refs

Return Value

ObjectType

Exceptions

InvalidArgumentException

at line 330
protected ArrayType buildArray(array $schema, array $refs = [])

Build an array type from the given schema fragment.

Parameters

array $schema
array $refs

Return Value

ArrayType

Exceptions

InvalidArgumentException

at line 364
protected StringType buildString(array $schema)

Build a string type from the given schema fragment.

Parameters

array $schema

Return Value

StringType

at line 394
protected IntegerType buildInteger(array $schema)

Build an integer type from the given schema fragment.

Parameters

array $schema

Return Value

IntegerType

at line 404
protected NumberType buildNumber(array $schema)

Build a number type from the given schema fragment.

Parameters

array $schema

Return Value

NumberType

at line 421
protected IntegerType|NumberType applyNumericBounds(IntegerType|NumberType $type, array $schema, callable|null $cast = null)

Apply the numeric bound keywords to the given integer or number type.

Parameters

IntegerType|NumberType $type
array $schema
callable|null $cast

Return Value

IntegerType|NumberType

Exceptions

InvalidArgumentException

at line 451
protected ArrayType|StringType applyIntegerBounds(ArrayType|StringType $type, array $schema, string $minimumKeyword, string $maximumKeyword)

Apply integer-valued minimum and maximum keywords to an array or string type.

Parameters

ArrayType|StringType $type
array $schema
string $minimumKeyword
string $maximumKeyword

Return Value

ArrayType|StringType

Exceptions

InvalidArgumentException

at line 475
protected void applyCommon(Type $type, array $schema)

Apply the keywords shared by every type to the given instance.

Parameters

Type $type
array $schema

Return Value

void

Exceptions

InvalidArgumentException

at line 520
protected array resolveType(array $schema)

Resolve the base type name and whether the schema is nullable.

Parameters

array $schema

Return Value

array

Exceptions

InvalidArgumentException

at line 577
protected string|null inferType(array $schema)

Infer the type name when "type" is absent but the shape is unambiguous.

Parameters

array $schema

Return Value

string|null

at line 594
protected string|null inferEnumType(array $enum)

Infer the scalar type shared by a homogeneous enum of scalars.

Parameters

array $enum

Return Value

string|null

at line 637
protected void ensureUnionConstraintsAreSupported(array $schema)

Ensure a multi-type union carries no type-specific constraint keywords.

Parameters

array $schema

Return Value

void

Exceptions

InvalidArgumentException

at line 655
protected void ensureAssertionsAreSupported(array $schema)

Ensure the schema carries no standard assertions this builder cannot represent.

Parameters

array $schema

Return Value

void

Exceptions

InvalidArgumentException

at line 669
protected string describeBranch(string $keyword)

Describe a composition branch.

Parameters

string $keyword

Return Value

string

at line 681
protected array ensureSchemaFragmentIsArray(mixed $fragment, string $context)

Ensure the given schema fragment is an array.

Parameters

mixed $fragment
string $context

Return Value

array

Exceptions

InvalidArgumentException

at line 708
protected array mergeSchemaFragments(array $base, array $overlay, string $context)

Merge two schema fragments without weakening represented assertions.

Parameters

array $base
array $overlay
string $context

Return Value

array

Exceptions

InvalidArgumentException

at line 742
protected void ensureAnyOfConstraintsAreSupported(array $schema)

Ensure a general anyOf composition carries no competing structural constraints.

Parameters

array $schema

Return Value

void

Exceptions

InvalidArgumentException

at line 768
protected array normalizeUnions(array $schema, array $refs = [], array|null $composition = null)

Collapse "anyOf" / "oneOf" null branches into a single effective schema.

Parameters

array $schema
array $refs
array|null $composition

Return Value

array

Exceptions

InvalidArgumentException

at line 838
protected bool isNullBranch(array $branch)

Determine if the given schema branch describes only the "null" type.

Parameters

array $branch

Return Value

bool

at line 854
protected bool mayAcceptNull(array $branch)

Determine if the schema branch may accept null.

Parameters

array $branch

Return Value

bool

at line 873
protected bool hasBranchOnlyEnumExcludingNull(array $schema, array $branch)

Determine if the branch alone constrains an enum that excludes null.

Parameters

array $schema
array $branch

Return Value

bool

at line 890
protected array resolveRef(array $schema, array $refs = [])

Resolve a local "$ref" against the root schema, merging sibling keys.

Parameters

array $schema
array $refs

Return Value

array

Exceptions

InvalidArgumentException

at line 931
protected array lookupRef(string $ref)

Look up a local JSON pointer reference within the root schema.

Parameters

string $ref

Return Value

array

Exceptions

InvalidArgumentException

at line 967
protected int|float|null toNumber(mixed $value)

Normalize the given value to an integer or float, or null when not numeric.

Parameters

mixed $value

Return Value

int|float|null

at line 985
protected int toInteger(int|float $value)

Normalize the given number to a PHP integer.

Parameters

int|float $value

Return Value

int

Exceptions

InvalidArgumentException

at line 1003
protected void countNode()

Count an expanded schema fragment.

Return Value

void

Exceptions

InvalidArgumentException