https://github.com/nark3d/phalueobjects
PHP Value Objects
https://github.com/nark3d/phalueobjects
domain-driven-design php php-value-objects usable value-object valueobject
Last synced: 2 months ago
JSON representation
PHP Value Objects
- Host: GitHub
- URL: https://github.com/nark3d/phalueobjects
- Owner: nark3d
- License: gpl-2.0
- Created: 2015-09-12T19:47:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-19T07:02:57.000Z (about 8 years ago)
- Last Synced: 2025-02-28T19:06:00.656Z (3 months ago)
- Topics: domain-driven-design, php, php-value-objects, usable, value-object, valueobject
- Language: PHP
- Homepage: http://bestservedcold.com
- Size: 1.06 MB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PhalueObjects
[](https://travis-ci.org/nark3d/PhalueObjects)
[](https://scrutinizer-ci.com/g/nark3d/phalueobjects/)
[](https://scrutinizer-ci.com/g/nark3d/phalueobjects/)
[](https://insight.sensiolabs.com/projects/5820fcfd-8593-4b76-99a6-397b94cd659c)A generic set of PHP Value Objects for use in any project.
[Wikipedia](https://en.wikipedia.org/wiki/Domain-driven_design)
> When people exchange business cards, they generally do not distinguish between each unique card; they only are concerned about the information printed on the card. In this context, business cards are value objects.## Table of contents
* [Installation](#installation)
* [Philosophy](#philosophy)
* Documents
* [Format](src/Format/README.md)## Installation
```shell
composer require best-served-cold/phalue-objects
```## Philosophy
To make this code consistent, we've stuck to a certain set of restrictions:### Rules
* [**Must** be immutable](#must-be-immutable)
* [**Must** contain one value](#must-contain-one-value)
* [**Can** instantiate new object from value](#can-instantiate-new-object-from-value)
* [**Can** be created from multiple arguments](#can-be-created-from-multiple-arguments)
* [**Can** be equal regardless of object](#can-be-equal-regardless-of-object)
* [**Must** have a zero lifespan](#must-have-a-zero-lifespan)*Disclaimer: This is my interpretation of "The rules".*
#### Must be immutable
The value object's value must be set at the time of construction.
At no point should the value be mutated within the object.#### Must contain one value
The value object can only be constructed from one value, this can be
any of the following types:
* boolean
* integer
* float/double
* string
* array
* object
* resource
* null#### Can instantiate new object from value
Rather than mutating, a new object can be instantiated from an existing one.Example:
```php
//...
public function double()
{
return new static($this->getValue() * 2);
}
...//
```#### Can be created from multiple arguments
Instead of an object having multiple object properties, it should be created from
multiple arguments.Example:
```php
//...
public static function fromVars($one = 1, $two = 2, $three = 3)
{
return new static([$one, $two, $three]);
}
...//
```#### Can be equal regardless of object
The type of a value object is irrelevant to equality:Example:
```php
//...
$bob = $stringValueObject->equals($csvValueObject);
...//
``````$bob``` is true where the type and value are equal.
#### Must have a zero lifespan
Value objects must not persist data between run times. For example:
no database or session information should be collected from within the
object.## Conventions
Follow [PSR-FIG](http://www.php-fig.org/) rules.### Constructor
Example:```php
new SomeClass('value');
```* **Must** only have one argument of any type
### Creation methods (From)
Example:```php
SomeClass::fromSomeObject($someObject);
```* **Always** start with "from"
* **Must** be static
* **Can** contain multiple arguments
* **Must** return new static instance### Conversion methods (To)
Example:```php
$someObject->toArray();
```* **Always** start with "to"
* **Must not** be static
* **Must** have zero arguments
* **Must** return new static instance