PHP Classes

PHP Entities: Create entities and collections of objects

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 93 All time: 9,900 This week: 47Up
Version License PHP version Categories
entities 1.0.0The PHP License5PHP 5, Data types, Design Patterns
Description 

Author

This package can be used to create entities and collections of objects.

It provides a base entity class that should be extended by applications to create application specify entity classes.

It takes an array of key-value pairs to assign to the entity class.

It also provides an abstract consistency check function that implementation classes should define to evaluate if the entity object has its variables set in a consistent way according to application rules.

The package also provides and entity collection class that can gather a collection of entity objects like an array.

Innovation Award
PHP Programming Innovation award nominee
August 2020
Number 3
Entities are usually objects defined by classes with variables that store values of simple objects that represent important application data structures.

This package provides an implementation of simple classes that can be used define entity objects that can provide their own consistency checking methods.

It provides also a base implementation of objects that be used to manage collections of entity objects.

Manuel Lemos
Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 21 packages by
Country: France France
Age: ???
All time rank: 289980 in France France
Week rank: 4 Up1 in France France Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Documentation

alchemistery/entities

This package provide a way to implements entities. Useful for your services or repositories.

Usage

Create your entity in dedicated class :

use Alchemistery\Entity;

class Human extends Entity
{
    public $name;
    public $age;

    public function isConsistent(): bool
    {
        return ! is_null($this->name) && ! is_null($this->age);
    }
}

Then instanciate a new entity like that :

$human = new Human([
    'name' => 'Bob',
    'age' => 42,
]);

$human->name // Bob
$human->age // 42
$human->isConsistent(); // true

Create your entity list like this :

use Alchemistery\EntityList;

class People extends EntityList
{
    public function hasExpectedType(Entity $entity): bool
    {
        return $entity instanceof Human::class;
    }

    public function getYoungest(): Human
    {
        $consistentPeople = $this->getConsistentEntities();
        
        uasort($consistentPeople, function ($a, $b) {
            if ($a->age === $b->age) {
                return 0;
            }
    
            return ($a > $b) ? -1 : 1;
        });

        return array_pop($consistentPeople);
    }
}

Then instanciate a list like that :

$bob = new Human(['name' => 'Bob', 'age' => 12]);
$john = new Human(['name' => 'John', 'age' => 10]);

$people = new People([$bob, $john]);

$people[0]->name // Bob
$people[1]->name // John
$people->getYoungest()->name // John

  Files folder image Files (9)  
File Role Description
Files folder imagesrc (3 files)
Files folder imagetests (2 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files (9)  /  src  
File Role Description
  Plain text file Entity.php Class Class source
  Plain text file EntityList.php Class Class source
  Plain text file UnexpectedEntityException.php Class Class source

  Files folder image Files (9)  /  tests  
File Role Description
  Plain text file EntityListTest.php Class Class source
  Plain text file EntityTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:93
This week:0
All time:9,900
This week:47Up