PHPackages                             morebec/value-objects - 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. morebec/value-objects

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

morebec/value-objects
=====================

PHP ValueObjects library

1.4.0(5y ago)270[9 issues](https://github.com/Morebec/ValueObjects/issues)1PHPPHP ^7.1CI failing

Since Nov 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Morebec/ValueObjects)[ Packagist](https://packagist.org/packages/morebec/value-objects)[ RSS](/packages/morebec-value-objects/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (3)Versions (7)Used By (1)

ValueObjects
============

[](#valueobjects)

A PHP Value Object Library in use by Morebec Projects

[![Build Status](https://camo.githubusercontent.com/6266ce45ac91a0e993f41e7554b39932bde495c23f4491ca5a0fea2fb03d89db/68747470733a2f2f7472617669732d63692e636f6d2f4d6f72656265632f56616c75654f626a656374732e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/Morebec/ValueObjects)[![Coverage Status](https://camo.githubusercontent.com/8713083a4550dd7bdc7ae0abe1632f4f0c36fedfa7b5c1e25bd8cd753e648d34/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4d6f72656265632f56616c75654f626a656374732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Morebec/ValueObjects?branch=master)

Value objects are small objects representing simple concepts, and whose equality is based on their internal property values rather than a specific identity.

Value objects must honour the following contract:

- They are immutable (no setters)
- They are Self Validating
- They represent and describe concepts in a clear way

Installation
------------

[](#installation)

To install the library in a project, add these lines to your `composer.json` configuration file:

```
{
    "repositories": [
        {
            "url": "https://github.com/Morebec/ValueObjects.git",
            "type": "git"
        }
    ],
    "require": {
        "morebec/value-objects": "^1.0"
    }
}
```

Usage
-----

[](#usage)

This library comes with a number of predesigned ValueObject classes, that you can use in your projects. The ValueObject either implement the `ValueObjectInterface` or extend the `BasicEnum` class.

### Creating your own Value Object using `ValueObjectInterface`

[](#creating-your-own-value-object-using-valueobjectinterface)

To create, one needs to implement the `ValueObjectInterface` and implement the two following methods:

- `__toString()`
- `isEqualTo(ValueObjectInterface $valueObject): bool`

Here's a basic example:

```
use Assert\Assertion;
use Morebec\ValueObjects\ValueObjectInterface;

/**
 * Age Value Object
 */
final class Age implements ValueObjectInterface
{
    /** @var int age */
    private $age;

    public function __construct(int $age)
    {
        Assertion::min($age, 1);
        $this->age = $age;
    }

    public function __toString()
    {
        return strval($this->age);
    }

    /**
     * Returns the value of this age object
     * @return int
     */
    public function toInt(): int
    {
        return $this->age;
    }

    /**
     * Indicates if this value object is equal to abother value object
     * @param  ValueObjectInterface $valueObject othervalue object to compare to
     * @return boolean                           true if equal otherwise false
     */
    public function isEqualTo(ValueObjectInterface $vo): bool
    {
        return (string)$this === (string)$vo;
    }
}
```

Doing that, our class can be used as follows:

```
$age = new Age(24);

// Test Equality
$maturity = new Age(18);
$age->isEqualTo($maturity); // false
$age == $maturity; // false
$age === '18'; // false

// Test Greater than
$age->toInt() >= 18; // true
$age->toInt() >= $maturity->toInt();
```

### Creating your own Enum class extending `BasicEnum`

[](#creating-your-own-enum-class-extending-basicenum)

To create a new Enum, one needs to extend the `BasicEnum` class. As an example, lets pretend we want to create a CardinalPoint Class. Since there are strictly 4 cardinal points, we will create an enum based ValueObject:

```
