-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mapping objects implementing ArrayAccess (not collections)
Only handle objects as array when they implement both ArrayAccess and Traversable. BC break! ---- Originally, JsonMapper handled objects extending ArrayObject as arrays. Extending own collection classes from ArrayObject is not always feasible (issue #175, #175), so a way was sought to rely on interfaces only. Patch #197 (#197) changed the implementation to check for the ArrayAccess interface instead of ArrayObject. This unfortunately breaks objects-that-allow-array-access-but-are-not-traversable-arrays (issue #224, #224), for example when you allow array access to properties stored in some internal variable. The correct solution is to check that the object implements ArrayAcces *and* Traversable - then we can be sure the object is intended to be used with e.g. foreach(). Resolves: #224
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/ValueObject.php'; | ||
|
||
class JsonMapperTest_ArrayAccessObject implements \ArrayAccess | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
public $eins; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $zwei; | ||
|
||
/** | ||
* @var JsonMapperTest_ValueObject | ||
*/ | ||
public $valueObject; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function offsetExists($offset) | ||
{ | ||
return isset($this->$offset); | ||
} | ||
|
||
#[\ReturnTypeWillChange] | ||
public function offsetGet($offset) | ||
{ | ||
return $this->$offset ?? null; | ||
} | ||
|
||
#[\ReturnTypeWillChange] | ||
public function offsetSet($offset, $value) | ||
{ | ||
$this->$offset = $value; | ||
} | ||
|
||
#[\ReturnTypeWillChange] | ||
public function offsetUnset($offset) | ||
{ | ||
unset($this->$offset); | ||
} | ||
} |