Flow PHP

OptionalType implements Type

Read onlyYes
FinalYes
Tags
template
implements

Interfaces

Type

Methods

__construct()  : mixed
Nullability is idempotent, so wrapping an already-optional type collapses to one level instead of throwing - callers forcing nullability never need a guard.
assert()  : T
Checks that the value is of the type of this object, throwing when it is not.
base()  : Type<string|int, T>
cast()  : T
Takes a value and when necessary casts it to the type of this object.
fromArray()  : OptionalType<string|int, mixed>
isValid()  : bool
Checks if the value is of the type of this object, returning a boolean instead of throwing.
normalize()  : array{type: "optional", base: array}
toString()  : string
Returns a string representation of the type.

Methods

__construct()

Nullability is idempotent, so wrapping an already-optional type collapses to one level instead of throwing - callers forcing nullability never need a guard.

public __construct(Type<string|int, T$base) : mixed
Parameters
$base : Type<string|int, T>
Tags
throws
InvalidTypeException

assert()

Checks that the value is of the type of this object, throwing when it is not.

public assert(mixed $value) : T
Parameters
$value : mixed
Return values
T

cast()

Takes a value and when necessary casts it to the type of this object.

public cast(mixed $value) : T
Parameters
$value : mixed
Return values
T

fromArray()

public static fromArray(array<string, mixed> $data) : OptionalType<string|int, mixed>
Parameters
$data : array<string, mixed>
Return values
OptionalType<string|int, mixed>

isValid()

Checks if the value is of the type of this object, returning a boolean instead of throwing.

public isValid(mixed $value) : bool

When this method returns true, static analysis tools narrow the value's type to T at the call site (via @assert-if-true). Use this when you want to branch on the result; use assert() when you want the call to fail loudly on a mismatch.

Parameters
$value : mixed
Return values
bool

normalize()

public normalize() : array{type: "optional", base: array}
Return values
array{type: "optional", base: array}

toString()

Returns a string representation of the type.

public toString() : string
  • string - for type_string()
  • int - for type_int()
  • ?float - for type_optional(type_float())
Return values
string
On this page

Search results