This library allow you to take untrusted data and check that it can be represented as type T
.
<?php
use Klimick\Decode\Decoder as t;
// Describes runtime type for array{name: string, age: int, meta: list<string>}
$libraryDefinition = t\shape(
id: t\int(),
name: t\string(),
meta: t\listOf(t\string()),
);
// Untrusted data
$json = '{
"id": 42,
"name": "Decode",
"meta": [
"runtime type system",
"psalm integration",
"with whsv26/functional"
]
}';
// If decode will fail, CastException is thrown.
// $person is array{name: string, age: int, meta: list<string>}
$person = t\tryCast(
value: $json,
to: t\fromJson($libraryDefinition),
);
// Either data type from whsv26/functional
// Left side contains decoding errors
// Right side holds decoded valid
// $person is Either<Invalid, Valid<array{name: string, age: int, meta: list<string>}>>
$personEither = t\decode(
value: $json,
with: t\fromJson($libraryDefinition),
)
// Option data type from whsv26/functional
// $person is Option<array{name: string, age: int, meta: list<string>}>
$personOption = t\cast(
value: $json,
to: t\fromJson($libraryDefinition),
);
Represents value of any possible type.
Represents type for null value. Suitable for nullable types.
$nullOrInt = union(null(), int())
Represents integer number.
Represents positive integer number.
Represents number with floating point.
Represents either integer or float numbers.
Like numeric()
but represents also string numbers.
Represents boolean value.
Represents string value.
Represents string that cannot be empty.
Any scalar value.
Represents array key (int | string)
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
:
$datetime = datetime(fromFormat: 'Y-m-d H:i:s');
It uses UTC timezone by default. You can pass different time zone during decoder instantiation:
$datetime = datetime(timezone: 'Moscow/Europe');
Represents type whose value will be of a single type out of multiple types.
// int | string
$intOrString = union(int(), string());
// float | null
$floatOrNull = union(float(), null());
// int | float | string | null
$intOrFloatOrStringOrNull = union($intOrString, $floatOrNull);
Represents array
with keys of type TK
and values of type TV
.
// array<int, string>
$arr = arrayOf(int(), string());
Represents non-empty-array
with keys of type TK
and values of type TV
.
// non-empty-array<int, string>
$nonEmptyArr = nonEmptyArrayOf(int(), string());
Represents list
with values of type TV
.
// list<string>
$list = listOf(string());
Represents non-empty-list
with values of type TV
.
// non-empty-list<string>
$list = nonEmptyListOf(string());
Represents array
with knows keys.
// array{prop1: int, prop2: string, prop3: bool}
$shape = shape(
prop1: int(),
prop2: string(),
prop3: bool(),
);
Like shape
represents array
with knows keys, but each key is possibly undefined.
// array{prop1?: int, prop2?: string, prop3?: bool}
$shape = partialShape(
prop1: int(),
prop2: string(),
prop3: bool(),
);
Decoder that allows to combine multiple shape
or partialShape
into the one.
// array{prop1: string, prop2: string, prop3?: string, prop4?: string}
$intersection = intersection(
shape(
prop1: string(),
prop2: string(),
),
partialShape(
prop3: string(),
prop4: string(),
),
);
Represents array that indexed from zero with fixed items count.
// array{int, string, bool}
$tuple = tuple(int(), string(), bool());
Allows to create decoder for existed class. For each parameter of the constructor, you must explicitly specify a corresponding decoder.
final class SomeClass
{
public function __construct(
public int $prop1,
public string $prop2,
) {}
/**
* @return DecoderInterface<SomeClass>
*/
public static function type(): DecoderInterface
{
return object(self::class)(
prop1: int(),
prop2: string(),
);
}
}
Like object
decoder, but each parameter of the constructor must be nullable.
Represents recursive type. Only objects can be recursive.
final class SomeClass
{
/**
* @param list<SomeClass> $recursive
*/
public function __construct(
public int $prop1,
public string $prop2,
public array $recursive = [],
) { }
/**
* @return DecoderInterface<SomeClass>
*/
public static function type(): DecoderInterface
{
$self = rec(fn() => self::type());
return object(self::class)(
prop1: int(),
prop2: string(),
recursive: listOf($self),
);
}
}
Combinator for decoder of type T
which will be parsed from json representation.
$shapeFromJson = fromJson(
shape(
prop1: string(),
prop2: string(),
)
);
Allows you to mark property as possibly undefined.
$personD = shape(
name: string(),
additional: listOf(string())->optional(),
);
// inferred type: array{name: string, additional?: list<string>}
$firstShape = tryCast(['name' => 'foo'], $personD);
// No additional field
// ['name' => 'foo']
print_r($firstShape);
// inferred type: array{name: string, additional?: list<string>}
$secondShape = tryCast(['name' => 'foo', 'additional' => ['bar']], $personD);
// ['name' => 'foo', 'additional' => ['bar']]
print_r($secondShape);
Allows you to define a fallback value if an untrusted source does not present one.
$personD = shape(
name: string(),
isEmployed: bool()->default(false),
);
// inferred type: array{name: string, isEmployed: bool}
$firstShape = tryCast(['name' => 'foo'], $personD);
// With default ['isEmployed' => false]
// ['name' => 'foo', 'isEmployed' => false]
print_r($firstShape);
// inferred type: array{name: string, isEmployed: bool}
$secondShape = tryCast(['name' => 'foo', 'isEmployed' => true], $personD);
// ['name' => 'foo', 'isEmployed' => true]
print_r($secondShape);
All decoders additionally can be constrained.
$personD = shape(
name: string()->constrained(
minSize(is: 1),
maxSize(is: 255),
),
street: string()->constrained(
minSize(is: 1),
maxSize(is: 255),
),
);
Helper method from
is defined for each decoder.
It allows you to specify a path for a result property or rename one.
$personD = shape(
name: string()->from('$.person'),
street: string()->from('$.address.street'),
);
$untrustedData = [
'person' => 'foo',
'address' => [
'street' => 'bar',
],
];
// Inferred type: array{name: string, street: string}
$personShape = tryCast($untrustedData, $personD);
/* Decoded data looks different rather than source: [
'name' => 'foo',
'street' => 'bar',
] */
print_r($personShape);
The $
sign means root of object. You can use just $
when you want to change decoded structure nesting:
$messengerD = shape(
kind: string()->from('$.messenger_type'),
contact: string()->from('$.messenger_contact'),
);
$personD = shape(
name: string()->from('$.person'),
street: string()->from('$.address.street'),
messenger: $messengerD->from('$'), // means "use the same data for this decoder"
);
$untrustedData = [
'person' => 'foo',
'address' => [
'street' => 'bar',
],
'messenger_type' => 'telegram',
'messenger_contact' => '@Klimick',
];
// inferred type: array{name: string, street: string, messenger: array{kind: string, messenger: string}}
$personShape = tryCast($untrustedData, $personD);
/* Decoded data looks different rather than source: [
'name' => 'foo',
'street' => 'bar',
'messenger' => [
'kind' => 'telegram',
'contact' => '@Klimick',
]
] */
print_r($personShape);
Constraints can be attached to decoder with the constrained higher order helper.
Checks that a numeric value is equal to the given one.
$fooString = string()
->constrained(equal('foo'));
Checks that a numeric value is greater than the given one.
$greaterThan10 = int()
->constrained(greater(10));
Checks that a numeric value is greater or equal to the given one.
$greaterOrEqualTo10 = int()
->constrained(greaterOrEqual(10));
Checks that a numeric value is less than the given one.
$lessThan10 = int()
->constrained(less(10));
Checks that a numeric value is less or equal to the given one.
$lessOrEqualTo10 = int()
->constrained(lessOrEqual(10));
Checks that a numeric value is in the given range
$from10to20 = int()
->constrained(inRange(10, 20));
Checks that a string value size is not less than given one.
$min10char = string()
->constrained(minLength(10));
Checks that a string value size is not greater than given one.
$max10char = string()
->constrained(maxLength(10));
Checks that a string value starts with the given value.
$startsWithFoo = string()
->constrained(startsWith('foo'));
Checks that a string value ends with the given value.
$endsWithFoo = string()
->constrained(endsWith('foo'));
Checks that a string value is a valid UUID.
$uuidString = string()
->constrained(uuid());
Checks that a string value has no leading or trailing whitespace.
$noLeadingOrTrailingSpaces = string()
->constrained(trimmed());
Checks that a string value matches the given regular expression.
$stringWithNumbers = string()
->constrained(matchesRegex('/^[0-9]{1,3}$/'));
Checks that the given constraint holds for all elements of an array value.
$allNumbersGreaterThan10 = forall(greater(than: 10));
$numbersGreaterThan10 = listOf(int())
->constrained($allNumbersGreaterThan10);
Checks that the given constraint holds for some elements of an array value.
$hasNumbersGreaterThan10 = exists(greater(than: 10));
$withNumberGreaterThan10 = listOf(int())
->constrained($hasNumbersGreaterThan10);
Checks that an array value contains a value equal to the given one.
$listWith10 = listOf(int())
->constrained(inCollection(10));
Checks that an array value size is not greater than the given one.
$max10numbers = listOf(int())
->constrained(maxSize(is: 10));
Checks that an array value size is not less than the given one.
$atLeast10numbers = listOf(int())
->constrained(minSize(is: 10));
Conjunction of all constraints.
$from100to200 = allOf(
greaterOrEqual(to: 100),
lessOrEqual(to: 200),
);
$numbersFrom100to200 = listOf(int())
->constrained($from100to200);
Disjunction of all constraints.
$from100to200 = allOf(
greaterOrEqual(to: 100),
lessOrEqual(to: 200),
);
$from300to400 = allOf(
greaterOrEqual(to: 300),
lessOrEqual(to: 400),
);
$numbersFrom100to200orFrom300to400 = listOf(int())
->constrained(anyOf($from100to200, $from300to400));