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

Skip to content
Closed
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
41 changes: 20 additions & 21 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
}

$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
$overwrite = true;

// Add the root object to the list
array_unshift($propertyValues, array(
Expand All @@ -81,18 +80,19 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
for ($i = count($propertyValues) - 1; $i >= 0; --$i) {
$objectOrArray = & $propertyValues[$i][self::VALUE];

if ($overwrite) {
$property = $propertyPath->getElement($i);
$property = $propertyPath->getElement($i);

if ($propertyPath->isIndex($i)) {
$this->writeIndex($objectOrArray, $property, $value);
} else {
$this->writeProperty($objectOrArray, $property, $value);
}
if ($propertyPath->isIndex($i)) {
$this->writeIndex($objectOrArray, $property, $value);
} else {
$this->writeProperty($objectOrArray, $property, $value);
}

if ($propertyValues[$i][self::IS_REF]) {
return;
}

$value = & $objectOrArray;
$overwrite = !$propertyValues[$i][self::IS_REF];
}
}

Expand Down Expand Up @@ -127,7 +127,6 @@ public function isWritable($objectOrArray, $propertyPath)

try {
$propertyValues = $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
$overwrite = true;

// Add the root object to the list
array_unshift($propertyValues, array(
Expand All @@ -138,21 +137,21 @@ public function isWritable($objectOrArray, $propertyPath)
for ($i = count($propertyValues) - 1; $i >= 0; --$i) {
$objectOrArray = $propertyValues[$i][self::VALUE];

if ($overwrite) {
$property = $propertyPath->getElement($i);
$property = $propertyPath->getElement($i);

if ($propertyPath->isIndex($i)) {
if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) {
return false;
}
} else {
if (!$this->isPropertyWritable($objectOrArray, $property)) {
return false;
}
if ($propertyPath->isIndex($i)) {
if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) {
return false;
}
} else {
if (!$this->isPropertyWritable($objectOrArray, $property)) {
return false;
}
}

$overwrite = !$propertyValues[$i][self::IS_REF];
if ($propertyValues[$i][self::IS_REF]) {
return true;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TestClass
private $publicAccessorWithMoreRequiredParameters;
private $publicIsAccessor;
private $publicHasAccessor;
private $publicGetter;

public function __construct($value)
{
Expand All @@ -37,6 +38,7 @@ public function __construct($value)
$this->publicAccessorWithMoreRequiredParameters = $value;
$this->publicIsAccessor = $value;
$this->publicHasAccessor = $value;
$this->publicGetter = $value;
}

public function setPublicAccessor($value)
Expand Down Expand Up @@ -166,4 +168,9 @@ private function hasPrivateHasAccessor()
{
return 'foobar';
}

public function getPublicGetter()
{
return $this->publicGetter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ public function getValidPropertyPaths()
array(array('index' => array('%!@$§.' => 'Bernhard')), '[index][%!@$§.]', 'Bernhard'),
array((object) array('%!@$§' => 'Bernhard'), '%!@$§', 'Bernhard'),
array((object) array('property' => (object) array('%!@$§' => 'Bernhard')), 'property.%!@$§', 'Bernhard'),

// nested objects and arrays
array(array('foo' => new TestClass('bar')), '[foo].publicGetSetter', 'bar'),
array(new TestClass(array('foo' => 'bar')), 'publicGetSetter[foo]', 'bar'),
array(new TestClass(new TestClass('bar')), 'publicGetter.publicGetSetter', 'bar'),
array(new TestClass(array('foo' => new TestClass('bar'))), 'publicGetter[foo].publicGetSetter', 'bar'),
array(new TestClass(new TestClass(new TestClass('bar'))), 'publicGetter.publicGetSetter.publicGetSetter', 'bar'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

publicGetter.publicGetter.publicGetSetter should be enough (no need for GetSetter in middle)

array(new TestClass(array('foo' => array('baz' => new TestClass('bar')))), 'publicGetter[foo][baz].publicGetSetter', 'bar'),
);
}

Expand All @@ -430,4 +438,12 @@ public function testTicket5755()

$this->assertEquals('foobar', $object->getProperty());
}

public function testPublicGetter()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not seem necessary since everything is already covered.

{
$this->propertyAccessor = new PropertyAccessor(true);

$this->assertTrue($this->propertyAccessor->isReadable(new TestClass('foo'), 'publicGetter'));
$this->assertEquals('foo', $this->propertyAccessor->getValue(new TestClass('foo'), 'publicGetter'));
}
}