Skip to content

Rule

A declarative description of one canonical-array shape, structural only (allowed keys, enums-of-kind, nesting) — not required-ness that depends on runtime state or document processing order, which stays a Layer-2 semantic check in the handler’s own executeArray()/toCanonicalArray().

$closed on a Struct means an unrecognized key is a diagnostic rather than silently ignored, matching the XSDs’ closed-content-model default.

final readonly class Rule

Since1.0.0
SourceConfig/Schema/Rule.php
PropertyTypeDescription
$closedboolreadonly.
$enumValuesarrayreadonly.
$items?Rulereadonly.
$keysarrayreadonly.
$nullableboolreadonly.
$requiredarrayreadonly.
$typeSchemaTypereadonly.
$variantsarrayreadonly.
MethodDescription
bool(bool $nullable = false): RuleBuilds a rule for a real PHP bool, or null when $nullable is set.
dictOf(Rule $value, bool $nullable = false): RuleBuilds a rule for a map with dynamic string keys whose every value must match $value.
enumOf(list<string> $values, bool $nullable = false): Rule
int(bool $nullable = false): RuleBuilds a rule for a real PHP int, or null when $nullable is set.
listOf(Rule $item, bool $nullable = false): RuleBuilds a rule for a sequential list whose every element must match $item.
mixed(): RuleBuilds a rule that accepts any value, including null.
oneOf(Rule ...$variants): RuleBuilds a rule for a value that may take any one of $variants’ shapes.
phpClass(bool $nullable = false): RuleBuilds a rule for a non-empty string that is shaped like a PHP class name.
string(bool $nullable = false): RuleBuilds a rule for a PHP string value, or null when $nullable is set.
struct(array<string, Rule> $keys, list<string> $required = [], bool $closed = true, bool $nullable = false): Rule

public static function bool(bool $nullable = false): Rule

Builds a rule for a real PHP bool, or null when $nullable is set.

Strings such as “true” and “on” do not pass; the canonical array is expected to have had such literals coerced by the config handler before it reaches schema validation.

ParameterTypeDescription
$nullablebool

Returns Rule

public static function dictOf(Rule $value, bool $nullable = false): Rule

Builds a rule for a map with dynamic string keys whose every value must match $value.

Use this where the key set is data rather than schema — a connection-name-keyed map of database entries, say — and Rule::struct() where the keys are known up front. Non-string keys are reported; the keys themselves are otherwise unconstrained. Pass $nullable to also accept null in this position.

ParameterTypeDescription
$valueRule
$nullablebool

Returns Rule

public static function enumOf(list<string> $values, bool $nullable = false): Rule

ParameterTypeDescription
$valueslist``<``string``>
$nullablebool

Returns Rule

public static function int(bool $nullable = false): Rule

Builds a rule for a real PHP int, or null when $nullable is set.

Numeric strings and floats do not pass, so a value read straight from XML must have been cast by the config handler first.

ParameterTypeDescription
$nullablebool

Returns Rule

public static function listOf(Rule $item, bool $nullable = false): Rule

Builds a rule for a sequential list whose every element must match $item.

The value has to be a real list — an array with contiguous integer keys from zero — so a string-keyed map in this position is reported rather than accepted. Pass $nullable to also accept null.

ParameterTypeDescription
$itemRule
$nullablebool

Returns Rule

public static function mixed(): Rule

Builds a rule that accepts any value, including null.

Nothing below this point is inspected, so it is how an open-ended region of the canonical array — a free-form parameter bag, for instance — is marked as deliberately unconstrained rather than left out of the schema. There is no $nullable argument because such a rule is always nullable.

Returns Rule

public static function oneOf(Rule ...$variants): Rule

Builds a rule for a value that may take any one of $variants’ shapes.

For a position that is genuinely alternative-shaped — a bool that a %env(...)% placeholder string stands in for until the compiled artifact is loaded, say — rather than one whose shape is unknown, which is what Rule::mixed() is for. A value matching no variant is reported once, against this position: the variants’ own diagnostics would each describe a shape the value was never meant to have.

ParameterTypeDescription
$variantsRule

Returns Rule

public static function phpClass(bool $nullable = false): Rule

Builds a rule for a non-empty string that is shaped like a PHP class name.

Only the syntax is checked — optional leading backslash, backslash-separated identifier segments — because schema validation is pure and does not autoload. Whether the class exists is left to whoever instantiates it. Pass $nullable to also accept null.

ParameterTypeDescription
$nullablebool

Returns Rule

public static function string(bool $nullable = false): Rule

Builds a rule for a PHP string value, or null when $nullable is set.

The check is on the PHP type only: numeric strings and the empty string both pass. Use Rule::enumOf() to restrict the value set and Rule::phpClass() for class-name strings.

ParameterTypeDescription
$nullablebool

Returns Rule

public static function struct(array<string, Rule> $keys, list<string> $required = [], bool $closed = true, bool $nullable = false): Rule

ParameterTypeDescription
$keysarray``<``string``, Rule>
$requiredlist``<``string``>
$closedbool
$nullablebool

Returns Rule