apfelfrisch/data-transfer-object

0.2.3 2021-05-18 10:25 UTC

This package is auto-updated.

Last update: 2024-04-21 20:53:21 UTC


README

Unit Test Static Analysis

This package is heavily inspired by spatie/data-transfer-object. The main difference is that the DataTransferObject doesn't come with a constructor. That makes the Initialization less magical which is nicer for static analysis.

Installation

You can install the package via composer:

composer require apfelfrisch/data-transfer-object

Usage

Here's what a DTO looks like:

use Apfelfrisch\DataTransferObject;
use Apfelfrisch\DataTransferObject\Casters\DateTimeCast;

class MyDTO extends DataTransferObject
{
    public function __construct(
        public int $a,

        public float $b,

        public OtherDTO $otherDTO,
        
        #[DateTimeCast]
        public DateTime $date,
    ) { }
}

You could construct this DTO with Parameter casting like so:

$dto = MyDTO::fromArrayWithCast([
    'a' => 1,
    'b' => 2.2,
    'otherDTO' => ['id' => 3],
    'date' => '2021-05-01'
);