PHP code example of marvin255 / value-object

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

    

marvin255 / value-object example snippets


use Marvin255\ValueObject\StringValueObject;
use Marvin255\ValueObject\StringNonEmptyValueObject;

// Basic string value object
$name = new StringValueObject('John Doe');
echo $name->value(); // "John Doe"

// String that cannot be empty
$title = new StringNonEmptyValueObject('Developer');
echo $title->value(); // "Developer"

use Marvin255\ValueObject\IntValueObject;
use Marvin255\ValueObject\IntPositiveValueObject;
use Marvin255\ValueObject\IntNonNegativeValueObject;
use Marvin255\ValueObject\IntNegativeValueObject;
use Marvin255\ValueObject\IntNonPositiveValueObject;

// Any integer
$count = new IntValueObject(-5);
echo $count->value(); // -5

// Positive integer only (> 0)
$age = new IntPositiveValueObject(25);
echo $age->value(); // 25

// Non-negative integer (>= 0)
$score = new IntNonNegativeValueObject(0);
echo $score->value(); // 0

// Negative integer only (< 0)
$debt = new IntNegativeValueObject(-100);
echo $debt->value(); // -100

// Non-positive integer (<= 0)
$offset = new IntNonPositiveValueObject(-5);
echo $offset->value(); // -5

use Marvin255\ValueObject\FloatValueObject;

// Any float value
$price = new FloatValueObject(19.99);
echo $price->value(); // 19.99

use Marvin255\ValueObject\BcMathNumberValueObject;

$file = new BcMathNumberValueObject('123.123123');
echo $empty->value(); // "123.123123"

use Marvin255\ValueObject\PercentageValueObject;

// Percentage between 0 and 100
$progress = new PercentageValueObject(75.5);
echo $progress->value(); // 75.5

$complete = new PercentageValueObject(100);
echo $complete->value(); // 100

$empty = new PercentageValueObject(0.0);
echo $empty->value(); // 0

// Decimal precision supported
$accuracy = new PercentageValueObject(99.99);
echo $accuracy->value(); // 99.99

// This will throw an exception
// new PercentageValueObject(101); // throws InvalidArgumentException

use Marvin255\ValueObject\EmailValueObject;

$email = new EmailValueObject('[email protected]');
echo $email->value(); // "[email protected]"

use Marvin255\ValueObject\UriValueObject;

$uri = new UriValueObject('https://example.com/path?query=value');
echo $uri->value(); // "https://example.com/path?query=value"

use Marvin255\ValueObject\FileInfoValueObject;

$file = new FileInfoValueObject('/path/to/file.txt');
echo $file->value()->getPathname(); // "/path/to/file.txt"
echo $file->getMimeType();          // "text/plain"