Git Product home page Git Product logo

entity-sets's Introduction

entity-sets

Build Status

Handle non-persisted sets of domain entities.

Requirements

Requires PHP 7.1

Installation

Through Composer, obviously:

composer require chrisharrison/entity-sets

Why?

In domain driven design (DDD), persistence is abstracted from the domain model. I've found that if you're persisting at the level of the aggregate root, it may not make sense to separately persist certain sets of entities. This library models a set of entities without having to worry about a persistence layer.

Usage

Implementing an entity

Implement the provided Entity interface. The interface is the ValueObject interface (see value objects library) with the addition of a getId() method. This method should return an EntityId.

final class Note implements Entity
{
    use CompositeTrait;

    private $id;
    private $content;

    public function __construct(NoteId $id, Content $content)
    {
        $this->id = $id;
        $this->content = $content;
    }

    public function getId(): NoteId
    {
        return $this->id;
    }

    public function getContent(): Content
    {
        return $this->content;
    }

    public static function fromNative($native)
    {
        return new static(
            NoteId::fromNative($native['id']),
            Content::fromNative($native['content'])
        );
    }
}

Implementing an entity ID

An EntityId is also a value object with two additional methods:

interface EntityId extends ValueObject
{
    /**
     * @return static
     */
    public static function start();

    /**
     * @return static
     */
    public function next();
}

The start() method should return a new ID at the start of a sequence.

The next() method should return a new instance of the ID with the sequence advanced by one.

There is a provided trait: EntityIdTrait that implements a simple numeric integer sequence.

final class NoteId implements EntityId
{
    use EntityIdTrait;
}

Implementing a set

A set of entities should implement the EntitySet interface. There is a provided abstract class that can be used to easily implement this interface: AbstractEntitySet.

final class NoteSet extends AbstractEntitySet implements EntitySet
{
    protected static function entityType(): string
    {
        return Note::class;
    }

    protected static function entityIdType(): string
    {
        return NoteId::class;
    }
}

Using a set

Adding entities

Because the set determines an entity's identity (ID) it is not possible to directly add entity objects to a set. Instead, a native representation of the entity should be added using the addNative() method.

$nativeNote = [
    'content' => 'This is a note.',
];

$noteSet = new NoteSet;
$noteSet = $noteSet->addNative($nativeNote);

The method will return a new instance of the set with the entity added. The native value will be parsed through the fromNative method of the provided Entity class and the id property will be populated using the EntityId class.

Updating entities

To update an entity, simply pass an entity to the update() method. This will return a new instance of the set with the entity with the corresponding EntityId replaced.

Removing entities

To remove an entity, simple pass an entity to the remove() method. This will return a new instance of the set with the entity with the corresponding EntityId removed.

Retrieving entities

All entities can be retrieved from the set with set(). Or a single entity with getById().

entity-sets's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.