Skip to content

Overview

Configuration

Configuration is a TypeLang\Mapper\Configuration DTO passed to the TypeLang\Mapper\Mapper constructor.

use TypeLang\Mapper\Configuration;
use TypeLang\Mapper\Mapper;
$mapper = new Mapper(
config: new Configuration(
// options = true|false
// default = false
strict: false,
// options = true|false
// default = true
normalizeAsArray: true,
// options = int<1, max>
// default = 1
maxViolations: 1,
// options = true|false
// default = false
rejectExtraFields: false,
// options = int<0, max>
// default = 1
expectedTypeDepth: 1,
// options = int<1, max>|null
// default = null
expectedTypeMaxFields: null,
),
);

Disables type coercion on denormalization: '42' is no longer an int.

$mapper = new Mapper(new Configuration(strict: false));
$mapper->denormalize(42, 'int'); // 42
$mapper->denormalize('42', 'int'); // 42
$mapper->denormalize(1, 'bool'); // true
$mapper->denormalize(42, 'float'); // 42.0
$mapper->denormalize((object) ['id' => 42], 'array{id: int}'); // ['id' => 42]

Whether objects are normalized into associative arrays or into stdClass.

final class User
{
public string $name = 'Alice';
}
$mapper = new Mapper(new Configuration(normalizeAsArray: true));
$mapper->normalize(new User()); // ['name' => 'Alice']

How many errors are collected before denormalization fails. By default it fails on the first one. Normalization always fails on the first one.

$mapper = new Mapper(new Configuration(maxViolations: 1));
$mapper->denormalize(['a', 2, 'c', 'd'], 'list<int>');
// throws InvalidValueException:
// $[0]: Value must be of type int, but "a" given

See collecting violations to read all of them.

Rejects fields that no property of a class (or field of a sealed shape) takes. By default they are ignored.

final class User
{
public string $name;
}
$mapper = new Mapper(new Configuration(rejectExtraFields: false));
$mapper->denormalize(['name' => 'Alice', 'age' => 42], User::class);
// User { name: 'Alice' }

How many levels of classes the messages describe by their fields. By default 1: the fields of a class, its nested classes are object. 0 describes a class as object.

final class User
{
public string $name;
public ?Address $address = null;
}
final class Address
{
public string $city;
}
$mapper = new Mapper(new Configuration(expectedTypeDepth: 0));
$mapper->denormalize('x', User::class);
// throws InvalidValueException: $: Value must be of type object, but "x" given

How many fields of a class the messages show, the others are .... By default all of them.

$mapper = new Mapper(new Configuration(expectedTypeMaxFields: null));
$mapper->denormalize('x', User::class);
// throws InvalidValueException:
// $: Value must be of type object{name: string, address?: ?object},
// but "x" given
use TypeLang\Mapper\Configuration;
use TypeLang\Mapper\Mapper;
use TypeLang\Mapper\Mapping\MetadataBuilder;
use TypeLang\Parser\TypeParser;
use TypeLang\Printer\PrettyTypePrinter;
$mapper = new Mapper(
config: new Configuration(),
builders: [/* TypeBuilderInterface */],
hooks: [/* ClassHookInterface */],
decorators: [/* TypeDecoratorInterface */],
expanders: [/* TypeExpanderInterface */],
accessors: [/* AccessStrategyInterface */],
instantiators: [/* InstantiationStrategyInterface */],
coercers: [/* 'int' => CoercerInterface, ... */],
metadata: MetadataBuilder::createDefault()->build(),
denormalizationFormatter: null, // ViolationFormatterInterface
normalizationFormatter: null, // ViolationFormatterInterface
printer: new PrettyTypePrinter(),
parser: new TypeParser(),
);

What each argument does and where it is called: see how the mapper works.