1. Go to this page and download the library: Download rjds/php-dto 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/ */
rjds / php-dto example snippets
use Rjds\PhpDto\Attribute\ArrayOf;
use Rjds\PhpDto\Attribute\CastTo;
use Rjds\PhpDto\Attribute\MapFrom;
use Rjds\PhpDto\DtoMapper;
final class TagDto
{
public function __construct(
public readonly string $name,
public readonly string $url,
) {
}
}
final class ArtistDto
{
/** @param list<TagDto> $tags */
public function __construct(
public readonly string $name,
#[MapFrom('stats.play_count')]
#[CastTo('int')]
public readonly int $playCount,
#[ArrayOf(TagDto::class)]
public readonly array $tags,
) {
}
}
$mapper = new DtoMapper();
$artist = $mapper->map([
'name' => 'Arctic Monkeys',
'stats' => ['play_count' => '150316'],
'tags' => [
['name' => 'rock', 'url' => 'https://www.last.fm/tag/rock'],
['name' => 'indie', 'url' => 'https://www.last.fm/tag/indie'],
],
], ArtistDto::class);
echo $artist->playCount; // 150316 (int)
echo $artist->tags[0]->name; // rock
$artist = $mapper->map($data, ArtistDto::class);
// $artist is inferred as ArtistDto when the second argument is a literal ::class
#[MapFrom('profile.first_name')]
public readonly string $firstName
#[CastTo('datetime')]
public readonly \DateTimeImmutable $registeredAt
#[ArrayOf(TagDto::class)]
public readonly array $tags
bash
composer
neon
- vendor/rjds/php-dto/phpstan-extension.neon
bash
composer install
php vendor/bin/grumphp run
bash
php vendor/bin/infection --threads=4
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.