Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Merge branch 'strict-object-checking' into fork
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Feb 23, 2024
2 parents 653ad86 + 2002702 commit 083d034
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ public function mapArray($json, $array, $class = null, $parent_key = '')
settype($jvalue, $class);
$array[$key] = $jvalue;
} else {
if ($this->bStrictObjectTypeChecking) {
throw new JsonMapper_Exception(
'JSON property'
. ' "' . ($parent_key ? $parent_key : '?') . '"'
. ' (array key "' . $key . '") must be an object, '
. gettype($jvalue) . ' given'
);
}

$array[$key] = $this->createInstance(
$class, true, $jvalue
);
Expand Down
28 changes: 28 additions & 0 deletions tests/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,34 @@ public function testStrictTypeCheckingObjectError()
);
}

public function testStrictTypeCheckingObjectInArray()
{
$jm = new JsonMapper();
$jm->bStrictObjectTypeChecking = true;
$sn = $jm->mapArray(
json_decode('[{"pStr":"abc"}]'),
[],
JsonMapperTest_PlainObject::class
);

$this->assertIsObject($sn[0]);
$this->assertInstanceOf('JsonMapperTest_PlainObject', $sn[0]);
$this->assertEquals('abc', $sn[0]->pStr);
}

public function testStrictTypeCheckingObjectInArrayError()
{
$this->expectException(JsonMapper_Exception::class);
$this->expectExceptionMessage('JSON property "?" (array key "0") must be an object, string given');
$jm = new JsonMapper();
$jm->bStrictObjectTypeChecking = true;
$jm->mapArray(
json_decode('["abc"]'),
[],
JsonMapperTest_Object::class
);
}

/**
* Test for "@var object|null" with null value
*/
Expand Down

0 comments on commit 083d034

Please sign in to comment.