PHPackages                             eventsourced/valueobjects - 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. eventsourced/valueobjects

Abandoned → [dql/valueobjects](/?search=dql%2Fvalueobjects)Library[Utility &amp; Helpers](/categories/utility)

eventsourced/valueobjects
=========================

A helper library for making valueobjects

4.7.1(8y ago)102654[1 issues](https://github.com/barryosull/valueobjects/issues)MITPHPPHP &gt;=5.5.9

Since Feb 9Pushed 8y ago1 watchersCompare

[ Source](https://github.com/barryosull/valueobjects)[ Packagist](https://packagist.org/packages/eventsourced/valueobjects)[ RSS](/packages/eventsourced-valueobjects/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (20)Used By (0)

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

[](#valueobjects)

ValueObjects (VOs) are the core of any DDD (Domain Driven Design) application, they ensure that values are valid and will be accepted by your domain.

In our experience, most ValueObject libraries offer a collection of ValueObjects, but they've locked them down, so it's hard to extend them and build new ones.

That's why we've built this ValueObjects toolkit, it makes building new ValueObjects quick, easy and painless.

For those using an onion architecture, consider this libary as part of the core.

ValueObjects and Validators
---------------------------

[](#valueobjects-and-validators)

### Single Values

[](#single-values)

These are ValueObjects that are given a single value that they must validate. For these ValueObjects all you need to do is specify their validator by extending the parent.

#### Making a new Single Value VO

[](#making-a-new-single-value-vo)

```
use EventSourced\ValueObject\ValueObject;

class Integer extends ValueObject\AbstractSingleValue
{
    protected function validator()
    {
        return parent::validator()->intVal();
    }
}
```

#### Accessing the value

[](#accessing-the-value)

If you want to access the value held within a single ValueObject, then do the following.

```
$integer = new Integer(1);
echo $integer->value();
```

Nice and easy.

### Validators

[](#validators)

ValueObjects use validators to do their job. Instead of writing our own library, we've decided to use the excellent [Respect Validation](http://respect.github.io/Validation/) library. It has all the validators you could ask for, and it's syntax is concise and elegant.

A helper method "validator" returns a new instance of the respect validator, it has been added to all abstract classes.

### Chaining Validators

[](#chaining-validators)

Respect Validators are chainable, so building complex validators for your value objects is a piece of cake.

```
use EventSourced\ValueObject\ValueObject\Type\AbstractSingleValue;

class Coordinate extends AbstractSingleValue
{
    protected function validator()
    {
        return parent::validator()->floatVal()->between(-90, 90);
    }
}
```

### Composite ValueObjects

[](#composite-valueobjects)

These are ValueObjects that are made from two or more ValueObjects. They are a composite that represents the pairing of the ValueObjects. An example is a locations GPS coordinate, it's actually a composite of two Coordinates, latitude and longitude.

#### Making a composite ValueObject

[](#making-a-composite-valueobject)

```
use EventSourced\ValueObject\ValueObject\Type\AbstractComposite;

class GPSCoordinates extends AbstractComposite
{
    protected $latitude;
    protected $longitude;

    public function __construct(Coordinate $latitude, Coordinate $longitude)
    {
        $this->latitude = $latitude;
        $this->longitude = $longitude;
    }

    public function latitude()
    {
        return $this->latitude;
    }

    public function longitude()
    {
        return $this->longitude;
    }
}
```

So it's simply just a holder for a bunch of valueobjects. If you want to run any validation across value objects, you should do it in the constructor. The base class takes care of the "equals" method, so you don't have to worry about that.

### Nullable Composite

[](#nullable-composite)

Making a nullable single value is easy, making a nullable composite is harder, and really should be used as a last resort. That said, it's useful to have.

To create a nullable composite, you set all the defaults values for the composite to null. That's it.

```
