Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
if ($propertyPath->isIndex($i)) {
if ($overwrite = !isset($zval[self::REF])) {
$ref = &$zval[self::REF];
$ref = $zval[self::VALUE];
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the prev line is useless now?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or $ref is a typo

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a typo: the prev line turns $zval[self::REF] into a reference, which is required for writeIndex to work.

Copy link

Choose a reason for hiding this comment

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

why not change these lines to more readable $zval[self::REF] = &$zval[self::VALUE]?

}
$this->writeIndex($zval, $property, $value);
if ($overwrite) {
Expand Down
176 changes: 176 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

class TestClass
Copy link
Member Author

Choose a reason for hiding this comment

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

borrowed from 2.7

{
public $publicProperty;
protected $protectedProperty;
private $privateProperty;

private $publicAccessor;
private $publicMethodAccessor;
private $publicGetSetter;
private $publicAccessorWithDefaultValue;
private $publicAccessorWithRequiredAndDefaultValue;
private $publicAccessorWithMoreRequiredParameters;
private $publicIsAccessor;
private $publicHasAccessor;
private $publicGetter;

public function __construct($value)
{
$this->publicProperty = $value;
$this->publicAccessor = $value;
$this->publicMethodAccessor = $value;
$this->publicGetSetter = $value;
$this->publicAccessorWithDefaultValue = $value;
$this->publicAccessorWithRequiredAndDefaultValue = $value;
$this->publicAccessorWithMoreRequiredParameters = $value;
$this->publicIsAccessor = $value;
$this->publicHasAccessor = $value;
$this->publicGetter = $value;
}

public function setPublicAccessor($value)
{
$this->publicAccessor = $value;
}

public function setPublicAccessorWithDefaultValue($value = null)
{
$this->publicAccessorWithDefaultValue = $value;
}

public function setPublicAccessorWithRequiredAndDefaultValue($value, $optional = null)
{
$this->publicAccessorWithRequiredAndDefaultValue = $value;
}

public function setPublicAccessorWithMoreRequiredParameters($value, $needed)
{
$this->publicAccessorWithMoreRequiredParameters = $value;
}

public function getPublicAccessor()
{
return $this->publicAccessor;
}

public function getPublicAccessorWithDefaultValue()
{
return $this->publicAccessorWithDefaultValue;
}

public function getPublicAccessorWithRequiredAndDefaultValue()
{
return $this->publicAccessorWithRequiredAndDefaultValue;
}

public function getPublicAccessorWithMoreRequiredParameters()
{
return $this->publicAccessorWithMoreRequiredParameters;
}

public function setPublicIsAccessor($value)
{
$this->publicIsAccessor = $value;
}

public function isPublicIsAccessor()
{
return $this->publicIsAccessor;
}

public function setPublicHasAccessor($value)
{
$this->publicHasAccessor = $value;
}

public function hasPublicHasAccessor()
{
return $this->publicHasAccessor;
}

public function publicGetSetter($value = null)
{
if (null !== $value) {
$this->publicGetSetter = $value;
}

return $this->publicGetSetter;
}

public function getPublicMethodMutator()
{
return $this->publicGetSetter;
}

protected function setProtectedAccessor($value)
{
}

protected function getProtectedAccessor()
{
return 'foobar';
}

protected function setProtectedIsAccessor($value)
{
}

protected function isProtectedIsAccessor()
{
return 'foobar';
}

protected function setProtectedHasAccessor($value)
{
}

protected function hasProtectedHasAccessor()
{
return 'foobar';
}

private function setPrivateAccessor($value)
{
}

private function getPrivateAccessor()
{
return 'foobar';
}

private function setPrivateIsAccessor($value)
{
}

private function isPrivateIsAccessor()
{
return 'foobar';
}

private function setPrivateHasAccessor($value)
{
}

private function hasPrivateHasAccessor()
{
return 'foobar';
}

public function getPublicGetter()
{
return $this->publicGetter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\PropertyAccess\Tests\Fixtures\Author;
use Symfony\Component\PropertyAccess\Tests\Fixtures\Magician;
use Symfony\Component\PropertyAccess\Tests\Fixtures\MagicianCall;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
use Symfony\Component\PropertyAccess\Tests\Fixtures\Ticket5775Object;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TypeHinted;

Expand Down Expand Up @@ -422,4 +423,14 @@ public function testSetTypeHint()
$this->propertyAccessor->setValue($object, 'date', $date);
$this->assertSame($date, $object->getDate());
}

public function testArrayNotBeeingOverwritten()
{
$value = array('value1' => 'foo', 'value2' => 'bar');
$object = new TestClass($value);

$this->propertyAccessor->setValue($object, 'publicAccessor[value2]', 'baz');
$this->assertSame('baz', $this->propertyAccessor->getValue($object, 'publicAccessor[value2]'));
$this->assertSame(array('value1' => 'foo', 'value2' => 'baz'), $object->getPublicAccessor());
}
}