1. Go to this page and download the library: Download grmvoid/basalt-value-object 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/ */
grmvoid / basalt-value-object example snippets
// Primitive obsession: any int slips through, and it is easy to swap arguments.
function register(string $firstname, string $lastname, int $age): void {}
register('Lovelace', 'Ada', -5); // compiles, runs, corrupts data
// Value objects: the type system and constructors guard the domain rules.
function register(Name $name, Age $age): void {}
register(
new Name(Firstname::fromString('Ada'), Lastname::fromString('Lovelace')),
Age::fromInt(-5), // throws ValueObjectException before reaching the domain
);
use Slpxxv\ValueObject\Boolean;
use Slpxxv\ValueObject\Double;
use Slpxxv\ValueObject\NonNegativeInteger;
use Slpxxv\ValueObject\PositiveInteger;
use Slpxxv\ValueObject\StringLiteral;
$enabled = Boolean::fromString('yes');
$ratio = Double::fromFloat(1.5);
$offset = NonNegativeInteger::fromInt(0);
$page = PositiveInteger::fromInt(1);
$label = StringLiteral::fromString('Slpxxv');
$enabled->value(); // true
(string) $page; // "1"
$label->length(); // 6