2017 © Pedro Peláez
 

library value-objects

A simple value-object implementation for use in Entities. Includes a couple of basic types.

image

somnambulist/value-objects

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  • Sunday, November 12, 2017
  • by dredfern
  • Repository
  • 1 Watchers
  • 0 Stars
  • 344 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 34 % Grown

The README.md

Moved to https://github.com/dave-redfern/somnambulist-domain

This repository has been archived. Please update to the combined package.s, (*1)

Value Objects Library

Value Objects (VOs) are Immutable domain objects that represent some value in your domain model but without a thread of continuous identity i.e. their identity is through their properties. VOs allow your entities to encapsulate properties and provide type safety., (*2)

This library provides an abstract base class that provides a basic equality test and foundation for your VOs along with a couple of basic types. As VOs form part of YOUR domain, you should implement the VOs that you need for your domain, following your domain naming (e.g. if you do not call an email address an EmailAddress then create your own VO for that purpose)., (*3)

VOs should be self-validating during construction. For this purpose, the Assertion library by Benjamin Eberlei is used, however you may wish to use another or filter_var() etc directly., (*4)

If you see something missing or have suggestions for other methods, submit a PR or ticket., (*5)

Requirements

  • PHP 7+
  • mb_string
  • beberlei/assert
  • eloquent/enumeration
  • somnambulist/collection

Installation

Install using composer, or checkout / pull the files from github.com., (*6)

  • composer require somnambulist/value-objects

Usage

Extend the abstract value object and implement the single required method toString(). The default equality method (equals()) uses reflection on the VO properties and compares them directly - only between VO types., (*7)

For example:, (*8)

use Assert\Assert;
use Somnambulist\ValueObjects\AbstractValueObject;

class Uuid extends AbstractValueObject
{

    /**
     * @var string
     */
    private $uuid;

    /**
     * Constructor.
     *
     * @param string $uuid
     */
    public function __construct(string $uuid)
    {
        Assert::that($uuid, null, 'uuid')->notEmpty()->uuid();

        $this->uuid = $uuid;
    }

    /**
     * @return string
     */
    public function toString(): string
    {
        return $this->uuid;
    }
}

$uuid = new Uuid(\Ramsey\Uuid\Uuid::uuid4());

Usage with Doctrine

These VOs may be used with Doctrine as Embeddable objects - however if you allow the VO to be null, it will be instantiated empty so your methods / toString() should handle that case e.g.:, (*9)

A User has a nullable Profile VO, when Doctrine hydrates the User, the Profile VO will also be hydrated but empty, so if the Profile has a nickname() or avatar() method, these must support returning null and your toString() method must cast null to a string to avoid type errors., (*10)

The Versions

12/11 2017

dev-master

9999999-dev

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

12/11 2017

1.3.1

1.3.1.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

02/09 2017

1.3.0

1.3.0.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

18/08 2017

1.2.2

1.2.2.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

07/08 2017

1.2.1

1.2.1.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

28/07 2017

1.2.0

1.2.0.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

13/07 2017

1.1.0

1.1.0.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

25/06 2017

1.0.2

1.0.2.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

24/06 2017

1.0.1

1.0.1.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events

24/06 2017

1.0.0

1.0.0.0

A simple value-object implementation for use in Entities. Includes a couple of basic types.

  Sources   Download

MIT

The Requires

 

The Development Requires

ddd value objects entities domain events