GithubHelp home page GithubHelp logo

darchipov / ddd-embeddables Goto Github PK

View Code? Open in Web Editor NEW

This project forked from biberlabs/ddd-embeddables

0.0 2.0 0.0 101 KB

A collection of reusable value objects written in PHP and targeting versions 5.6 and above.

License: MIT License

PHP 100.00%

ddd-embeddables's Introduction

## DDD Embeddables

Build Status Scrutinizer Code Quality Code Coverage

A collection of reusable value objects written in PHP and targeting versions 5.6 and above. Value objects are essential building blocks of Domain Driven Design approach and described by Martin Fowler in P of EAA page 486 as below:

"Value object is a small simple object, like money or a date range, whose equality isn't based on identity."

– Martin Fowler

All classes in this library annotated as ORM\Embeddable with appropriately adjusted column attributes. This makes them ready to use in any project with Doctrine ORM as Embeddables.

Installation & Usage

Install the library using composer.

$ composer require biberlabs/ddd-embeddables

and use it in your entities:

<?php

namespace MyApp\Entity;

use Doctrine\ORM\Mapping as ORM;
use DDD\Embeddable\EmailAddress;

/**
 * @ORM\Entity
 */
class User {

    /**
     * @ORM\Embedded(class="DDD\Embeddable\EmailAddress")
     */
    private $email;

    // Returns an EmailAddress instance, not a string
    public function getEmail();

    // Use type-hinting if you need a setter
    public function setEmail(EmailAddress $email);
}

Afterwards, you can write custom DQL queries based on your requirements while accessing properties of the value objects such as:

SELECT u FROM User u WHERE u.email = :email
-- OR
SELECT p FROM Payments p WHERE p.total.currency = :currency
SELECT p FROM Payments p WHERE p.total.amount > 1000
-- OR
SELECT u FROM User u WHERE u.name.surname = :surname
SELECT u FROM User u WHERE u.name.title = :title

Value objects enables us to write much more cleaner and readable rules when dealing with the domain rules, application-wide. For example:

$username  = $user->getEmail()->getLocalpart();

or

$blacklist = ['spam4me.io', 'foo.com'];
if(in_array($user->getEmail()->getDomain(), $blacklist)) {
   // ...
}

even

if($company->hasMap()) {
    $latLng = $company->getAddress()->getGeoPoint()->toArray();
    //..
}

class Company
{
    // ...
       
    /**
     * Returns a boolean TRUE if the geolocation of the company is known,
     * FALSE otherwise.
     *
     * @return bool
     */
    public function hasMap()
    {
        return $this->getAddress()->getGeoPoint() !== null;
    }
}

Running Tests

You can run unit tests locally via issuing the command below:

$ composer test

Please make sure that all test are green before creating a PR.

PHPUnit 5.1.6 by Sebastian Bergmann and contributors.

...........                 11 / 11 (100%)

Time: 269 ms, Memory: 7.25Mb

OK (39 tests, 71 assertions)

Contributing

If you want to contribute to ddd-embeddables and make it better, your help is very welcome.

Please take a look our CONTRIBUTING.md before creating a pull request.

Further Reading

Are you interested in Domain Driven Design? Here is a list of good resources to dig in-depth.

ddd-embeddables's People

Contributors

edigu avatar hkulekci avatar yasinaydin avatar

Watchers

James Cloos avatar Dimitri Archipov avatar

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.