From 59e2c2a3858f0614040e74e2a29ab751a22e8936 Mon Sep 17 00:00:00 2001 From: Jorrit Schippers Date: Thu, 18 Jan 2024 10:30:04 +0100 Subject: [PATCH] Support native "mixed" class properties in PHP 8 https://wiki.php.net/rfc/mixed_type_v2 --- src/JsonMapper.php | 7 ++-- tests/MixedType_PHP80_Test.php | 60 ++++++++++++++++++++++++++++ tests/namespacetest/PhpMixedType.php | 10 +++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 tests/MixedType_PHP80_Test.php create mode 100644 tests/namespacetest/PhpMixedType.php diff --git a/src/JsonMapper.php b/src/JsonMapper.php index 41dfd8994..846a4cf8f 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -357,7 +357,7 @@ protected function getFullNamespace($type, $strNs) return $type; } list($first) = explode('[', $type, 2); - if ($first === 'mixed' || $this->isSimpleType($first)) { + if ($this->isSimpleType($first)) { return $type; } @@ -734,7 +734,8 @@ protected function isSimpleType($type) || $type == 'boolean' || $type == 'bool' || $type == 'integer' || $type == 'int' || $type == 'double' || $type == 'float' - || $type == 'array' || $type == 'object'; + || $type == 'array' || $type == 'object' + || $type === 'mixed'; } /** @@ -756,7 +757,7 @@ protected function isObjectOfSameType($type, $value) /** * Checks if the given type is a type that is not nested - * (simple type except array and object) + * (simple type except array, object and mixed) * * @param string $type type name from gettype() * diff --git a/tests/MixedType_PHP80_Test.php b/tests/MixedType_PHP80_Test.php new file mode 100644 index 000000000..ec4d2120c --- /dev/null +++ b/tests/MixedType_PHP80_Test.php @@ -0,0 +1,60 @@ + + * @license OSL-3.0 http://opensource.org/licenses/osl-3.0 + * @link https://github.com/cweiske/jsonmapper + * @requires PHP 8.0 + */ +class MixedType_PHP80_Test extends \PHPUnit\Framework\TestCase +{ + const TEST_DATA_COMPLEX = '{"data": { "id": 123, "name": "Test User" }}'; + const TEST_DATA_SIMPLE = '{"data": 123}'; + + /** + * Sets up test cases loading required classes. + * + * This is in setUp and not at the top of this file to ensure this is only + * executed with PHP 8.0 (due to the `@requires` tag). + */ + protected function setUp(): void + { + require_once 'namespacetest/PhpMixedType.php'; + } + + /** + * Test for PHP 8.0 mixed type containing an object. + */ + public function testStrictTypesMapping_ComplexValue() + { + $jm = new JsonMapper(); + /** @var \namespacetest\PhpMixedType $sn */ + $sn = $jm->map( + json_decode(self::TEST_DATA_COMPLEX), + new \namespacetest\PhpMixedType() + ); + + $this->assertInstanceOf(stdClass::class, $sn->data); + $this->assertEquals(123, $sn->data->id); + $this->assertEquals('Test User', $sn->data->name); + } + + /** + * Test for PHP 8.0 mixed type containing an int. + */ + public function testStrictTypesMapping_SimpleValue() + { + $jm = new JsonMapper(); + /** @var \namespacetest\PhpMixedType $sn */ + $sn = $jm->map( + json_decode(self::TEST_DATA_SIMPLE), + new \namespacetest\PhpMixedType() + ); + + $this->assertEquals(123, $sn->data); + } +} diff --git a/tests/namespacetest/PhpMixedType.php b/tests/namespacetest/PhpMixedType.php new file mode 100644 index 000000000..d11a26e1c --- /dev/null +++ b/tests/namespacetest/PhpMixedType.php @@ -0,0 +1,10 @@ +