guennichi / mapper
A lightweight library to map array data to PHP immutable objects
Installs: 7 732
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- phpdocumentor/reflection-docblock: ^5.3
- phpdocumentor/type-resolver: ^1.6
- symfony/filesystem: >=5.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpbench/phpbench: ^1.2
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^10
- symfony/property-access: ^6.2
- symfony/property-info: ^6.2
- symfony/serializer: ^6.2
- symfony/var-dumper: ^6.2
README
A lightweight library to map array data into PHP immutable objects, collections and strongly typed arrays via constructors.
Benchmark
Check benchmark/
directory for more details about the implementation.
Installation
composer require guennichi/mapper
Usage
use Guennichi\Collection\Collection; final class Person { public function __construct(public readonly string $name) {} } /** * @extends Collection<Person> */ final class PersonCollection extends Collection { public function __construct(Person ...$elements) { parent::__construct(...$elements); } } $input = [ ['name' => 'Person1'], ['name' => 'Person2'], ['name' => 'Person3'], ]; $mapper = new Guennichi\Mapper\Mapper(/** dependencies */) $output = $mapper->map($input, PersonCollection::class); // Result instance of PersonCollection(Person{"name": "Person1"}, Person{"name": "Person2"}, Person{"name": "Person3"})
Dedication
Special thanks to my colleague @Gabriel Ostrolucký for his support and advices to make this happen.