PHP code example of slpxxv / value-object

1. Go to this page and download the library: Download slpxxv/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/ */

    

slpxxv / value-object example snippets


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

use Slpxxv\ValueObject\Identifier\Uuid;

$id = Uuid::generate();
$sameId = Uuid::fromString($id->value());
$binary = $id->toBytes();

$id->equals($sameId);       // true
Uuid::fromBytes($binary);   // equivalent UUID

use Slpxxv\ValueObject\DateTime\DateTime;

$createdAt = DateTime::fromString('2026-08-12T12:00:00+00:00');
$expiresAt = $createdAt->modify('+30 days');

$createdAt->lessThan($expiresAt); // true
$createdAt->format();             // DateTimeInterface::ATOM by default

use Slpxxv\ValueObject\Person\Age;
use Slpxxv\ValueObject\Person\Female;
use Slpxxv\ValueObject\Person\Firstname;
use Slpxxv\ValueObject\Person\Lastname;
use Slpxxv\ValueObject\Person\Name;

$name = new Name(
    Firstname::fromString('Ada'),
    Lastname::fromString('Lovelace'),
);

$age = Age::fromInt(36); // accepted range: 0..150
$gender = Female::create();

(string) $name; // "Ada Lovelace"