Skip to content
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

check for ArrayAccess instead of ArrayObject #197

Closed
wants to merge 1 commit into from
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
2 changes: 1 addition & 1 deletion src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function map($json, $object)
$array = $this->createInstance($proptype, false, $jvalue);
}
} else {
if (is_a($type, 'ArrayObject', true)) {
if (is_a($type, 'ArrayAccess', true)) {
$array = $this->createInstance($type, false, $jvalue);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use namespacetest\model\MyArrayObject;

require_once 'JsonMapperTest/Array.php';
require_once 'JsonMapperTest/ArrayAccessCollection.php';
require_once 'JsonMapperTest/Broken.php';
require_once 'JsonMapperTest/Simple.php';
require_once 'JsonMapperTest/Zoo/Animal.php';
Expand Down Expand Up @@ -221,6 +222,22 @@ public function testMapSimpleArrayObject()
$this->assertEquals(1, $sn->pSimpleArrayObject['zwei']);
}

public function testMapSimpleArrayAccess()
{
$jm = new JsonMapper();
$sn = $jm->map(
json_decode(
'{"pArrayAccessCollection":{"eins": 1,"zwei": "two"}}'
),
new JsonMapperTest_Array()
);
$this->assertInstanceOf('ArrayAccess', $sn->pArrayAccessCollection);
$this->assertIsInt($sn->pArrayAccessCollection['eins']);
$this->assertIsString($sn->pArrayAccessCollection['zwei']);
$this->assertEquals(1, $sn->pArrayAccessCollection['eins']);
$this->assertEquals("two", $sn->pArrayAccessCollection['zwei']);
}

public function testInvalidArray()
{
$this->expectException(JsonMapper_Exception::class);
Expand Down
5 changes: 5 additions & 0 deletions tests/JsonMapperTest/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class JsonMapperTest_Array
*/
public $pArrayObjectList;

/**
* @var ArrayAccessCollection
*/
public $pArrayAccessCollection;

/**
* Array of ArrayObject instances
* @var \namespacetest\model\MyArrayObject[]
Expand Down
35 changes: 35 additions & 0 deletions tests/JsonMapperTest/ArrayAccessCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

class ArrayAccessCollection implements \ArrayAccess
{
/**
* @var array
*/
private $data;

#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->data[$offset] ?? null;
}

#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}

#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
}