Download the PHP package funeralzone/valueobjects without Composer
On this page you can find all versions of the php package funeralzone/valueobjects. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download funeralzone/valueobjects
More information about funeralzone/valueobjects
Files in funeralzone/valueobjects
Package valueobjects
Short Description A PHP 7.1 value objects helper library.
License MIT
Informations about the package valueobjects
valueobjects
Requirements
Requires PHP >= 7.1
Installation
Through Composer, obviously:
Extensions
This library only deals with fundamental values (scalars). We've also released an extension library which provides a starting point for more complex values.
Our approach
We've written up our philosophy to PHP value objects in our A better way of writing value objects in PHP article.
Single value object
If your VO encapsulates a single value, it's most likely a scalar. We've provided some traits to deal with scalars under:
src/Scalars
Let's say you have a domain value called 'User Email'. You'd create a class which implements the ValueObject
interface:
You now need to implement the interface. But because an email can essentially be considered a special kind of string (in this simple case) the StringTrait
helper trait can implement most of the interface for you:
In our case, a user's email has other domain logic that we can encapsulate in our VO. User emails have to be a valid email:
You can see an example of how to implement single value objects in the examples directory.
Enums
Enums can be defined easily through use of the EnumTrait
. Then, the enum values are simply listed as constants on the class.
When dealing with value object serialisation, the constant names are used. They are case-sensitive. So:
In code, the trait utilises magic methods to create objects based on constant name like so:
If your IDE supports code completion and you'd like to use named methods to create enums you can add the following PHPDoc block to your enum class:
Composite value objects
A composite value object is a more complex value which is made from other values.
A Location
is made up of two VOs (latitude, longitude). We've provided a CompositeTrait
to easily implement most of the ValueObject
interface automatically. It handles toNative
serialistation by using reflection to return an array of all the class properties.
The CompositeTrait
does not implement fromNative
. We leave the construction of your object up to you.
You can see an example of how to implement composite objects in the examples directory.
Nulls, NonNulls and Nullables
This package allows you to deal with nullable value objects.
First create a type of value object.
Implement a non-null version of the value object.
Implement a null version of the value object.
Implement a nullable version of the value object.
This 'nullable' handles automatic creation of either a null or a non-null version of the interface based on the native input. For example:
The $phoneNumber
above will automatically use the NullPhoneNumber
implementation specified above.
Or:
The $phoneNumber
above will automatically use the NonNullPhoneNumber
implementation specified above.
Sets of value objects
A set of value objects should implement the Set
interface. It's just an extension of the ValueObject
interface with a few simple additions.
add
Add values from another set to the current set.remove
Remove all the values contained in another set from the current set.contains
Returnstrue
if the value exists in the current set.toArray
Returns a simple PHP array containing all of the value objects.
The other interfaces that the Set
interface extends from (\IteratorAggregate
, \ArrayAccess
, \Countable
) are for accessing the set object as though it was an array.
Non-null sets
The library provides a default implementation of the interface.
There are two abstract methods that need to be implemented.
typeToEnforce
should return a string of the class name of the value object that you want to make a set of.valuesShouldBeUnique
should return a boolean representing whether you want to force the set to be unique.
If the set is set to unique, if duplicate values are added to the set (at instantiation or through the add
method) the duplicates are filtered out.
Null and nullable sets
Just like standard value objects there are some constructs to help with creating nullable and null sets. See the Nulls, NonNulls and Nullables section for more information.
NullableSet
The set equivalent ofNullable
.NullSetTrait
The set equivalent of theNullTrait
.
Usage of sets
Iteration, access and counting
add
Merges another set.
remove
Removes values from a set by using another set as reference values.
contains
Checks whether a set contains a particular value object.
All versions of valueobjects with dependencies
beberlei/assert Version ^2.7 || ^3.1
chrisharrison/php-array-of Version ^1.0