2017 © Pedro Peláez
 

library vo

Simple, yet powerful immutable Value Object.

image

kint/vo

Simple, yet powerful immutable Value Object.

  • Friday, May 11, 2018
  • by cn007b
  • Repository
  • 1 Watchers
  • 0 Stars
  • 980 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

ValueObject

Build Status Scrutinizer Code Quality Code Coverage Codacy Badge Maintainability SensioLabsInsight Packagist PRs Welcome, (*1)

Value Object (VO) - it's immutable container for your parameters, which knows only about your parameters (how to set/get them) and about validation rules (what type of data allowed for certain parameter). You are not allowed to create VO with invalid parameters (exception will be thrown) - it provides you opportunity to be sure that your VO is always valid. Also, VO is immutable - hence you're on safe side and no one will modify your VO since it created!, (*2)

Main benefits: now you can use this VO everywhere and you don't have to worry about validation, you don't have to mix your business logic with validation code on every application layer (controller, service, model, etc), also you can use it as type-hint hence your code will be more clear, interface oriented and more precise., (*3)

Install

Via composer: composer require kint/vo., (*4)

Usage

Value object class:, (*5)

<?php

namespace VO;

use ValueObject\ValueObject;

/**
 * @method string getName Gets name.
 * @method string getEmail Gets email.
 */
class SecretAgent extends ValueObject
{
     protected function getRules()
     {
        return [
            'name' => ['NotBlank', 'Length' => ['min' => 5]],
            'email' => ['NotBlank', 'Email', 'Length' => ['min' => 15]],
        ];
     }
}

getRules - required method, which returns validation rules for properties of your value object.
These rules - Symfony validators! So you have all power of Symfony validation in your VO!
List of all validation rules (constraints) available here., (*6)

Now you can create new instance of VO:, (*7)

$secretAgent = new VO\SecretAgent(['name' => 'Bond', 'email' => 'james.bond@mi6.com']);
// Now you can use magic methods and get values from your VO.
$secretAgentName = $secretAgent->getName();
$secretAgentEmail = $secretAgent->getEmail();
// Also you can pass this VO as parameter.
$controller->doSomethingWithSecretAgent($secretAgent);

In case of invalid data - you'll receive exception with information about all violated rules:, (*8)

use VO\SecretAgent;
use ValueObject\Exception\ValidationException;

try {
    $secretAgent = new SecretAgent(['name' => 'Bond', 'email' => 'error']);
} catch (ValidationException $e) {
    $errors = $e->getMessages();
}

As result your code will be super simple, just like that:, (*9)

class SecretAgentController
{
    public function indexAction(array $postData)
    {
        (new SecretAgentService())->doSomethingWithSecretAgent(new VO\SecretAgent($postData));
    }
}

class SecretAgentService
{
    public function doSomethingWithSecretAgent(VO\SecretAgent $secretAgent)
    {
        (new SecretAgentModel())->update($secretAgent);
    }
}

class SecretAgentModel
{
    public function update(VO\SecretAgent $secretAgent)
    {
        $secretAgentName = $secretAgent->getName();
        $secretAgentEmail = $secretAgent->getEmail();
        // Update model.
    }
}

ENJOY! 🙂, (*10)

Example with custom validation rules and post-validation behavior available here., (*11)

Optional parameters and default values

Optional parameters and default values are out of value object's scope and you have to assign default value to optional parameter before creating value object., (*12)

Value Object vs Form

Value Object looks bit similar to Form, but the key difference - is that you can't rely on form everywhere because form may be valid or invalid at any point of time and you have always to check $form->isValid(); but with Value Object you 100% sure it's always valid, hence you can loosely use it everywhere!!!, (*13)

The Versions

11/05 2018

dev-master

9999999-dev https://github.com/cn007b/vo

Simple, yet powerful immutable Value Object.

  Sources   Download

MIT

The Requires

 

php ddd value object vo valueobject

02/03 2017

dev-cn007b-patch-1

dev-cn007b-patch-1 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

MIT

The Requires

 

php ddd value object vo valueobject

09/12 2016

1.1.6

1.1.6.0 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

MIT

The Requires

 

php ddd value object vo valueobject

21/10 2016

1.1.5

1.1.5.0 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

MIT

The Requires

 

php ddd value object vo valueobject

19/10 2016

1.1.4

1.1.4.0 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

MIT

The Requires

 

php ddd value object vo valueobject

19/10 2016

1.1.3

1.1.3.0 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

MIT

The Requires

 

php ddd value object vo valueobject

18/10 2016

1.1.2

1.1.2.0 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

The Requires

 

php ddd value object vo valueobject

18/10 2016

1.1.1

1.1.1.0 https://github.com/cn007b/vo

ValueObject.

  Sources   Download

The Requires

 

php ddd value object vo valueobject