Download the PHP package xtompie/typed without Composer
On this page you can find all versions of the php package xtompie/typed. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xtompie/typed
More information about xtompie/typed
Files in xtompie/typed
Package typed
Short Description Typed is a library that provides a set of classes that allow you to define types for your variables.
License MIT
Informations about the package typed
Typed
Primitives as Typed Objects. Library that maps pritmitve types into typed objects. Can be used to maps request/input into objects of defined classes. Gives ErrorCollection on fail.
- Typed
- Requiments
- Installation
- Docs
- Basic
- Class
- ArrayOf
- Source
- Only
- Callback
- Factory
- Creating assert
- Others
- Limitations
Requiments
PHP >= 8.0
Installation
Using composer
Docs
Basic
Maping is done throught class constructor.
When the conditions are met then $pet
will be an instance of PetPayload
e.g.
Else $pet
will be an instance of ErrorCollection e.g.
Advantages of use typed objects:
- Better static code analysis e.g. phpstan.
- Request payload in one place.
For maping objects Typed::object()
have more precise type definition:
It is better for phpstan.
Class
ArrayOf
Source
Primitve field name can have characters that can't be used in method property name.
To solve this Source
can be used.
Only
To not allow undefined fields Only
can be used.
Callback
Factory
By default objects are build through __constructor
and their propertires.
Alternative object can be build throught static factory method provided by Factory
class attribute.
In above example the Factory attribute can be even: #[Factory]
.
If class
is null then the context class is used.
method
parameter is be default typed
.
Method must be static. Must have one argument of type mixed. Must return the object or ErrorCollection.
Creating assert
Then add created assert attriute into property.
Others
Alnum, Alpha, ArrayKeyRegex, ArrayKeyString, ArrayLengthMax, ArrayLengthMin, ArrayValueLengthMax, ArrayValueLengthMin, ArrayValueString, Choice, Date, Digit, Email, LengthMax, LengthMin, Max, Min, NotBlank, Regex, Replace, ToBool, ToInt, ToString, Trim, TrimLeft, TrimRight,
Limitations
Object properties must have a specified type.
The type cannot be a union or intersection.
If incoming primitive data can have multiple types, use a mixed property.
In such cases, you can us a To*
assert or a Callback
.