https://github.com/compositephp/entity
Lightweight and smart PHP 8.1+ Entity class with automatic Data Mapping and serialization
https://github.com/compositephp/entity
datamapper entity hydration php php81 serialization
Last synced: 9 months ago
JSON representation
Lightweight and smart PHP 8.1+ Entity class with automatic Data Mapping and serialization
- Host: GitHub
- URL: https://github.com/compositephp/entity
- Owner: compositephp
- License: mit
- Created: 2022-10-24T22:56:24.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-05-26T11:13:37.000Z (10 months ago)
- Last Synced: 2025-06-12T06:07:23.120Z (10 months ago)
- Topics: datamapper, entity, hydration, php, php81, serialization
- Language: PHP
- Homepage:
- Size: 92.8 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Composite Entity
[](https://packagist.org/packages/compositephp/entity)
[](https://github.com/compositephp/entity/actions)
[](https://codecov.io/gh/compositephp/entity/)
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.
## Features
* Converts database rows to strictly typed objects and vice versa.
* Streamlines database interactions.
Overview:
* [Requirements](#requirements)
* [Installation](#installation)
* [Quick example](#quick-example)
* [Advanced usage](#advanced-usage)
## Requirements
* PHP 8.1 or higher.
## Installation
Install using Composer::
```shell
$ composer require compositephp/entity
```
## Supported column types:
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.
## Quick example
```php
use Composite\Entity\AbstractEntity;
class User extends AbstractEntity
{
public function __construct(
public readonly int $id,
public string $email,
public ?string $name = null,
public bool $is_test = false,
public array $languages = [],
public Status $status = Status::ACTIVE,
public readonly \DateTimeImmutable $created_at = new \DateTimeImmutable(),
) {}
}
enum Status
{
case ACTIVE;
case BLOCKED;
}
```
Example of serialization:
```php
$user = new User(
id: 123,
email: 'john@example.com',
name: 'John',
is_test: false,
languages: ['en', 'fr'],
status: Status::BLOCKED,
);
var_export($user->toArray());
//will output
array (
'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:
```php
$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.
## Advanced usage
### Custom Hydration
For tailored performance, implement your own hydrator:
1. Create a class implementing `Composite\Entity\HydratorInterface`.
2. Add `Composite\Entity\Attributes\Hydrator` attribute to your entity class.
### Useful Attributes
* #### Composite\Entity\Attributes\SkipSerialization
Exclude properties from hydration.
* #### Composite\Entity\Attributes\ListOf
Define lists of entities within a property.
Example:
```php
use Composite\Entity\AbstractEntity;
use Composite\Entity\Attributes\ListOf;
class User extends AbstractEntity
{
public readonly int $id;
public function __construct(
public string $name,
#[ListOf(Setting::class)]
public array $settings = [],
) {}
}
class Setting extends AbstractEntity
{
public function __construct(
public readonly string $name,
public bool $isActive,
) {}
}
```
## License:
MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained by Composite PHP.