devmakerlab / entities
Work with entities from your services or repositories.
Installs: 3 874
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^8.9|^9.0
- symfony/var-dumper: ^5.0|^6.0
This package is auto-updated.
Last update: 2024-10-20 07:22:07 UTC
README
This package provide a way to implements entities. Useful for your services or repositories.
Usage
Create your entity in dedicated class :
use DevMakerLab\Entity; class Human extends Entity { public string $name; public int $age; }
Then instanciate a new entity like that :
$human = new Human([ 'name' => 'Bob', 'age' => 42, ]); echo $human->name; // Bob echo $human->age; // 42
Create your entity list like this :
use DevMakerLab\EntityList; class People extends EntityList { public function getYoungest(): Human { $entities = $this->entities; uasort($entities, function ($a, $b) { if ($a->age === $b->age) { return 0; } return ($a > $b) ? -1 : 1; }); return array_pop($entities); } }
Then instanciate a list like that :
$bob = new Human(['name' => 'Bob', 'age' => 45]); $junior = new Human(['name' => 'Junior', 'age' => 21]); $jane = new Human(['name' => 'Jane', 'age' => 31]); $people = new People([$bob, $junior]); $people[] = $jane; echo $people[0]->name; // Bob echo $people[1]->name; // Junior echo $people->getYoungest()->name; // Junior