Download the PHP package othercodes/ddd-value-object without Composer
On this page you can find all versions of the php package othercodes/ddd-value-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download othercodes/ddd-value-object
More information about othercodes/ddd-value-object
Files in othercodes/ddd-value-object
Package ddd-value-object
Short Description Small library to easily apply the Value Object Pattern.
License MIT
Informations about the package ddd-value-object
DDD Value Object
Small library to easily manage the Value Object Pattern.
Installation
Use the following command to install with composer
:
This will automatically get the latest version and configure a composer.json
file.
Alternatively you can create the following composer.json
file and run composer install
to install it.
Usage
Build a Value object is quite simple, you just need to extend the ValueObject
Next, initialize the values in the
constructor by using the initialize
method. Finally, add invariant rules as protected method, using the prefix
invariant
(this prefix can be customized) and execute them with the checkInvariants
method.
Equality
Value equality is computed by serializing the object and hashing it with the sha256 algorithm. Alternatively, you can
override equalityHash
to calculate a proper hash for the object. This hash is used to check if the value objects are
equals or not.
Immutability
The immutability property blocks any attempt of value modification, that will end in exception:
You can customize the exception that will be thrown by overriding the immutabilityException
property. The same happens
with the error message, you just need to override the immutabilityMessages
property.
Invariants
The invariants methods must return a boolean value, true
if the invariant is successfully, false
otherwise. If any
in variant is violated you will get an exception:
By default, the invariant name is parsed and used as message error on invariant violation, but this can be easily
customized, you just need to throw an exception with your custom message instead returning false
in the invariants:
Additionally, you can fully customize how the invariant violation are managed by passing a custom function to the
checkInvariants
method:
All versions of ddd-value-object with dependencies
ramsey/uuid Version ^4.1
nesbot/carbon Version ^2.40
konekt/enum Version ^3.0