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, ),);Strict
Section titled “Strict”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]$mapper = new Mapper(new Configuration(strict: true));
$mapper->denormalize(42, 'int'); // 42$mapper->denormalize('42', 'int');// throws InvalidValueException: $: Value must be of type int, but "42" given
$mapper->denormalize(1, 'bool');// throws InvalidValueException: $: Value must be of type bool, but 1 given
$mapper->denormalize(42, 'float'); // 42.0$mapper->denormalize((object) ['id' => 42], 'array{id: int}'); // ['id' => 42]Normalize as array
Section titled “Normalize as array”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']$mapper = new Mapper(new Configuration(normalizeAsArray: false));
$mapper->normalize(new User()); // (object) ['name' => 'Alice']Max violations
Section titled “Max violations”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$mapper = new Mapper(new Configuration(maxViolations: 2));
$mapper->denormalize(['a', 2, 'c', 'd'], 'list<int>');// throws InvalidValueException:// $[0]: Value must be of type int, but "a" given (and 1 more)$mapper = new Mapper(new Configuration(maxViolations: 100));
$mapper->denormalize(['a', 2, 'c', 'd'], 'list<int>');// throws InvalidValueException:// $[0]: Value must be of type int, but "a" given (and 2 more)See collecting violations to read all of them.
Reject extra fields
Section titled “Reject extra fields”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' }$mapper = new Mapper(new Configuration(rejectExtraFields: true));
$mapper->denormalize(['name' => 'Alice', 'age' => 42], User::class);// throws InvalidValueException: $.age: Unexpected field "age"Expected type depth
Section titled “Expected type depth”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$mapper = new Mapper(new Configuration(expectedTypeDepth: 1));
$mapper->denormalize('x', User::class);// throws InvalidValueException:// $: Value must be of type object{name: string, address?: ?object},// but "x" given$mapper = new Mapper(new Configuration(expectedTypeDepth: 2));
$mapper->denormalize('x', User::class);// throws InvalidValueException:// $: Value must be of type// object{name: string, address?: ?object{city: string}}, but "x" givenExpected type max fields
Section titled “Expected type max fields”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$mapper = new Mapper(new Configuration(expectedTypeMaxFields: 1));
$mapper->denormalize('x', User::class);// throws InvalidValueException:// $: Value must be of type object{name: string, ...}, but "x" givenOther arguments
Section titled “Other arguments”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.