odan / hydrator
A high performance hydrator for PHP.
Installs: 18 593
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2019-04-27 12:39:50 UTC
README
A high performance hydrator for PHP.
Requirements
- PHP >= 7.1
Installation
composer require odan/hydrator
Features
- Array to Object
- Object to Array
ObjectProperty
Any data key matching a publicly accessible property will be hydrated; any public properties will be used for extraction.
ClassMethod
Any data key matching a setter method will be called in order to hydrate; any method matching a getter method will be called for extraction.
Usage
// User entity class User { public $id; public $username; public $firstName; public $email; } // A row from the database $userRow = [ 'id' => 1, 'username' => 'admin', 'first_name' => 'John Doe', 'email' => 'john@example.com' ]; // Create the hydrator $hydrator = new \Odan\Hydrator\ObjectProperty(); // Convert array to a new User object (with lower camel case properties) $user = $hydrator->hydrate($userRow, new User()); print_r($user); /* User Object ( [id] => 1 [username] => admin [firstName] => John Doe [email] => john@example.com ) */ // Convert User object to an array with lower camel case keys $array = $hydrator->extract($user); print_r($array); /* Array ( [id] => 1 [username] => admin [first_name] => John Doe [email] => john@example.com ) */
Alternatives
- https://github.com/zendframework/zend-hydrator
- https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md
- https://github.com/mark-gerarts/automapper-plus
License
The MIT License (MIT). Please see License File for more information.