mako/dataclass

This package is abandoned and no longer maintained. No replacement package was suggested.

A dataclass implementation inspired by Pydantic

dev-master / 1.0.x-dev 2024-07-07 14:38 UTC

This package is auto-updated.

Last update: 2024-08-20 19:28:30 UTC


README

Tests Static analysis

Examples

Basic usage

<?php

use mako\dataclass\DataClass;

class User extends DataClass
{
	public string $username;
	public string $email;
}

// Basic instantiation

$user = new User(
	username: 'freost',
	email: 'freost@example.org',
);

// Instantiation from an array

$array = [
	'username' => 'freost',
	'email' => 'freost@example.org',
];

$user = new User(...$array);

// Instantiation from JSON

$json = <<<JSON
{
	"username": "freost",
	"email": "freost@example.org"
}
JSON;

$user = User::fromJSON($json);

$user = new User(...json_decode($json, associative: true));

Validation

<?php

use mako\dataclass\DataClass;
use mako\dataclass\attributes\Validator;

class User extends DataClass
{
	public string $username;
	public string $email;

	#[Validator('username')]
	protected function usernameMustNotContainSpace(string $username): string
	{
		if (str_contains($username, ' ') === true) {
			throw new ValueError('username must not contain a space');
		}

		return $username;
	}
}

// An error will now be thrown if the username property contains a space

$user = new User(
	username: 'freost',
	email: 'freost@example.org',
);

Nested data classes

<?php

use mako\dataclass\DataClass;
use mako\dataclass\attributes\Validator;

class Avatar extends DataClass
{
	public string $url;
}

class User extends DataClass
{
	public string $username;
	public string $email;
	public Avatar $avatar;
}

// The avatar property will be instantiated as an Avatar instance

$user = new User(
	username: 'freost',
	email: 'freost@example.org',
	avatar: ['url' => 'https://example.org/avatar.png'],
);

Arrays of nested data classes

<?php

use mako\dataclass\DataClass;
use mako\dataclass\attributes\ArrayOf;
use mako\dataclass\attributes\Validator;

class Avatar extends DataClass
{
	public string $url;
}

class User extends DataClass
{
	public string $username;
	public string $email;
	#[ArrayOf(Avatar::class)]
	public array $avatars;
}

// The elements of the avatars property will be instantiated as Avatar instances

$user = new User(
	username: 'freost',
	email: 'freost@example.org',
	avatars: [['url' => 'https://example.org/avatar.png']],
);