Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Oct 31, 2016
0 parents commit aec3bb5
Show file tree
Hide file tree
Showing 13 changed files with 1,697 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
@@ -0,0 +1,5 @@
/.gitattributes export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.gitignore export-ignore
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/vendor
30 changes: 30 additions & 0 deletions .travis.yml
@@ -0,0 +1,30 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

# faster builds on new travis setup not using sudo
sudo: false

# cache vendor dirs
cache:
directories:
- $HOME/.composer/cache

install:
- travis_retry composer self-update && composer --version
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction

before_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
fi
script:
- phpunit --verbose $PHPUNIT_FLAGS
80 changes: 80 additions & 0 deletions Hydrator.php
@@ -0,0 +1,80 @@
<?php
namespace samdark\hydrator;

/**
* Hydrator can be used for two purposes:
*
* - To extract data from a class to be futher stored in a persistent storage.
* - To instantiate a class having its data.
*
* In both cases it is saving and filling protected and private properties without calling
* any methods which leads to ability to persist state of an object with properly incapsulated
* data.
*/
class Hydrator
{
/**
* Mapping of keys in data array to property names.
* @var array
*/
private $map;

public function __construct(array $map)
{
$this->map = $map;
}

/**
* Creates an instance of a class filled with data accoding to map
*
* @param array $data
* @param string $class
* @return object
*/
public function hydrate($data, $class)
{
$reflection = new \ReflectionClass($class);
$object = $reflection->newInstanceWithoutConstructor();

foreach ($this->map as $dataKey => $propertyName) {
if (!$reflection->hasProperty($propertyName)) {
throw new \InvalidArgumentException("There's no $propertyName property in $class.");
}

if (isset($data[$dataKey])) {
$property = $reflection->getProperty($propertyName);
if ($property->isPrivate() || $property->isProtected()) {
$property->setAccessible(true);
}
$property->setValue($object, $data[$dataKey]);
}
}

return $object;
}

/**
* Extracts data from an object according to map
*
* @param object $object
* @return array
*/
public function extract($object)
{
$data = [];

$reflection = new \ReflectionObject($object);

foreach ($this->map as $dataKey => $propertyName) {
if ($reflection->hasProperty($propertyName)) {
$property = $reflection->getProperty($propertyName);
if ($property->isPrivate() || $property->isProtected()) {
$property->setAccessible(true);
}
$data[$dataKey] = $property->getValue($object);
}
}

return $data;
}
}
15 changes: 15 additions & 0 deletions LICENSE.md
@@ -0,0 +1,15 @@
The MIT License (MIT)
Copyright (c) 2016 Alexander Makarov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of
the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions composer.json
@@ -0,0 +1,25 @@
{
"name": "samdark/hydrator",
"type": "library",
"description": "Allows to extract data from an object or create a new object based on data for the purpose of persisting state. Works with private and protected properties.",
"keywords": ["hydrator", "extract", "hydrate"],
"require-dev": {
"phpunit/phpunit": "^5.6"
},
"license": "MIT",
"support": {
"issues": "https://github.com/samdark/hydrator/issues",
"source": "https://github.com/samdark/hydrator"
},
"authors": [
{
"name": "Alexander Makarov",
"email": "sam@rmcreative.ru"
}
],
"autoload": {
"psr-4": {
"samdark\\hydrator\\": ""
}
}
}

0 comments on commit aec3bb5

Please sign in to comment.