PHP Entities: Create entities and collections of objects

Recommend this page to a friend!

  Author Author  
Picture of DeGraciaMathieu
Name: DeGraciaMathieu <contact>
Classes: 9 packages by
Country: France France
Innovation award
Innovation award
Nominee: 4x


  Detailed description   Download Download .zip .tar.gz  
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.

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

  Classes of DeGraciaMathieu  >  PHP Entities  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Entities
Base name: entities
Description: Create entities and collections of objects
Version: -
PHP version: 5
License: The PHP License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Data types Modeling and manipulating data types View top rated classes
Group folder image Design Patterns Implementations of well known design patterns View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  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

Download Download all files: entities.tar.gz entities.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.

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