Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified ObjectWalker to handle private properties in parent classes #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/DMS/Filter/ObjectWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,24 @@ private function setPropertyValue($propertyName, $value)
*/
private function getAccessibleReflectionProperty($propertyName)
{
$property = $this->reflClass->getProperty($propertyName);
$property = $this->getReflectionProperty($this->reflClass, $propertyName);
$property->setAccessible(true);

return $property;
}

private function getReflectionProperty($class, $name)
{
try {
return $class->getProperty($name);
}
catch (\ReflectionException $e) {
$parent = $class->getParentClass();
if (null === $parent) {
throw new \Exception(sprintf('property not exists %s', $name));
}

return $this->getReflectionProperty($parent, $name);
}
}
}
10 changes: 10 additions & 0 deletions tests/DMS/Filter/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,14 @@ public function testGetMetadataFactory()
{
$this->assertInstanceOf('DMS\Filter\Mapping\ClassMetadataFactory', $this->filter->getMetadataFactory());
}

public function testFilterPrivate()
{
$class = new Dummy\Classes\AnotherChildAnnotatedClass();
$class->setPrivateProperty(' needs to be trimmed ');

$this->filter->filterEntity($class);

$this->assertEquals($class->getPrivateProperty(), 'needs to be trimmed');
}
}
16 changes: 16 additions & 0 deletions tests/DMS/Tests/Dummy/Classes/AnnotatedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class AnnotatedClass
*/
public $zendalternate;

/**
* @var string
* @Filter\Trim
*/
private $privateProperty;

/**
* @param $value
* @return string
Expand All @@ -68,4 +74,14 @@ public static function anotherCallback($value)
{
return 'called_back';
}

public function setPrivateProperty($value)
{
$this->privateProperty = $value;
}

public function getPrivateProperty()
{
return $this->privateProperty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace DMS\Tests\Dummy\Classes;

use DMS\Filter\Rules as Filter;

class AnotherChildAnnotatedClass extends ChildAnnotatedClass
{
}