https://github.com/devmakerlab/entities
https://github.com/devmakerlab/entities
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/devmakerlab/entities
- Owner: devmakerlab
- Created: 2021-03-29T11:53:20.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-02-20T02:10:36.000Z (over 4 years ago)
- Last Synced: 2024-08-10T07:02:28.032Z (almost 2 years ago)
- Language: PHP
- Size: 58.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# DevMakerLab - Entities
[](https://github.com/devmakerlab/entities/actions)
[](https://scrutinizer-ci.com/g/devmakerlab/entities/?branch=master)
[](https://packagist.org/packages/devmakerlab/entities)
This package provide a way to implements entities. Useful for your services or repositories.
## Usage
Create your entity in dedicated class :
```php
use DevMakerLab\Entity;
class Human extends Entity
{
public string $name;
public int $age;
}
```
Then instanciate a new entity like that :
```php
$human = new Human([
'name' => 'Bob',
'age' => 42,
]);
echo $human->name; // Bob
echo $human->age; // 42
```
Create your entity list like this :
```php
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 :
```php
$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
```