Download the PHP package klimick/decode without Composer
On this page you can find all versions of the php package klimick/decode. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package decode
Decode
This library allow you to take untrusted data and check that it can be represented as type T
.
- Usage example
- Built in atomics
- Generic types
- Higher order helpers
- Constraints
Usage example
Builtin type atomics
mixed()
Represents value of any possible type.
null()
Represents type for null value. Suitable for nullable types.
int()
Represents integer number.
positiveInt()
Represents positive integer number.
float()
Represents number with floating point.
numeric()
Represents either integer or float numbers.
numericString()
Like numeric()
but represents also string numbers.
bool()
Represents boolean value.
string()
Represents string value.
nonEmptyString()
Represents string that cannot be empty.
scalar()
Any scalar value.
arrKey()
Represents array key (int | string)
datetime()
Represents decoder that can create DateTimeImmutable
from string.
It uses the constructor of DateTimeImmutable
by default.
You can specify a format, and then the decoder will be use DateTimeImmutable::createFromFormat
:
It uses UTC timezone by default. You can pass different time zone during decoder instantiation:
Generic types
union(T1, T2, T3)
Represents type whose value will be of a single type out of multiple types.
arrayOf(TK, TV)
Represents array
with keys of type TK
and values of type TV
.
nonEmptyArrayOf(TK, TV)
Represents non-empty-array
with keys of type TK
and values of type TV
.
listOf(TV)
Represents list
with values of type TV
.
nonEmptyListOf(TV)
Represents non-empty-list
with values of type TV
.
shape(prop1: T, prop2: T, propN: T)
Represents array
with knows keys.
partialShape(prop1: T, prop2: T, propN: T)
Like shape
represents array
with knows keys, but each key is possibly undefined.
intersection(T1, T2, T3)
Decoder that allows to combine multiple shape
or partialShape
into the one.
tuple(T1, T2, T3)
Represents array that indexed from zero with fixed items count.
object(SomeClass::class)(prop1: T1, prop2: T2, propN: TN)
Allows to create decoder for existed class. For each parameter of the constructor, you must explicitly specify a corresponding decoder.
partialObject(SomeClass::class)(prop1: T1, prop2: T2, propN: T3)
Like object
decoder, but each parameter of the constructor must be nullable.
rec(fn() => T)
Represents recursive type. Only objects can be recursive.
fromJson(T)
Combinator for decoder of type T
which will be parsed from json representation.
Higher order helpers
optional
Allows you to mark property as possibly undefined.
default
Allows you to define a fallback value if an untrusted source does not present one.
constrained
All decoders additionally can be constrained.
List of builtin constraints
from
Helper method from
is defined for each decoder.
It allows you to specify a path for a result property or rename one.
The $
sign means root of object. You can use just $
when you want to change decoded structure nesting:
Constraints
Constraints can be attached to decoder with the constrained higher order helper.
equal (all types)
Checks that a numeric value is equal to the given one.
greater (int, float, numeric)
Checks that a numeric value is greater than the given one.
greaterOrEqual (int, float, numeric)
Checks that a numeric value is greater or equal to the given one.
less (int, float, numeric)
Checks that a numeric value is less than the given one.
lessOrEqual (int, float, numeric)
Checks that a numeric value is less or equal to the given one.
inRange (int, float, numeric)
Checks that a numeric value is in the given range
minLength (string, non-empty-string)
Checks that a string value size is not less than given one.
maxLength (string, non-empty-string)
Checks that a string value size is not greater than given one.
startsWith (string, non-empty-string)
Checks that a string value starts with the given value.
endsWith (string, non-empty-string)
Checks that a string value ends with the given value.
uuid (string, non-empty-string)
Checks that a string value is a valid UUID.
trimmed (string, non-empty-string)
Checks that a string value has no leading or trailing whitespace.
matchesRegex (string, non-empty-string)
Checks that a string value matches the given regular expression.
forall (array<array-key, T>)
Checks that the given constraint holds for all elements of an array value.
exists (array<array-key, T>)
Checks that the given constraint holds for some elements of an array value.
inCollection (array<array-key, T>)
Checks that an array value contains a value equal to the given one.
maxSize (array<array-key, T>)
Checks that an array value size is not greater than the given one.
`
minSize (array<array-key, T>)
Checks that an array value size is not less than the given one.
`
allOf (any type)
Conjunction of all constraints.
anyOf (any type)
Disjunction of all constraints.