lbacik/value-object

Simple Value Object implementation

Maintainers

Package info

github.com/lbacik/value-object

pkg:composer/lbacik/value-object

Statistics

Installs: 3 307

Dependents: 2

Suggesters: 0

Stars: 1

Open Issues: 1

v1.0 2022-04-03 08:56 UTC

This package is not auto-updated.

Last update: 2026-03-16 03:26:53 UTC


README

PHP Composer codecov

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);

use Sushi\ValueObject\Invariant;
use Sushi\ValueObject;

use function PHPUnit\Framework\assertGreaterThanOrEqual;
use function PHPUnit\Framework\assertIsInt;
use function PHPUnit\Framework\assertIsString;

class ExampleValueObject extends ValueObject
{
    private const NAME_MIN_LENGTH = 4;

    public function __construct(
        public readonly string $name,
        public readonly int $age
    ) {
        parent::__construct();
    }

    #[Invariant]
    protected function validateName(): void
    {
        assertGreaterThanOrEqual(self::NAME_MIN_LENGTH, mb_strlen($this->name));
    }

    #[Invariant]
    protected function validateAge(): void
    {
        assertIsInt($this->age);
        assertGreaterThanOrEqual(0, $this->age);
    }
}

$valueObjectOne = new ExampleValueObject(name: "FooBar", age: 30);

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