Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Use concrete Class Methods to check validations #7271

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

Merged
merged 2 commits into from
Mar 18, 2013
Merged
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function mergeConstraints(ClassMetadata $source)

$this->addMemberMetadata($member);

if (!$member->isPrivate()) {
if (!$member->isPrivate($this->name)) {
$property = $member->getPropertyName();

if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Validator/Mapping/GetterMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public function __construct($class, $property)
*/
public function getPropertyValue($object)
{
return $this->getReflectionMember()->invoke($object);
return $this->newReflectionMember($object)->invoke($object);
}

/**
* {@inheritDoc}
*/
protected function newReflectionMember()
protected function newReflectionMember($objectOrClassName)
{
return new \ReflectionMethod($this->getClassName(), $this->getName());
return new \ReflectionMethod($objectOrClassName, $this->getName());
}
}
37 changes: 24 additions & 13 deletions src/Symfony/Component/Validator/Mapping/MemberMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class MemberMetadata extends ElementMetadata implements PropertyMetadat
public $cascaded = false;
public $collectionCascaded = false;
public $collectionCascadedDeeply = false;
private $reflMember;
private $reflMember = array();

/**
* Constructor.
Expand Down Expand Up @@ -123,31 +123,37 @@ public function getPropertyName()
/**
* Returns whether this member is public
*
* @param object|string $objectOrClassName The object or the class name
*
* @return Boolean
*/
public function isPublic()
public function isPublic($objectOrClassName)
{
return $this->getReflectionMember()->isPublic();
return $this->getReflectionMember($objectOrClassName)->isPublic();
}

/**
* Returns whether this member is protected
*
* @param object|string $objectOrClassName The object or the class name
*
* @return Boolean
*/
public function isProtected()
public function isProtected($objectOrClassName)
{
return $this->getReflectionMember()->isProtected();
return $this->getReflectionMember($objectOrClassName)->isProtected();
}

/**
* Returns whether this member is private
*
* @param object|string $objectOrClassName The object or the class name
*
* @return Boolean
*/
public function isPrivate()
public function isPrivate($objectOrClassName)
{
return $this->getReflectionMember()->isPrivate();
return $this->getReflectionMember($objectOrClassName)->isPrivate();
}

/**
Expand Down Expand Up @@ -202,21 +208,26 @@ public function getValue($object)
/**
* Returns the Reflection instance of the member
*
* @param object|string $objectOrClassName The object or the class name
*
* @return object
*/
public function getReflectionMember()
public function getReflectionMember($objectOrClassName)
{
if (!$this->reflMember) {
$this->reflMember = $this->newReflectionMember();
$className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
if (!isset($this->reflMember[$className])) {
$this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
}

return $this->reflMember;
return $this->reflMember[$className];
}

/**
* Creates a new Reflection instance for the member
*
* @return object
* @param object|string $objectOrClassName The object or the class name
*
* @return mixed Reflection class
*/
abstract protected function newReflectionMember();
abstract protected function newReflectionMember($objectOrClassName);
}
11 changes: 8 additions & 3 deletions src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@ public function __construct($class, $name)
*/
public function getPropertyValue($object)
{
return $this->getReflectionMember()->getValue($object);
return $this->getReflectionMember($object)->getValue($object);
}

/**
* {@inheritDoc}
*/
protected function newReflectionMember()
protected function newReflectionMember($objectOrClassName)
{
$member = new \ReflectionProperty($this->getClassName(), $this->getName());
$class = new \ReflectionClass($objectOrClassName);
while (!$class->hasProperty($this->getName())) {
$class = $class->getParentClass();
}

$member = new \ReflectionProperty($class->getName(), $this->getName());
$member->setAccessible(true);

return $member;
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Validator/Tests/Fixtures/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Entity extends EntityParent implements EntityInterface
protected $firstName;
protected $lastName;
public $reference;

private $internal;
public $data = 'Overridden data';

public function __construct($internal = null)
{
Expand All @@ -53,4 +53,9 @@ public function getLastName()
{
return $this->lastName;
}

public function getData()
{
return 'Overridden data';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class EntityParent
{
protected $firstName;
private $internal;
private $data = 'Data';

/**
* @NotNull
*/
protected $other;

public function getData()
{
return 'Data';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public function testGetPropertyValueFromPublicGetter()

$this->assertEquals('foobar from getter', $metadata->getPropertyValue($entity));
}

public function testGetPropertyValueFromOverriddenPublicGetter()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'data');

$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getPropertyValue($object)
{
}

protected function newReflectionMember()
protected function newReflectionMember($object)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class PropertyMetadataTest extends \PHPUnit_Framework_TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';

public function testInvalidPropertyName()
{
Expand All @@ -32,4 +33,13 @@ public function testGetPropertyValueFromPrivateProperty()

$this->assertEquals('foobar', $metadata->getPropertyValue($entity));
}

public function testGetPropertyValueFromOverriddenPrivateProperty()
{
$entity = new Entity('foobar');
$metadata = new PropertyMetadata(self::PARENTCLASS, 'data');

$this->assertTrue($metadata->isPublic($entity));
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
}
}