PHPackages                             thepsion5/entities - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. thepsion5/entities

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

thepsion5/entities
==================

A simple library for managing entities and value objects

47PHP

Since Jun 12Pushed 11y ago1 watchersCompare

[ Source](https://github.com/thepsion5/entities)[ Packagist](https://packagist.org/packages/thepsion5/entities)[ RSS](/packages/thepsion5-entities/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

\#Entities A simple package to aid in managing entities and value objects.

[![Build Status](https://camo.githubusercontent.com/09932d1d2a459e258dcb7a03b22e3c1fa3d860be27ac85a990ee27acd6392658/68747470733a2f2f7472617669732d63692e6f72672f7468657073696f6e352f656e7469746965732e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/09932d1d2a459e258dcb7a03b22e3c1fa3d860be27ac85a990ee27acd6392658/68747470733a2f2f7472617669732d63692e6f72672f7468657073696f6e352f656e7469746965732e7376673f6272616e63683d6d6173746572) [![Coverage Status](https://camo.githubusercontent.com/5ae95c26139e285fc480f3f64db335959d0eb981b1b250e93d673ef34e20da75/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7468657073696f6e352f656e7469746965732e737667)](https://coveralls.io/r/thepsion5/entities)

\##Installation

Add `thepsion5/entities` as a requirement to your composer.json:

```
{
    "require": {
        "thepsion5/entity" : "dev-master"
    }
}
```

Then run composer update.

\##Entities

Add the EntityTrait to the relevant class:

```
class User implements \Thepsion5\Entities\EntityInterface
{
    use \Thepsion5\Entities\EntityTrait;

    /* snip */
}
```

Now, your User class will have functions to get and set the ID. If there isn't an ID already defined for this entity via `setId()`, a new uuid will automatically be generated automatically using PHP's [uniqid function](php.net/uniqid).

\##Entity Collections

The `GenericEntityCollection` class provides a simple API for creating and maintaining collections of entities. Entities are automatically indexed by their IDs. Furthermore, the abstract class `AbstractEntityCollection` class is provided so that typehinted custom entity classes can be created:

```
class UserCollection extends AbstractEntityCollection
{

    public function __construct(array $users)
    {
        foreach($users as $user) {
            $this->add($user);
        }
    }

    public function add(UserEntity $user)
    {
        return $this->addEntity($user);
    }

    public function get($id)
    {
        return $this->getEntity($id);
    }

    public function has($entityOrId)
    {
        return $this->hasEntity($id);
    }
}
```

\##Value Objects

Creating a generic Value Object is as simple as creating a class and using the correct trait:

```
class Title
{
    use \Thepsion5\Entities\Traits\SimpleValueObjectTrait;
}
```

Using them is equally simple:

```
$title = new Title('This is a title');
$invalidTitle = new Title(''); //will throw an invalid argument exception
print $title; //converts the value back to a string
```

You can also define more complex rules for whether a VO is valid:

```
class Email
{
    use \Thepsion5\Entities\Traits\SimpleValueObjectTrait;

    protected function isValid($email)
    {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }

    protected function onInvalid($value)
    {
        throw new \InvalidArgumentException("The email [$value] is not a valid email address.");
    }
}
```

\##Enums

An Enum trait is provided to make the implementation and use of enums simpler. All that's needed is to define class constants representing the enum values and then use the trait:

```
class UserStatus
{
    use \Thepsion5\Entity\Traits\EnumTrait;

    const BANNED        = -1;
    const UNACTIVATED   = 1;
    const ACTIVATE      = 2;
}

UseStatus::toArray(); //['BANNED' => -1, 'UNACTIVATED' => 1, 'ACTIVATED' => 2]
$banned = UserStatus::BANNED();
$banned == new UserStatus(UserStatus::BANNED); //true
$banned->is('BANNED'); //true
```

\##Todo

- Pre-defined value objects for common use-cases

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/59b85632c6b77d0364d849b60ed2ca9de84745c09559d9f2f1b5e1c9a629900a?d=identicon)[thepsion5](/maintainers/thepsion5)

---

Top Contributors

[![thepsion5](https://avatars.githubusercontent.com/u/2222722?v=4)](https://github.com/thepsion5 "thepsion5 (13 commits)")

### Embed Badge

![Health badge](/badges/thepsion5-entities/health.svg)

```
[![Health](https://phpackages.com/badges/thepsion5-entities/health.svg)](https://phpackages.com/packages/thepsion5-entities)
```

###  Alternatives

[roave/backward-compatibility-check

Tool to compare two revisions of a public API to check for BC breaks

5953.3M56](/packages/roave-backward-compatibility-check)[robinvdvleuten/ulid

Universally Unique Lexicographically Sortable Identifier (ULID) implementation for PHP.

4583.6M29](/packages/robinvdvleuten-ulid)[bref/extra-php-extensions

Extra PHP extensions for your lambda application.

2244.3M8](/packages/bref-extra-php-extensions)[mageplaza/magento-2-seo-extension

Magento 2 SEO extension

138506.6k2](/packages/mageplaza-magento-2-seo-extension)[mattketmo/camel

Tiny library to handle words case transformation

29357.0k17](/packages/mattketmo-camel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
