Blog Detail

11

Oct
Some General-Purpose Value Objects to Use in Laravel App cover image

arrow_back Some General-Purpose Value Objects to Use in Laravel App

Laravel Value Objects is a fantastic package that offers a bunch of general-purpose value objects you can use in your Laravel application.
The package requires PHP ^8.0 and Laravel ^9.7.

Installation

Install the package using composer:

composer require michael-rubel/laravel-value-objects

Artisan command

You can generate custom value objects with the Artisan command:

php artisan make:value-object YourNameValueObject

Usage

Boolean

$bool = new Boolean('1');
$bool = Boolean::make('1');
$bool = Boolean::from('1');

$bool->value();   // true
(string) $bool;   // 'true'
$bool->toArray(); // ['true']

Decimal

$decimal = new Decimal('10.20999', scale: 2);
$decimal = Decimal::make('10.20999', scale: 2);
$decimal = Decimal::from('10.20999', scale: 2);

$decimal->value();   // '10.20'
(string) $decimal;   // '10.20'
$decimal->toArray(); // ['10.20']

Integer

$integer = new Integer(10);
$integer = Integer::make(10);
$integer = Integer::from(10);

$integer->value();   // 10
(string) $integer;   // '10'
$integer->toArray(); // [10]

Text

$text = new Text('Lorem Ipsum is simply dummy text.');
$text = Text::make('Lorem Ipsum is simply dummy text.');
$text = Text::from('Lorem Ipsum is simply dummy text.');

$text->value();   // 'Lorem Ipsum is simply dummy text.'
(string) $text;   // 'Lorem Ipsum is simply dummy text.'
$text->toArray(); // ['Lorem Ipsum is simply dummy text.']

ClassString

$classString = new ClassString('\Exception');
$classString = ClassString::make('\Exception');
$classString = ClassString::from('\Exception');

$classString->value(); // '\Exception'
(string) $classString; // '\Exception'
$name->toArray();      // ['\Exception']

$classString->classExists();     // true
$classString->interfaceExists(); // false
$classString->instantiate();     // Exception { ... }
$classString->instantiateWith(['message' => 'My message.']); // Exception { #message: "test" ... }

Name

$name = new Name(' Company name! ');
$name = Name::make(' Company name! ');
$name = Name::from(' Company name! ');

$name->value();   // 'Company name!'
(string) $name;   // 'Company name!'
$name->toArray(); // ['Company name!']

Handle failed validation

If you want to avoid try/catching your value object when the validation fails, you can use makeOrNull method:

$bool = Boolean::makeOrNull('bad input'); // null

$bool?->value(); // null

Extending functionality

All value objects are Macroable. This way you can add new methods dynamically. If you need to extend existing methods, you can create a value object locally with make:value-object command and use inheritance.

For example:

ValueObject::macro('str', function () {
    return str($this->value());
});

$name = new Text('Lorem ipsum');

$name->str()->is('Lorem ipsum'); // true

Conditionable

Value objects utilize a Conditionable trait. You can use when and unless methods.

TaxNumber::from('PL0123456789')->when(function ($number) {
    return $number->prefix() !== null;
})->prefix();

For more details and source code, please visit Github

Published at : 11-10-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project