Download the PHP package yeast/loafpan without Composer
On this page you can find all versions of the php package yeast/loafpan. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yeast/loafpan
More information about yeast/loafpan
Files in yeast/loafpan
Package loafpan
Short Description Object and config hydration from associative arrays
License MIT
Informations about the package loafpan
Loafpan
A simple PHP 8 native object expansion (or hydration as some call it) framework
Features
- Only focused on deserialization of data, thus perfect for configs
- JSON Schema generation (also works for yaml!)
- PHP 8 Attribute guided
- Simple templating/generics support
- Simple alternative format expanders
- Custom expander support allows for expanding out-of-branch objects
- Readable code generation, for ease of debugging and speed
Installation
Very quick use
Usage
To start using Loafpan, you first need to annotate your objects, in the next few examples we will create to fake classes to explain the details
An expandable object is called a "Unit", Units either need to be either annotated with Unit
or have a custom expander
registered. However for now we'll cover annotated Units
In the first example, Sandwich is a purely setter based unit, and will use the default constructor to instantiate the object and then set the properties manually.
Loafpan is here guided by the Field
attributes, which signal which properties can be applied from an object.
Topping however is a purely expander based, and will take only a string, as this is the only instantiation method available
Expander functions are public static functions with either 1 or 2 arguments (the optional second one being a Loafpan instance), the first argument defines the input type that can be used to expand the object from, e.g. Topping can be made from a string alone
While Topping only defines 1 Expander, you can add multiple, be aware that the results can be unpredictable when the types overlap.
With the 2 classes we just defined, a valid json object for Sandwich would be
which will roughly translate into
Finally, applying all this, create a Loafpan instance with a directory in which it can scribble php files and
call expand
with the class you want to expand into, in our case Sandwich
and the user input.
The following example shows this usage
If the options given here doesn't give you enough flexibility, you can always implement your own expander by
implementing UnitExpander
(\Yeast\Loafpan\UnitExpander
) on a class and either registering it to loafpan by using
the registerExpander
function on a Loafpan instance or setting the expander
parameter on the Unit
attribute
See src/Expander for some examples of custom UnitExpanders
Default expanders
list<T>
- only accepts an array list with items of type Tmap<T>
- only accepts an associative array with items of type TDateTime
/DateTimeImmutable
- only accepts a string with an ISO-8601 formatted dateRamsey\Uuid\Uuid
/Ramsey\Uuid\UuidInterface
- only accepts a string with a properly formatted UUID
Todo
- Generate in-depth errors about invalid input
All versions of loafpan with dependencies
brick/varexporter Version ^0.3.5
symfony/polyfill-php81 Version ^1.24
jawira/case-converter Version ^3.4