Laravel is awesome. Spatie's data transfer object package for PHP is awesome. But they don't cast objects like dates to DateTimes and collections are a bit of pain. Plain Old PHP Objects (POPOs) are a bit better in that regard.
Have you ever wanted to cast your JSON columns to a value object?
This package gives you 2 caster classes:
Serializer
which serializes your value object and stores it in a single JSON fieldNormalizer
which normalizes your value object and stores the properties as fields on your model
Under the hood it implements Laravel's Castable
interface with a Laravel custom cast that handles serializing between the object
(or a compatible array) and your JSON database column. It uses Symfony's Serializer to do this.
This package is inspired by Laravel Castable Data Transfer Object!
You can install the package via composer:
composer require morrislaptop/laravel-popo-caster
namespace App\Values;
class Address
{
public function __construct(
public string $street,
public string $suburb,
public string $state,
public Carbon $moved_at,
) {
}
}
Note that this should be a jsonb
or json
column in your database schema. Objects and arrays are both supported.
namespace App\Models;
use App\Values\Address;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\LaravelPopoCaster\Serializer;
/**
* @property Address $address
*/
class User extends Model
{
protected $casts = [
'address' => Serializer::class . ':' . Address::class,
'prev_addresses' => Serializer::class . ':' . Address::class . '[]',
];
}
And that's it! You can now pass either an instance of your Address
class, or even just an array with a compatible structure. It will automatically be cast between your class and JSON for storage and the data will be validated on the way in and out.
$user = User::create([
// ...
'address' => [
'street' => '1640 Riverside Drive',
'suburb' => 'Hill Valley',
'state' => 'California',
'moved_at' => now(),
],
'addresses' => [
[
'street' => '42 Wallaby Way',
'suburb' => 'Sydney',
'state' => 'NSW',
'moved_at' => '2020-01-14T00:00:00Z',
],
]
])
$residents = User::where('address->suburb', 'Hill Valley')->get();
But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.
$user->address->toMapUrl();
$user->address->getCoordinates();
$user->address->getPostageCost($sender);
$user->address->calculateDistance($otherUser->address);
$user->address->moved_at->diffForHumans();
echo (string) $user->address;
namespace App\Values;
class Money
{
public function __construct(
public int $amount,
public string $currency,
) {
}
}
Note that the properties of your value object should be columns in your database schema.
namespace App\Models;
use App\Values\Money;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\LaravelPopoCaster\Normalizer;
/**
* @property Money $money
*/
class User extends Model
{
protected $casts = [
'money' => Normalizer::class . ':' . Money::class,
];
}
And that's it! You can now pass either an instance of your Money
class, or set the individual properties on the model. It will automatically be cast between your class and properties for storage and the data will be validated on the way in and out.
$user = User::create([
// ...
'amount' => 1000,
'curency' => 'AUD',
]);
$user = User::create([
// ...
'money' => new Money(1000, 'AUD'),
])
But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.
$user->money->convertTo('USD');
Want an easy way to mock or have factories for your POPOs? Check out morrislaptop/popo-factory
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.