New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

antennaio/laravel-vo

Making the most of value objects in Laravel
78 2

Larablocks Analysis

antennaio/laravel-vo works with Laravel 5 (latest release v0.0.1, Apr 11, 2016), which reached end of life: no more fixes, even for security.

Recommendation checks

Not advised

Not advised: no release or commit in over 2 years.

  • No release or commit in over 2 years
  • Supports only Laravel 5, past end of life
  • No release in the last 12 months (last: Apr 11, 2016)
  • 0 downloads a month (needs 10,000)
  • 2 GitHub stars (needs 100)

State

Likely abandoned
  • No release or commit in over 2 years.
  • Last stable release: Apr 11, 2016.
  • No releases in the last 12 months.
  • First released: Apr 11, 2016.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Dec 4, 2026.

Install
composer require antennaio/laravel-vo
Latest Version:v0.0.1
Laravel:5
License:MIT
Last Updated:Aug 25, 2016
Links: GitHub  ·  Packagist
Maintainer: antennaio

Laravel VO

Making the most of value objects in Laravel

A simple ValueObject abstract class and ValueObjectCollection class that will help you make the most of value objects in Laravel.

Installation

Install through composer:

composer require antennaio/laravel-vo:~0.0.1

Usage - ValueObject

Here is a simple example of a Domain value object:

use Antennaio\VO\ValueObject;
use InvalidArgumentException;

class Domain extends ValueObject
{
    protected function validate($value)
    {
        if (!preg_match('/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', $value)) {
            throw new InvalidArgumentException('Domain is invalid: '.$value);
        }
    }
}
class MyModel extends Model
{
    ...

    public function getDomainsAttribute($domain)
    {
        return new Domain($domain);
    }

    public function setDomainAttribute(Domain $domain)
    {
        $this->attributes['domain'] = $domain;
    }
}

Now whenever the domain attribute is set you are expected to pass a Domain object:

$myModel = new MyModel;
$myModel->domain = new Domain('google.com');

In a truly DRY fashion a value object can be used to perform validation in your requests:

Validator::extend('domain', function ($attribute, $value, $parameters, $validator) {
    try {
        new Domain($value);
    } catch (InvalidArgumentException $e) {
        return false;
    }

    return true;
});

Using the custom validator to validate the domain field:

use App\Http\Requests\Request;

class SampleRequest extends Request
{
    ...

    public function rules()
    {
        return ['domain' => 'required|domain'];
    }
}

Usage - ValueObjectCollection

Sometimes it's useful to collect and store multiple value objects together. This is when ValueObjectCollection comes into play. Creating an immutable ValueObjectCollection is as simple as telling it which value objects will be stored as part of the collection.

class DomainCollection extends ValueObjectCollection
{
    protected $valueObject = Domain::class;
}
class MyModel extends Model
{
    ...

    public function getDomainsAttribute($domains)
    {
        return new DomainCollection(unserialize($domains));
    }

    public function setDomainsAttribute(DomainCollection $domains)
    {
        $this->attributes['domains'] = serialize($domains->toArray());
    }
}

In the example below DomainCollection will ONLY accept valid domain names or throw an InvalidArgumentException.

$myModel = new MyModel;
$myModel->domains = new DomainCollection(['google.com', 'amazon.com']);

// nah - InvalidArgumentException thrown
$myModel->domains = new DomainCollection(['google.com', 'amazon']);

Usually a collection is created out of user input, that's why you can also pass a string that will get parsed when a new collection is created:

$myModel = new MyModel;
$myModel->domains = new DomainCollection('google.com, amazon.com');

The delimiter (a comma by default) can be adjusted by setting delimiter property in DomainCollection.

Dislaying the domain collection in the view:

// will output a comma separated list of domain names: google.com, amazon.com
{{ $myModel->domains }}

Finally, just like in case of ValueObject, ValueObjectCollection can be used to create a custom validator:

Validator::extend('domains', function ($attribute, $value, $parameters, $validator) {
    try {
        new DomainCollection($domains);
    } catch (InvalidArgumentException $e) {
        return false;
    }

    return true;
});

Validating a collection of domains:

use App\Http\Requests\Request;

class SampleRequest extends Request
{
    ...

    public function rules()
    {
        return ['domains' => 'required|domains'];
    }
}

Other examples

For another example of HexColor and HexColorCollection check out the tests/* directory.

Related Packages

valorin/random

Random is a simple helper package designed to make it easy to generate a range o...

479,812 234
php
cline/variable-keys

Laravel Blueprint macros for variable primary key and morph types

26,787 0
php
cline/struct

Lean, attribute-driven Laravel data objects with explicit validation and Eloquen...

22,365 0
php
cline/toggl

Advanced feature flag management with strategies, time bombs, and groups

15,405 0
php
cline/sequencer

Sequential migration and operation orchestration for Laravel

14,448 0
php

Version History

Version Released Laravel License
v0.0.1 ^5.1 MIT