chalcedonyt / laravel-valueobject by chalcedonyt

Helpers to implement the Value Object pattern in Laravel 5
3,239
4
1
Package Data
Maintainer Username: chalcedonyt
Maintainer Contact: chalcedonyt@gmail.com (Timothy Teoh)
Package Create Date: 2015-10-08
Package Last Update: 2015-11-13
Language: PHP
License: MIT
Last Refreshed: 2024-03-28 03:11:13
Package Statistics
Total Downloads: 3,239
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 4
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Value Object

A simple implementation of the Value Object pattern (http://c2.com/cgi/wiki?ValueObject) with some helpers for Laravel 5.

Install

Via Composer

$ composer require chalcedonyt/laravel-valueobject:1.*

Once composer is finished, add the service provider to the providers array in app/config/app.php:

Chalcedonyt\ValueObject\Providers\ValueObjectServiceProvider::class

Usage

This package adds a helper generator for Value Objects to quickly create them.

php artisan make:valueobject NewValueObject

Enter the class or variable name for parameter 0 (Examples: \App\User or $user) [Blank to stop entering parameters] [(no_param)]:
 > $var1

 Enter the class or variable name for parameter 1 (Examples: \App\User or $user) [Blank to stop entering parameters] [(no_param)]:
 > $var2

<?php
namespace App\ValueObjects;

class NewValueObject extends Chalcedonyt\ValueObject\ValueObject
{
    /**
    * @var
    */
    protected $var1;

    /**
    * @var
    */
    protected $var2;

    /**
    *
    *  @param $var1
    *  @param $var2
    */
    public function __construct( $var1, $var2)
    {
        $this -> var1 = $var1;
        $this -> var2 = $var2;
    }
}

It also introduces a static method create that will return an instance of the ValueObject from an array.

$args = ['var1' => 1, 'var2' => 2];
$obj = NewValueObject::create($args);
$obj -> __toString(); //"{"var1":1,"var2":2}"

Change log

  • 1.1 You can now create a ValueObject inside a directory by specifying it in the classname, e.g. php artisan make:valueobject MyDir\\MyObject

Please see [CHANGELOG] for more information what has changed recently.