lbacik / value-object
Simple Value Object implementation
Installs: 2 839
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: 3.*
This package is not auto-updated.
Last update: 2024-11-10 21:21:57 UTC
README
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!)