- Notifications
You must be signed in to change notification settings - Fork0
Lightweight and smart PHP 8.1+ Entity class with automatic Data Mapping and serialization
License
compositephp/entity
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Composite Entity is a PHP 8.1+ lightweight class designed for efficient and intelligent data handling.It specializes in the serialization and deserialization of data, making it highly beneficial for database management.
- Converts database rows to strictly typed objects and vice versa.
- Streamlines database interactions.
Overview:
- PHP 8.1 or higher.
Install using Composer::
$ composer require compositephp/entity
Composite Entity supports a wide range of data types:
- Basic types: String, Integer, Float, Bool, Array.
- Complex types: Object (stdClass), DateTime/DateTimeImmutable, Enum.
- Advanced types: Entity, Entity Lists or Maps, Collections (e.g., Doctrine Collection), Custom classes implementing Composite\DB\Entity\CastableInterface.
useComposite\Entity\AbstractEntity;class Userextends AbstractEntity{publicfunction__construct(publicreadonlyint$id,publicstring$email,public ?string$name =null,publicbool$is_test =false,publicarray$languages = [],publicStatus$status = Status::ACTIVE,publicreadonly\DateTimeImmutable$created_at =new \DateTimeImmutable(), ) {}}enum Status{caseACTIVE;caseBLOCKED;}
Example of serialization:
$user =newUser( id:123, email:'john@example.com', name:'John', is_test:false, languages: ['en','fr'], status: Status::BLOCKED,);var_export($user->toArray());//will outputarray ('id' =>123,'email' =>'user@example.com','name' =>'John','is_test' =>false,'languages' =>'["en","fr"]','status' =>'BLOCKED','created_at' =>'2022-01-01 11:22:33.000000',)
You can also deserialize (hydrate) entity from array:
$user = User::fromArray(['id' =>123,'email' =>'user@example.com','name' =>'John','is_test' =>false,'languages' =>'["en","fr"]','status' =>'BLOCKED','created_at' =>'2022-01-01 11:22:33.000000',]);
And that's it. No need for special getters or setters, no additional "behaviours" or extra layers of code.Composite Entity handles everything automatically, ensuring seamless data casting.
For tailored performance, implement your own hydrator:
- Create a class implementing
Composite\Entity\HydratorInterface. - Add
Composite\Entity\Attributes\Hydratorattribute to your entity class.
Exclude properties from hydration.
Define lists of entities within a property.
Example:
useComposite\Entity\AbstractEntity;useComposite\Entity\Attributes\ListOf;class Userextends AbstractEntity{publicreadonlyint$id;publicfunction__construct(publicstring$name, #[ListOf(Setting::class)]publicarray$settings = [], ) {}}class Settingextends AbstractEntity{publicfunction__construct(publicreadonlystring$name,publicbool$isActive, ) {}}
MIT License (MIT). Please seeLICENSE for more information. Maintained by Composite PHP.
About
Lightweight and smart PHP 8.1+ Entity class with automatic Data Mapping and serialization
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.