Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lightweight and smart PHP 8.1+ Entity class with automatic Data Mapping and serialization

License

NotificationsYou must be signed in to change notification settings

compositephp/entity

Repository files navigation

Latest Stable VersionBuild StatusCodecov

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

  • PHP 8.1 or higher.

Installation

Install using Composer::

$ 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

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.

Advanced usage

Custom Hydration

For tailored performance, implement your own hydrator:

  1. Create a class implementingComposite\Entity\HydratorInterface.
  2. AddComposite\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:

    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,    ) {}}

License:

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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp