Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jul 5, 2016
0 parents commit c309a38
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*~
vendor/*
.*~
composer.lock
phpunit.xml
build/
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[![Latest Stable Version](https://poser.pugx.org/mouf/tdbm-hydrator/v/stable)](https://packagist.org/packages/mouf/tdbm-hydrator)
[![Total Downloads](https://poser.pugx.org/mouf/tdbm-hydrator/downloads)](https://packagist.org/packages/mouf/tdbm-hydrator)
[![Latest Unstable Version](https://poser.pugx.org/mouf/tdbm-hydrator/v/unstable)](https://packagist.org/packages/mouf/tdbm-hydrator)
[![License](https://poser.pugx.org/mouf/tdbm-hydrator/license)](https://packagist.org/packages/mouf/tdbm-hydrator)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/tdbm-hydrator/badges/quality-score.png?b=1.0)](https://scrutinizer-ci.com/g/thecodingmachine/tdbm-hydrator/?branch=1.0)
[![Build Status](https://travis-ci.org/thecodingmachine/tdbm-hydrator.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/tdbm-hydrator)
[![Coverage Status](https://coveralls.io/repos/thecodingmachine/tdbm-hydrator/badge.svg?branch=1.0&service=github)](https://coveralls.io/github/thecodingmachine/tdbm-hydrator?branch=1.0)


About the TDBM hydrator
=======================

This package contains an **hydrator**.
An **hydrator** is a class that takes an array in parameter and maps it to an object (calling the appropriate getters and setters).

Unlike most existing hydrators that need an object instance to be filled, the *tdbm-hydrator* package can (optionally) create a new object instance. This is very useful when you have big constructors with lots of parameters to fill from the array, which happen often if you use [TDBM](http://mouf-php.com/packages/mouf/database.tdbm).

Note that this package is completely standalone and does not need TDBM or Mouf to run. Still, this hydrator is known to work very well with TDBM generated beans (hence the name).

45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name" : "mouf/tdbm-hydrator",
"description" : "A PHP hydrator allowing easy mapping between an array and an object.",
"keywords" : [
"tdbm",
"hydrator"
],
"homepage" : "http://mouf-php.com/packages/mouf/tdbm-hydrator",
"type" : "library",
"license" : "MIT",
"authors" : [{
"name" : "David Négrier",
"email" : "d.negrier@thecodingmachine.com",
"homepage" : "http://mouf-php.com"
}
],
"require" : {
"php" : ">=7.0",
"danielstjules/stringy": "^2.3.2"
},
"require-dev" : {
"phpunit/phpunit": "~5.0",
"satooshi/php-coveralls": "~1.0"
},
"autoload" : {
"psr-4" : {
"Mouf\\Hydrator\\" : "src/"
}
},
"autoload-dev" : {
"psr-4" : {
"Mouf\\Hydrator\\" : "tests/"
}
},
"minimum-stability" : "dev",
"prefer-stable": true,
"extra" : {
"mouf" : {
"logo" : "icon.png",
"section": {
"name": "Database"
}
}
}
}
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
23 changes: 23 additions & 0 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Mouf\Hydrator;

interface Hydrator
{
/**
* Creates a new $className object, filling it with $data.
*
* @param array $data
* @param string $className
* @return object
*/
public function hydrateNewObject(array $data, string $className);

/**
* Fills $object with $data.
*
* @param array $data
* @param $object
* @return object
*/
public function hydrateObject(array $data, $object);
}
13 changes: 13 additions & 0 deletions src/MissingParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace Mouf\Hydrator;


class MissingParameterException extends \BadMethodCallException
{
public static function create(string $missingParameter)
{
return new self(sprintf('Unable to find require parameter %s', $missingParameter));
}
}
67 changes: 67 additions & 0 deletions src/TdbmHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php


namespace Mouf\Hydrator;


use Stringy\StaticStringy;

class TdbmHydrator implements Hydrator
{

/**
* Creates a new $className object, filling it with $data.
*
* @param array $data
* @param string $className
* @return object
*/
public function hydrateNewObject(array $data, string $className)
{
$reflectionClass = new \ReflectionClass($className);
$constructor = $reflectionClass->getConstructor();
$constructorParameters = $constructor->getParameters();

$underscoredData = [];
foreach ($data as $key => $value) {
$underscoredData[(string) StaticStringy::underscored($key)] = $value;
}

$parameters = [];

foreach ($constructorParameters as $constructorParameter) {
$underscoredVariableName = (string) StaticStringy::underscored($constructorParameter->getName());

if (array_key_exists($underscoredVariableName, $underscoredData)) {
$value = $underscoredData[$underscoredVariableName];
unset($underscoredData[$underscoredVariableName]);
} else {
if (!$constructorParameter->isDefaultValueAvailable()) {
throw MissingParameterException::create($constructorParameter->getName());
}
$value = $constructorParameter->getDefaultValue();
}

// TODO: check if sub object!
$parameters[] = $value;
}

$object = $reflectionClass->newInstanceArgs($parameters);

$this->hydrateObject($data, $object);

return $object;
}


/**
* Fills $object with $data.
*
* @param array $data
* @param $object
*/
public function hydrateObject(array $data, $object)
{
// TODO
}
}
58 changes: 58 additions & 0 deletions tests/Fixtures/FixtureA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


namespace Mouf\Hydrator\Fixtures;


class FixtureA
{
private $one;
private $two;
private $three;

/**
* FixtureA constructor.
* @param $one
* @param $two
* @param int $three
*/
public function __construct($one, $two, $three = 3)
{
$this->one = $one;
$this->two = $two;
$this->three = $three;
}

/**
* @return mixed
*/
public function getOne()
{
return $this->one;
}

/**
* @return mixed
*/
public function getTwo()
{
return $this->two;
}

/**
* @return int
*/
public function getThree()
{
return $this->three;
}

/**
* @param int $three
*/
public function setThree($three)
{
$this->three = $three;
}

}
49 changes: 49 additions & 0 deletions tests/TdbmHydratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace Mouf\Hydrator;


use Mouf\Hydrator\Fixtures\FixtureA;

class TdbmHydratorTest extends \PHPUnit_Framework_TestCase
{
public function testHydrate()
{
$hydrator = new TdbmHydrator();
$a = $hydrator->hydrateNewObject([
'one' => 1,
'two' => 2,
], FixtureA::class);

/** @var $a FixtureA */
$this->assertEquals(1, $a->getOne());
$this->assertEquals(2, $a->getTwo());
$this->assertEquals(3, $a->getThree());
}

public function testHydrateNull()
{
$hydrator = new TdbmHydrator();
$a = $hydrator->hydrateNewObject([
'one' => 1,
'two' => 2,
'three' => null
], FixtureA::class);

/** @var $a FixtureA */
$this->assertEquals(1, $a->getOne());
$this->assertEquals(2, $a->getTwo());
$this->assertEquals(null, $a->getThree());
}

public function testMissingCompulsoryParameter()
{
$hydrator = new TdbmHydrator();
$this->expectException(MissingParameterException::class);
$hydrator->hydrateNewObject([
'one' => 1,
], FixtureA::class);

}
}

0 comments on commit c309a38

Please sign in to comment.