Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hydrator' of https://github.com/prolic/zf2 into feature…
Browse files Browse the repository at this point in the history
…/hydrator-returns-object
  • Loading branch information
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
18 changes: 7 additions & 11 deletions src/Hydrator/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,21 @@ public function extract($object)
*
* @param array $data
* @param object $object
* @return void
* @return object
* @throws Exception\BadMethodCallException for an $object not implementing exchangeArray() or populate()
*/
public function hydrate(array $data, $object)
{
if (!is_callable(array($object, 'exchangeArray'))
&& !is_callable(array($object, 'populate'))
) {
if (is_callable(array($object, 'exchangeArray'))) {
$object->exchangeArray($data);
} else if (is_callable(array($object, 'populate'))) {
$object->populate($data);
} else {
throw new Exception\BadMethodCallException(sprintf(
'%s expects the provided object to implement exchangeArray() or populate()',
__METHOD__
));
}

if (is_callable(array($object, 'exchangeArray'))) {
$object->exchangeArray($data);
return;
}

$object->populate($data);
return $object;
}
}
3 changes: 2 additions & 1 deletion src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function extract($object)
*
* @param array $data
* @param object $object
* @return void
* @return object
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function hydrate(array $data, $object)
Expand All @@ -122,5 +122,6 @@ public function hydrate(array $data, $object)
$method = 'set' . ucfirst($property);
$object->$method($value);
}
return $object;
}
}
2 changes: 1 addition & 1 deletion src/Hydrator/HydratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function extract($object);
*
* @param array $data
* @param object $object
* @return void
* @return object
*/
public function hydrate(array $data, $object);
}
3 changes: 2 additions & 1 deletion src/Hydrator/ObjectProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function extract($object)
*
* @param array $data
* @param object $object
* @return void
* @return object
* @throws Exception\BadMethodCallException for a non-object $object
*/
public function hydrate(array $data, $object)
Expand All @@ -74,5 +74,6 @@ public function hydrate(array $data, $object)
foreach ($data as $property => $value) {
$object->$property = $value;
}
return $object;
}
}
6 changes: 3 additions & 3 deletions test/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testHydratorClassMethodsCamelCase()
$this->assertEquals($datas['fooBar'], '1');
$this->assertTrue(isset($datas['fooBarBaz']));
$this->assertFalse(isset($datas['foo_bar']));
$hydrator->hydrate(array('fooBar' => 'foo', 'fooBarBaz' => 'bar'), $this->classMethodsCamelCase);
$this->classMethodsCamelCase = $hydrator->hydrate(array('fooBar' => 'foo', 'fooBarBaz' => 'bar'), $this->classMethodsCamelCase);
$this->assertEquals($this->classMethodsCamelCase->getFooBar(), 'foo');
$this->assertEquals($this->classMethodsCamelCase->getFooBarBaz(), 'bar');
}
Expand All @@ -73,7 +73,7 @@ public function testHydratorClassMethodsCamelCaseWithSetterMissing()
$this->assertEquals($datas['fooBar'], '1');
$this->assertFalse(isset($datas['fooBarBaz']));
$this->assertFalse(isset($datas['foo_bar']));
$hydrator->hydrate(array('fooBar' => 'foo'), $this->classMethodsCamelCaseMissing);
$this->classMethodsCamelCaseMissing = $hydrator->hydrate(array('fooBar' => 'foo'), $this->classMethodsCamelCaseMissing);
$this->assertEquals($this->classMethodsCamelCaseMissing->getFooBar(), 'foo');
$this->assertEquals($this->classMethodsCamelCaseMissing->getFooBarBaz(), '2');
}
Expand All @@ -86,7 +86,7 @@ public function testHydratorClassMethodsUnderscore()
$this->assertEquals($datas['foo_bar'], '1');
$this->assertTrue(isset($datas['foo_bar_baz']));
$this->assertFalse(isset($datas['fooBar']));
$hydrator->hydrate(array('foo_bar' => 'foo', 'foo_bar_baz' => 'bar'), $this->classMethodsUnderscore);
$this->classMethodsUnderscore = $hydrator->hydrate(array('foo_bar' => 'foo', 'foo_bar_baz' => 'bar'), $this->classMethodsUnderscore);
$this->assertEquals($this->classMethodsUnderscore->getFooBar(), 'foo');
$this->assertEquals($this->classMethodsUnderscore->getFooBarBaz(), 'bar');
}
Expand Down

0 comments on commit 2e4447c

Please sign in to comment.