php-dextrin

Coercion
in package

FinalYes

Schema-driven type coercion on encode -- a capability with no Elixir `dextrin` equivalent, ported from `node-dextrin` because the value model's own `DXN*::fromX()`/`::from*()` conversion helpers already make the coercion table trivial to build: every class's documented native-type converter doubles as an encode-time coercion, with no separate registration.

self::tryCoerce() is attempted by Validator only when a value doesn't already match its field's declared type -- never during decode, which always knows its own concrete wire type unambiguously and has nothing to coerce. Governed by Dextrin::encode()/encodeBinary()'s $coerce parameter, default true.

One coercion the JS port has that this port deliberately omits: symbol from a native symbol. PHP's keyword always decodes as DXNKeyword, with nothing analogous to JS's Symbol.for, so there is no native PHP value that source coercion could ever apply to.

Table of Contents

Methods

tryCoerce()  : mixed
Attempts to coerce `$value` toward `$type`. Returns the coerced value on success, or {@see NotCoercible::Instance} if no applicable coercion exists (the caller falls back to treating this as an ordinary type mismatch). Never throws -- a coercion attempt that fails (e.g. an invalid UUID string) is simply not a coercion, not a distinct error.
__construct()  : mixed
coerceListOf()  : mixed
coerceOneOf()  : mixed
coerceOrOriginal()  : mixed
Returns the coerced value, or `$value` itself unchanged if no coercion applies -- used for element-wise coercion, where a non-coercible element should still pass through for the outer type-check to judge on its own merits.
coercePrimitive()  : mixed
coerceSetOf()  : mixed
coerceTupleOf()  : mixed
tryOr()  : mixed

Methods

tryCoerce()

Attempts to coerce `$value` toward `$type`. Returns the coerced value on success, or {@see NotCoercible::Instance} if no applicable coercion exists (the caller falls back to treating this as an ordinary type mismatch). Never throws -- a coercion attempt that fails (e.g. an invalid UUID string) is simply not a coercion, not a distinct error.

public static tryCoerce(TypeExpr $type, mixed $value) : mixed
Parameters
$type : TypeExpr
$value : mixed

__construct()

private __construct() : mixed

coerceListOf()

private static coerceListOf(TypeExpr|null $elemType, mixed $value) : mixed
Parameters
$elemType : TypeExpr|null
$value : mixed

coerceOneOf()

private static coerceOneOf(array<int, TypeExpr$variants, mixed $value) : mixed
Parameters
$variants : array<int, TypeExpr>
$value : mixed

coerceOrOriginal()

Returns the coerced value, or `$value` itself unchanged if no coercion applies -- used for element-wise coercion, where a non-coercible element should still pass through for the outer type-check to judge on its own merits.

private static coerceOrOriginal(TypeExpr $type, mixed $value) : mixed
Parameters
$type : TypeExpr
$value : mixed

coercePrimitive()

private static coercePrimitive(string $name, mixed $value) : mixed
Parameters
$name : string
$value : mixed

coerceSetOf()

private static coerceSetOf(TypeExpr|null $elemType, mixed $value) : mixed
Parameters
$elemType : TypeExpr|null
$value : mixed

coerceTupleOf()

private static coerceTupleOf(array<int, TypeExpr$elemTypes, mixed $value) : mixed
Parameters
$elemTypes : array<int, TypeExpr>
$value : mixed

tryOr()

private static tryOr(callable(): mixed $fn) : mixed
Parameters
$fn : callable(): mixed
On this page

Search results