DDD Embeddables


A collection of reusable value objects written in PHP and targeting versions 8.0 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 ORMEmbeddable 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 MyAppEntity; use DoctrineORMMapping as ORM; use DDDEmbeddableEmailAddress; /** * @ORMEntity */ class User { /** * @ORMEmbedded(class="DDDEmbeddableEmailAddress") */ 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 9.5.25 #StandWithUkraine ................................................................. 65 / 75 ( 86%) .......... 75 / 75 (100%) Time: 00:00.037, Memory: 6.00 MB OK (75 tests, 124 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.

Value Objects - Martin Fowler Separating Concerns using Embeddables - Doctrine ORM documentation Domain-Driven Design in PHP - Leanpub 380 pages e-book. Agregate Componenet Pattern In Action - Another good article from 2009 Domain Driven Design Concepts in ZF2 - An article written by Oleg Krivtsov in 2014 Domain Driven Design Quickly - A quickly-readable minibook and introduction to the fundamentals of DDD by InfoQ.

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。