kevbradwick / drift
A lightweight PHP library for mapping data to PHP classes
1.2.0
2016-11-06 08:22 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ~5.2.1
- squizlabs/php_codesniffer: ~2.5.1
This package is not auto-updated.
Last update: 2025-01-04 20:31:15 UTC
README
Drift is a PHP library for mapping arbitrary data to PHP classes.
Installation
Drift is available on Packagist, so you can use composer;
composer require kevbradwick/drift
Usage
Say you have a class with private member variables like this;
namespace Application; class Actor { /** * @Drift\String() */ private $name; /** * @Drift\Int() */ private $age; /** * @Drift\Date(field="date_of_birth") */ private $dateOfBirth; }
And you have some data, possibly consumed from an API, that looks like this;
$data = [ 'name' => 'Arnold Schwarzenegger', 'age' => 68, 'date_of_birth' => 'July 30, 1947' ]
You can then use Drift\Mapper
to create a new instance of the class,
initialised with the data.
use Drift\Mapper; use Drift\Reader\AnnotationReader; use Application\Actor; $mapper = new Mapper(new AnnotationReader()); $mapper->setData($data); $actor = $mapper->instantiate(Actor::class);
In addition to annotations, you can specify mapping using Yaml or plain old php.
Full documentation can be found on the Wiki.