Download the PHP package somnambulist/value-objects without Composer
On this page you can find all versions of the php package somnambulist/value-objects. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download somnambulist/value-objects
More information about somnambulist/value-objects
Files in somnambulist/value-objects
Package value-objects
Short Description A simple value-object implementation for use in Entities. Includes a couple of basic types.
License MIT
Informations about the package value-objects
Moved to https://github.com/dave-redfern/somnambulist-domain
This repository has been archived. Please update to the combined package.s
Value Objects Library
Value Objects (VOs) are Immutable domain objects that represent some value in your domain model but without a thread of continuous identity i.e. their identity is through their properties. VOs allow your entities to encapsulate properties and provide type safety.
This library provides an abstract base class that provides a basic equality test and foundation for your VOs along with a couple of basic types. As VOs form part of YOUR domain, you should implement the VOs that you need for your domain, following your domain naming (e.g. if you do not call an email address an EmailAddress then create your own VO for that purpose).
VOs should be self-validating during construction. For this purpose, the Assertion library by Benjamin Eberlei is used, however you may wish to use another or filter_var() etc directly.
If you see something missing or have suggestions for other methods, submit a PR or ticket.
Requirements
- PHP 7+
- mb_string
- beberlei/assert
- eloquent/enumeration
- somnambulist/collection
Installation
Install using composer, or checkout / pull the files from github.com.
- composer require somnambulist/value-objects
Usage
Extend the abstract value object and implement the single required method toString(). The default equality method (equals()) uses reflection on the VO properties and compares them directly - only between VO types.
For example:
Usage with Doctrine
These VOs may be used with Doctrine as Embeddable objects - however if you allow the VO to be null, it will be instantiated empty so your methods / toString() should handle that case e.g.:
A User has a nullable Profile VO, when Doctrine hydrates the User, the Profile VO will also be hydrated but empty, so if the Profile has a nickname() or avatar() method, these must support returning null and your toString() method must cast null to a string to avoid type errors.
Links
All versions of value-objects with dependencies
ext-mbstring Version *
beberlei/assert Version ~3.0
eloquent/enumeration Version ~5.1
somnambulist/collection Version ~2.1