nikolas-lada / reflection-factory
The tiny Factory static class which loads parameters and injects them into constructors by reflection.
v1.0.0
2020-02-02 18:12 UTC
Requires
- php: >= 7.1
Requires (Dev)
- nette/tester: ^2.0
This package is auto-updated.
Last update: 2024-04-29 03:41:26 UTC
README
The factory class which loads parameters and injects them into constructors by reflection.
Installation
composer require nikolas-lada/reflection-factory
It requires PHP version 7.1+.
Usage
For \stdClass:
use \NikolasLada\ReflectionFactory\ReflectionFactory; $data = new \stdClass; /** * no matter of property order */ $data->created = '2018-03-20 12:00:00'; $data->id = 1; $data->updated = \null; $beforeCreate = function() use ($data) { $data->created = new \DateTime($data->created); if (!\is_null($data->updated)) { $data->updated = new \DateTime($data->updated); } return $data; }; $article = ReflectionFactory::createFromStdClass( \NikolasLada\ReflectionFactory\Tests\Domain\Article::class, $data, $beforeCreate ); // or $article = ReflectionFactory::create( \NikolasLada\ReflectionFactory\Tests\Domain\Article::class, $data, $beforeCreate );
For array:
use \NikolasLada\ReflectionFactory\ReflectionFactory; $data = []; /** * The createFromArray method depends on order items in array! */ $data['id'] = 1; $data['created'] = '2018-03-20 12:00:00'; $data['updated'] = '2018-03-20 12:01:13'; $data['created'] = new \DateTime($data['created']); if (!\is_null($data['updated'])) { $data['updated'] = new \DateTime($data['updated']); } $article = ReflectionFactory::createFromArray( \NikolasLada\ReflectionFactory\Tests\Domain\Article::class, $data ); // or $beforeCreate = function() use ($data) { $data['created'] = new \DateTime($data['created']); return $data; }; $article = ReflectionFactory::create( \NikolasLada\ReflectionFactory\Tests\Domain\Article::class, $data, $beforeCreate );
License
The Reflection factory library is open-sourced software licensed under: