1. Go to this page and download the library: Download frankvanhest/value-objects library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
frankvanhest / value-objects example snippets
use FrankVanHest\ValueObjects\Abstracts\FloatValueObject;
final readonly class Money extends FloatValueObject
{
protected function assert(float $value): void
{
if ($value < 0) {
throw new \InvalidArgumentException('Only a value greater than zero is allowed');
}
}
}
use FrankVanHest\ValueObjects\Abstracts\FloatValueObject;
use FrankVanHest\ValueObjects\Interfaces\StringValueObject;
final readonly class Money extends FloatValueObject implements StringValueObject
{
protected function assert(float $value): void
{
if ($value < 0) {
throw new \InvalidArgumentException('Only a value greater than zero is allowed');
}
}
public function toString(): string
{
return sprintf('The value of Money is %f', $this->asFloat());
}
public static function fromString(string $value): static
{
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Only numeric values are allowed');
}
return new static((float)$value);
}
}
use FrankVanHest\ValueObjects\Interfaces\FloatValueModifier;
final readonly class DivideBy implements FloatValueModifier
{
public function __construct(private float $divideBy)
{
}
public function modify(float $value): float
{
return $value / $this->divideBy;
}
}
use FrankVanHest\ValueObjects\Factories\FloatValueObjectFactory;
$money = FloatValueObjectFactory::create(Money::class, 100.50, new DivideBy(10));