Download the PHP package shipmonk/input-mapper without Composer
On this page you can find all versions of the php package shipmonk/input-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shipmonk/input-mapper
More information about shipmonk/input-mapper
Files in shipmonk/input-mapper
Package input-mapper
Short Description Performant array-to-object mapper supporting generics, array shapes, optional fields and much more!
License MIT
Informations about the package input-mapper
ShipMonk Input Mapper
Bidirectional mapper for PHP with support for generics, array shapes and nullable types. For each class, input and output mappers are generated at runtime and cached on disk. The mappers are generated only once and then reused on subsequent requests. The generated mappers are highly optimized for performance and designed to be human readable. You can see examples of generated mappers in the tests directory: output mapper.
Installation:
Features
Built-in mappers
Input Mapper comes with built-in mappers for the following types:
array,bool,float,int,mixed,string,listpositive-int,negative-int,int<TMin, TMax>,non-empty-string,non-empty-listarray<V>,array<K, V>,list<V>,non-empty-list<V>array{K1: V1, ...}?T,Optional<T>DateTimeInterface,DateTimeImmutableBackedEnum- and most importantly classes with public constructor
All built-in mappers support both input (array → object) and output (object → array) directions.
You can write your own mappers or replace the default mappers with your own.
Built-in validators
Input Mapper comes with some built-in validators (input mapping only):
- int validators:
AssertInt16AssertInt32AssertIntRangeAssertPositiveIntAssertNegativeIntAssertNonNegativeIntAssertNonPositiveIntAssertIntMultipleOf
- float validators:
AssertFloatRangeAssertPositiveFloatAssertNegativeFloatAssertNonNegativeFloatAssertNonPositiveFloatAssertFloatMultipleOf
- string validators:
AssertStringLengthAssertStringMatchesAssertStringNonEmptyAssertUrl
- list validators:
AssertListItemAssertListLengthAssertUniqueItems(compares items by===)
- date time validators:
AssertDateTimeRange
You can write your own validators if you need more.
Usage:
Write Input Class
To use Input Mapper, write a class with a public constructor and add either native or PHPDoc types to all constructor parameters.
Optional fields can either be marked with #[Optional] attribute (allowing you to specify a default value),
or if you need to distinguish between default and missing values, you can wrap the type with ShipMonk\InputMapper\Runtime\Optional class.
By default, any extra properties are not allowed. You can change that by adding #[AllowExtraKeys] over the class.
Map Input
To map input data (e.g. JSON) to objects, use MapperProvider:
Generated mappers are PHP files that get included — point tempDir at a directory owned by your application, not a shared world-writable location such as the system temporary directory, so that no other local user can pre-create it and plant files that your application would then execute.
Map Output
To convert objects back to plain arrays (e.g. for JSON serialization), use the same MapperProvider:
The output mapper converts objects to arrays, enums to their backing values, DateTimeImmutable to formatted strings, and Optional properties are omitted from the output when not defined. All types supported by input mapping are also supported by output mapping.
Adding Validation Rules
You can add validation rules by adding attributes to constructor parameters.
For example, to validate that age is between 18 and 99, you can add the AssertIntRange attribute to the constructor parameter:
Renaming keys
If the input keys do not match the property names, you can use the #[SourceKey] attribute to specify the key name:
To rename keys globally — e.g. camelCase PHP properties ↔ snake_case wire keys — configure a PropertyNameTransformer
on the DefaultMapperCompilerFactoryProvider. The transform is applied in both directions (input lookup and output emission),
and #[SourceKey] always takes precedence over it on properties where it is set:
The built-in CamelToSnakeCasePropertyNameTransformer handles common acronym boundaries (HTTPServer → http_server,
parseURL → parse_url). Implement PropertyNameTransformer yourself for other conventions.
Parsing polymorphic classes (subtypes with a common parent)
If you need to parse a hierarchy of classes, you can use the #[Discriminator] attribute.
(The discriminator field does not need to be mapped to a property if #[AllowExtraKeys] is used.)
or, with enum:
Using custom mappers
To map a class with your own hand-written mapper, implement the Mapper interface and register a factory for the class with MapperProvider:
Use registerOutputFactory() the same way to customize the output direction. The factory callable receives the concrete class name, the list of generic inner mappers, and the MapperProvider instance, so it can delegate to other mappers if needed. A factory may also be registered for an interface or parent class — it then applies to all of its implementations.
Customizing default mappers inferred from types
To customize how default mappers are inferred from types, you need to implement ShipMonk\InputMapper\Compiler\MapperFactory\MapperCompilerFactory and MapperCompilerFactoryProvider.
Then pass your factory provider to the MapperProvider — the same provider serves both the input and output directions:
Contributing
- Check your code by
composer check - Autofix coding-style by
composer fix:cs - All functionality must be tested
All versions of input-mapper with dependencies
nette/utils Version ^3.2 || ^4.0
nikic/php-parser Version ^5.0
phpstan/phpdoc-parser Version ^2.0.0