Value Object
A simple implementation of the Value Object pattern (http://c2.com/cgi/wiki?ValueObject) with some helpers for Laravel 5., (*1)
Install
Via Composer, (*2)
``` bash
$ composer require chalcedonyt/laravel-valueobject:1.*, (*3)
Once composer is finished, add the service provider to the `providers` array in `app/config/app.php`:
Chalcedonyt\ValueObject\Providers\ValueObjectServiceProvider::class, (*4)
## Usage
This package adds a helper generator for Value Objects to quickly create them.
``` php
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., (*5)
$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., (*6)