transformers / mapping-transformer
Integrates Mapper to transform array collections using Mappings.
Installs: 53 888
Dependents: 1
Suggesters: 1
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- scriptfusion/mapper: ^1|^2|^3
- scriptfusion/porter: ^7
Requires (Dev)
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5.23
- roave/security-advisories: dev-latest
README
MappingTransformer integrates Mapper into Porter to support data transformations using Mapping
objects. A full discussion of Mapper is beyond the scope of this document but the linked repository contains comprehensive documentation. MappingTransformer builds on Mapper by providing a powerful mapping strategy called SubImport
.
Sub-imports
The SubImport
strategy provides a way to join data sets together. A mapping may contain any number of sub-imports, each of which may receive a different ImportSpecification
. A sub-import causes Porter to begin a new import operation and thus supports all import options without limitation, including importing from different providers and applying a separate mapping to each sub-import.
Signature
SubImport(ImportSpecification|callable $specificationOrCallback)
$specificationOrCallback
– Either anImportSpecification
instance orcallable
that returns such an instance.
ImportSpecification Example
The following example imports MyImportSpecification
and copies the foo field from the input data into the output mapping. Next it performs a sub-import using MyDetailsSpecification
and stores the result in the details key of the output mapping.
$records = $porter->import( (new MyImportSpecification) ->setMapping(new AnonymousMapping([ 'foo' => new Copy('foo'), 'details' => new SubImport(MyDetailsSpecification), ])) );
Callback example
The following example is the same as the previous except MyDetailsSpecification
now requires an identifier that is copied from details_id present in the input data. This is only possible using a callback since we cannot inject strategies inside specifications.
$records = $porter->import( (new MyImportSpecification) ->setMapping(new AnonymousMapping([ 'foo' => new Copy('foo'), 'details' => new SubImport( function (array $record) { return new MyDetailsSpecification($record['details_id']); } ), ])) );