Skip to content

Repository files navigation

Value Object implementation

PHP Composercodecov

Instalation

Value Object on packagist: https://packagist.org/packages/lbacik/value-object. To install it you can use composer:

composer require lbacik/value-object

Example

The below example uses assertion, but it is not mandatory of course :)

declare(strict_types=1);
useSushi\ValueObject\Invariant;
useSushi\ValueObject;
usefunctionPHPUnit\Framework\assertGreaterThanOrEqual;
usefunctionPHPUnit\Framework\assertIsInt;
usefunctionPHPUnit\Framework\assertIsString;
class ExampleValueObject extends ValueObject
{
privateconstNAME_MIN_LENGTH = 4;
publicfunction__construct(
publicreadonlystring$name,
publicreadonlyint$age
) {
parent::__construct();
}
#[Invariant]
protectedfunctionvalidateName(): void
{
assertGreaterThanOrEqual(self::NAME_MIN_LENGTH, mb_strlen($this->name));
}
#[Invariant]
protectedfunctionvalidateAge(): void
{
assertIsInt($this->age);
assertGreaterThanOrEqual(0, $this->age);
}
}
$valueObjectOne = newExampleValueObject(name: "FooBar", age: 30);

For more information please visit: https://lbacik.github.io/php-sushi (should be updated soon!)

About

Simple Value Object implementation

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages