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

Commit

Permalink
Merge branch 'hotfix/45'
Browse files Browse the repository at this point in the history
Close #45
weierophinney committed May 17, 2017
2 parents cf8b079 + 6278a89 commit e09f490
Showing 8 changed files with 128 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file, in reverse
class to the `HydratorPluginManagerFactory` class, and make the
`HydratorManager` service an alias to the fully-qualified
`HydratorPluginManager` class.
- [#45](https://github.com/zendframework/zend-hydrator/pull/45) changes the
`ClassMethods` hydrator to take into account naming strategies when present,
making it act consistently with the other hydrators.

### Deprecated

18 changes: 16 additions & 2 deletions src/AbstractHydrator.php
Original file line number Diff line number Diff line change
@@ -59,6 +59,11 @@ public function getStrategy($name)
{
if (isset($this->strategies[$name])) {
return $this->strategies[$name];
} elseif ($this->hasNamingStrategy()
&& ($hydrated = $this->getNamingStrategy()->hydrate($name))
&& isset($this->strategies[$hydrated])
) {
return $this->strategies[$hydrated];
}

if (! isset($this->strategies['*'])) {
@@ -80,8 +85,17 @@ public function getStrategy($name)
*/
public function hasStrategy($name)
{
return array_key_exists($name, $this->strategies)
|| array_key_exists('*', $this->strategies);
if (array_key_exists($name, $this->strategies)) {
return true;
}

if ($this->hasNamingStrategy()
&& array_key_exists($this->getNamingStrategy()->hydrate($name), $this->strategies)
) {
return true;
}

return array_key_exists('*', $this->strategies);
}

/**
2 changes: 2 additions & 0 deletions test/ArraySerializableTest.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@
*/
class ArraySerializableTest extends TestCase
{
use HydratorTestTrait;

/**
* @var ArraySerializable
*/
2 changes: 2 additions & 0 deletions test/ClassMethodsTest.php
Original file line number Diff line number Diff line change
@@ -25,6 +25,8 @@
*/
class ClassMethodsTest extends TestCase
{
use HydratorTestTrait;

/**
* @var ClassMethods
*/
46 changes: 46 additions & 0 deletions test/HydratorTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Hydrator;

use Zend\Hydrator\NamingStrategy\NamingStrategyInterface;
use Zend\Hydrator\Strategy\StrategyInterface;
use ZendTest\Hydrator\TestAsset\SimpleEntity;

trait HydratorTestTrait
{
public function testHydrateWithNamingStrategyAndStrategy()
{
$namingStrategy = $this->createMock(NamingStrategyInterface::class);
$namingStrategy
->expects($this->any())
->method('hydrate')
->with($this->anything())
->will($this->returnValue('value'))
;

$strategy = $this->createMock(StrategyInterface::class);
$strategy
->expects($this->any())
->method('hydrate')
->with($this->anything())
->will($this->returnValue('hydrate'))
;

$this->hydrator->setNamingStrategy($namingStrategy);
$this->hydrator->addStrategy('value', $strategy);

$entity = $this->hydrator->hydrate(['foo_bar_baz' => 'blub'], new SimpleEntity());
$this->assertSame(
'hydrate',
$entity->getValue(),
sprintf('Hydrator: %s', get_class($this->hydrator))
);
}
}
2 changes: 2 additions & 0 deletions test/ObjectPropertyTest.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@
*/
class ObjectPropertyTest extends TestCase
{
use HydratorTestTrait;

/**
* @var ObjectProperty
*/
2 changes: 2 additions & 0 deletions test/ReflectionTest.php
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@
*/
class ReflectionTest extends TestCase
{
use HydratorTestTrait;

/**
* @var Reflection
*/
55 changes: 55 additions & 0 deletions test/TestAsset/SimpleEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Hydrator\TestAsset;

class SimpleEntity
{
public $value;

/**
* @param mixed $value
* @return void
*/
public function setValue($value)
{
$this->value = $value;
}

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

/**
* Exchange internal values from provided array
*
* @param array $array
* @return void
*/
public function exchangeArray(array $array)
{
if (array_key_exists('value', $array)) {
$this->setValue($array['value']);
}
}

/**
* Return an array representation of the object
*
* @return array
*/
public function getArrayCopy()
{
return ['value' => $this->getValue()];
}
}

0 comments on commit e09f490

Please sign in to comment.