Skip to content

Metadata

Getters and setters

How a property is read on normalization and written on denormalization. By default a property with its own value is accessed directly, bypassing its hooks, and a virtual property through its hooks.

A getter takes no required arguments, a setter takes one value, its result is not used. The type is the return type of the getter and the parameter type of the setter, #[MapType] and PHPDoc of the method override it.

use TypeLang\Mapper\Mapping\MapGetter;
use TypeLang\Mapper\Mapping\MapSetter;
final class User
{
public string $name = '';
#[MapGetter('name')]
public function getName(): string
{
return \ucfirst($this->name);
}
#[MapSetter('name')]
public function setName(string $name): void
{
$this->name = \strtolower($name);
}
}
$user = $mapper->denormalize(['name' => 'JOHN'], User::class);
$user->name; // 'john'
$mapper->normalize($user); // ['name' => 'John']

Accesses a property with its own value through its PHP 8.4 hooks. The type is the type of the property for the get hook and the parameter type of the set hook.

use TypeLang\Mapper\Mapping\MapGetter;
use TypeLang\Mapper\Mapping\MapSetter;
final class User
{
public string $name = '' {
#[MapGetter]
get => \ucfirst($this->name);
#[MapSetter]
set(string $value) => \strtolower($value);
}
}

Maps a property in one direction only.

use TypeLang\Mapper\Mapping\Direction;
use TypeLang\Mapper\Mapping\MapOnly;
final class User
{
#[MapOnly(Direction::Normalization)]
public string $createdAt = '2026-01-01';
}
$user = $mapper->denormalize(['createdAt' => 'now'], User::class);
$user->createdAt; // '2026-01-01'
$mapper->normalize($user); // ['createdAt' => '2026-01-01']