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.
Methods
Section titled “Methods”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); }}<?php
return [ App\User::class => [ 'properties' => [ 'name' => [ 'getter' => 'getName', 'setter' => 'setName', ], ], ],];{ "App\\User": { "properties": { "name": { "getter": "getName", "setter": "setName" } } }}App\User: properties: name: getter: getName setter: setNameApp\User: properties: name: getter: getName setter: setName$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); }}<?php
return [ App\User::class => [ 'properties' => [ 'name' => [ 'getter' => true, 'setter' => true, ], ], ],];{ "App\\User": { "properties": { "name": { "getter": true, "setter": true } } }}App\User: properties: name: getter: true setter: trueApp\User: properties: name: getter: true setter: trueOne direction
Section titled “One direction”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';}<?php
return [ App\User::class => [ 'properties' => [ 'createdAt' => [ 'only' => 'normalization', ], ], ],];{ "App\\User": { "properties": { "createdAt": { "only": "normalization" } } }}App\User: properties: createdAt: only: normalizationApp\User: properties: createdAt: only: normalization$user = $mapper->denormalize(['createdAt' => 'now'], User::class);$user->createdAt; // '2026-01-01'$mapper->normalize($user); // ['createdAt' => '2026-01-01']