Skip to content

Commit

Permalink
Merge branch 'weierophinney-hotfix/4296' into develop
Browse files Browse the repository at this point in the history
Forward Port zendframework#4296
  • Loading branch information
akrabat committed Apr 26, 2013
2 parents 646eb79 + eb1da9b commit 81c1290
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
16 changes: 10 additions & 6 deletions library/Zend/Stdlib/Hydrator/ObjectProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ public function extract($object)
));
}

$self = $this;
$data = get_object_vars($object);
array_walk($data, function (&$value, $name) use ($self, &$data, $object) {
if (!$self->getFilter()->filter($name)) {

$filter = $this->getFilter();
foreach ($data as $name => $value) {
// Filter keys, removing any we don't want
if (!$filter->filter($name)) {
unset($data[$name]);
} else {
$value = $self->extractValue($name, $value, $object);
continue;
}
});
// Extract data
$data[$name] = $this->extractValue($name, $value);
}

return $data;
}

Expand Down
58 changes: 58 additions & 0 deletions tests/ZendTest/Stdlib/HydratorObjectPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace ZendTest\Stdlib;

use Zend\Stdlib\Hydrator\ObjectProperty;

class HydratorObjectPropertyTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->hydrator = new ObjectProperty();
}

public function testMultipleInvocationsWithDifferentFiltersFindsAllProperties()
{
$instance = (object) array();

$instance->id = 4;
$instance->array = array(4, 3, 5, 6);
$instance->object = (object) array();
$instance->object->id = 4;

$this->hydrator->addFilter('values', function ($property) {
return true;
});
$result = $this->hydrator->extract($instance);
$this->assertArrayHasKey('id', $result);
$this->assertEquals($instance->id, $result['id']);
$this->assertArrayHasKey('array', $result);
$this->assertEquals($instance->array, $result['array']);
$this->assertArrayHasKey('object', $result);
$this->assertSame($instance->object, $result['object']);

$this->hydrator->removeFilter('values');
$this->hydrator->addFilter('complex', function ($property) {
switch ($property) {
case 'array':
case 'object':
return false;
default:
return true;
}
});
$result = $this->hydrator->extract($instance);
$this->assertArrayHasKey('id', $result);
$this->assertEquals($instance->id, $result['id']);
$this->assertArrayNotHasKey('array', $result);
$this->assertArrayNotHasKey('object', $result);
}
}

0 comments on commit 81c1290

Please sign in to comment.