PHP Entities: Create entities and collections of objects

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-08-30 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 55 This week: 2All time: 9,834 This week: 124Up
Version License PHP version Categories
entities 1.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: 9 packages by
Country: France France
Age: ???
All time rank: 339288 in France France
Week rank: 96 Up5 in France France Up
Innovation award
Innovation award
Nominee: 5x

Details

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  
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  /  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  /  tests  
File Role Description
  Plain text file EntityListTest.php Class Class source
  Plain text file EntityTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:55
This week:2
All time:9,834
This week:124Up

For more information send a message to info at phpclasses dot org.