Giter Club home page Giter Club logo

vo's Introduction

ValueObject

Build Status Scrutinizer Code Quality Code Coverage Codacy Badge Maintainability SensioLabsInsight Packagist PRs Welcome

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!

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.

Install

Via composer: composer require kint/vo.

Usage

Value object class:

<?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.

Now you can create new instance of VO:

$secretAgent = new VO\SecretAgent(['name' => 'Bond', 'email' => '[email protected]']);
// 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:

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:

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! ๐Ÿ™‚

Example with custom validation rules and post-validation behavior available here.

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.

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!!!

vo's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.