Download the PHP package cmath10/mapper without Composer
On this page you can find all versions of the php package cmath10/mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cmath10/mapper
More information about cmath10/mapper
Files in cmath10/mapper
Package mapper
Short Description Mapping library
License MIT
Homepage https://github.com/cmath10/mapper
Informations about the package mapper
Mapper
The library provides a utility for converting objects to each other (and arrays to objects) and transferring data between them by reusable map.
Basic example:
How it works
The main term of the library is a map. It is an object which is containing information about how to extract values from the source and inject them into a destination object. Values can be processed before injection (to get an opportunity for sanitization and nested mapping).
Map includes:
- field accessors — objects are responsible for value extraction; by default, it is used
symfony/property-access
for that purpose, but there are other options, like using callbacks orsymfony/expression-language
and presets (if the extracted value is null), also there is an interface for custom accessors; - field filters — objects are responsible for value processing; by default, there are no filters for extracted value; you can use callbacks and mapping filters for nested mapping.
By default, a map creates a set of accessors based on a class properties list, so a field from a source will be mapped on a field of a destination with the same name. You can change this behavior for specific fields if needed.
All maps must be registered in a mapper instance before using. When you call the mapper method map
it
seeks the correct map for a given source and destination, then it uses accessors to extract values, filters to process
them, and, after that, injects values to a destination. If the destination is a class name, the mapper will try to
create an instance of the given class, if the class constructor is public and all possible arguments are optional.
Installation
API
cmath10\Mapper\MapperInterface
— interface for mappers:
create(string $sourceType, string $destinationType)
— creates, registers and returns default map for two classes;register(MapInterface $map)
— registers a custom map in the mapper;map($source, $destination)
— performs mapping from a source to a destination object or class.
cmath10\mapper\MapInterface
— interface for maps:
getSourceType()
— returns name of source type; used to find the correct map;getDestinationType()
— returns name of destination type; used to find the correct map;getFieldAccessors()
— returns all fields accessors;getFieldFilters()
— returns all fields filters;getOverwriteIfSet()
— returns boolean flag; if true, destination member will be overwritten even if its value is not null;getSkipNull()
— returns boolean flag; if true, the mapper will not push null source member's value;route(string $destinationMember, string $sourceMember)
— sets mapping of destination route to source route; fluent;forMember(string $destinationMember, AccessorInterface $fieldMapper)
— sets custom accessor for field; fluent;filter(string $destinationMember, FilterInterface $fieldFilter)
— sets a custom filter for value processing; fluent;ignoreMember(string $destinationMember)
— excludes members from accounting, so they will not be filled; fluent.
cmath10\mapper\FieldAccessor\AccessorInterface
— interface for accessors:
getValue($source)
— extracts value from a source.
cmath10\mapper\FieldFilter\FitlerInterface
— interface for filters:
filter($value)
— processes the value.
cmath10\Mapper\TypeFilterInterface
— interface for type filters is used to process type names. Useful for
extracting class names from runtime generated classes like proxies in Doctrine. Used in default mapper which uses an
array of type filters as a constructor parameter (to instantiate cmath10\mapper\TypeGuesser
which is used to
determine the correct map for supplied types in mapper's map
method).
Available maps
AbstractMap
Basic class is used to create custom map classes, implements MapperInterface
;
provides following protected method:
setupDefaults
— creates a set of accessors based on a class properties list.
Usage example:
DefaultMap
Simple descendant of AbstractMap
, just calls setupDefaults
in
its constructor; the mapper create
method creates, registers and returns instance of this class.
Available accessors
ClosureAccessor
Uses callback to extract a value:
ExpressionAccessor
Uses symfony/expression-language
to extract a value:
PresetAccessor
Does not extract a value, just provides:
PropertyPathAccessor
Uses symfony/property-access
to extract a value. This accessor is used by default. Calls like:
and
are equivalent. Also, if the fields in a source and destination have the same name, and you use
setupDefaults
, you don't need to call route
or forMember
explicitly.
Available filters
ClosureFilter
Uses callback to process value:
IfNullFilter
Replaces the value if it is null:
AbstractMappingFilter
Basic class for filters that can use mapper. Requires class name in the constructor. Used for nested mapping.
ObjectMappingFilter
Descendant of AbstractMappingFilter
, uses class name and mapper to make an
object from a source member:
ObjectArrayMappingFilter
Descendant of AbstractMappingFilter
, uses class name and mapper to make an
array of objects from a source member, if the source member's value is array (returns
an empty array instead):
All versions of mapper with dependencies
symfony/property-access Version ^4.4 || ^5.0
symfony/expression-language Version ^4.4 || ^5.0