8000 [Form] PropertyPath readValue fails for custom array objects by uwej711 · Pull Request #4612 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] PropertyPath readValue fails for custom array objects #4612

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor setValue to work with classes implementing ArrayAccess
  • Loading branch information
uwej711 committed Jun 19, 2012
commit 0e73d233eb6daec797fc2eaeb25568d54fb94865
41 changes: 29 additions & 12 deletions src/Symfony/Component/Form/Util/PropertyPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,32 @@ public function getValue($objectOrArray)
*/
public function setValue(&$objectOrArray, $value)
{
$objectOrArray =& $this->readPropertyAt($objectOrArray, $this->length - 2);

if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
throw new UnexpectedTypeException($objectOrArray, 'object or array');
}

$property = $this->elements[$this->length - 1];
$singular = $this->singulars[$this->length - 1];
$isIndex = $this->isIndex[$this->length - 1];
$originalObjectOrArray = $objectOrArray;

for ($i = $this->length - 2; $i >= 0; --$i) {
$objectOrArray = $this->readPropertyAt($objectOrArray, $i);

if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
throw new UnexpectedTypeException($objectOrArray, 'object or array');
}

$property = $this->elements[$i+1];
$singular = $this->singulars[$i+1];
$isIndex = $this->isIndex[$i+1];

$this->writeProperty($objectOrArray, $property, $singular, $isIndex, $value);

$value = $objectOrArray;
$objectOrArray = $originalObjectOrArray;
}

$property = $this->elements[0];
$singular = $this->singulars[0];
$isIndex = $this->isIndex[0];

$this->writeProperty($objectOrArray, $property, $singular, $isIndex, $value);
}
Expand All @@ -329,7 +346,7 @@ public function setValue(&$objectOrArray, $value)
*
* @throws UnexpectedTypeException If a value within the path is neither object nor array.
*/
protected function &readPropertyAt(&$objectOrArray, $index)
protected function readPropertyAt(&$objectOrArray, $index)
{
for ($i = 0; $i <= $index; ++$i) {
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
Expand All @@ -344,7 +361,7 @@ protected function &readPropertyAt(&$objectOrArray, $index)
$property = $this->elements[$i];
$isIndex = $this->isIndex[$i];

$objectOrArray =& $this->readProperty($objectOrArray, $property, $isIndex);
$objectOrArray = $this->readProperty($objectOrArray, $property, $isIndex);
}

return $objectOrArray;
Expand All @@ -363,7 +380,7 @@ protected function &readPropertyAt(&$objectOrArray, $index)
* @throws PropertyAccessDeniedException If the property cannot be accessed due to
* access restrictions (private or protected).
*/
protected function &readProperty(&$objectOrArray, $property, $isIndex)
protected function readProperty(&$objectOrArray, $property, $isIndex)
{
$result = null;

Expand All @@ -373,7 +390,7 @@ protected function &readProperty(&$objectOrArray, $property, $isIndex)
}

if (isset($objectOrArray[$property])) {
$result =& $objectOrArray[$property];
$result = $objectOrArray[$property];
}
} elseif (is_object($objectOrArray)) {
$camelProp = $this->camelize($property);
Expand Down Expand Up @@ -402,16 +419,16 @@ protected function &readProperty(&$objectOrArray, $property, $isIndex)
$result = $objectOrArray->$hasser();
} elseif ($reflClass->hasMethod('__get')) {
// needed to support magic method __get
$result =& $objectOrArray->$property;
$result = $objectOrArray->$property;
} elseif ($reflClass->hasProperty($property)) {
if (!$reflClass->getProperty($property)->isPublic()) {
throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "%s()" or "%s()"?', $property, $reflClass->name, $getter, $isser));
}

$result =& $objectOrArray->$property;
$result = $objectOrArray->$property;
} elseif (property_exists($objectOrArray, $property)) {
// needed to support \stdClass instances
$result =& $objectOrArray->$property;
$result = $objectOrArray->$property;
} else {
throw new InvalidPropertyException(sprintf('Neither property "%s" nor method "%s()" nor method "%s()" exists in class "%s"', $property, $getter, $isser, $reflClass->name));
}
Expand Down
0