tiny-blocks / value-object
Delimits default behaviors for Value Objects.
Installs: 19 574
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- tiny-blocks/immutable-object: ^1
Requires (Dev)
- infection/infection: ^0.29
- phpmd/phpmd: ^2.15
- phpstan/phpstan: ^1
- phpunit/phpunit: ^11
- squizlabs/php_codesniffer: ^3.10
README
Overview
A Value Object (VO) is an immutable type that is only distinguishable by the state of its properties, that is, unlike an entity, which has a unique identifier and remains distinct even if its properties are identical, VOs with the same properties can be considered the same.
Because they are immutable, VOs cannot be changed once created. Modifying one is conceptually the same as discard the old one and create a new one.
More details about VOs.
Installation
composer require tiny-blocks/value-object
How to use
The library exposes available behaviors through the ValueObject
interface, and the implementation of these behaviors
through the ValueObjectAdapter
trait.
Concrete implementation
With the implementation of the ValueObject
interface, and the ValueObjectAdapter
trait, the use of
__get
, __set
and __unset
methods is suppressed, making the object immutable.
<?php namespace Example; use TinyBlocks\Vo\ValueObject; use TinyBlocks\Vo\ValueObjectBehavior; final class TransactionId implements ValueObject { use ValueObjectBehavior; public function __construct(private readonly string $value) { } }
Using the equals method
The equals
method compares the value of two VOs, and checks if they are equal.
$transactionId = new TransactionId(value: 'e6e2442f-3bd8-421f-9ac2-f9e26ac4abd2'); $otherTransactionId = new TransactionId(value: 'e6e2442f-3bd8-421f-9ac2-f9e26ac4abd2'); $transactionId->equals(other: $otherTransactionId); # true
License
Value Object is licensed under MIT.
Contributing
Please follow the contributing guidelines to contribute to the project.