diff --git a/src/Registry.php b/src/Registry.php index 0988a78d..8f77a04b 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -204,7 +204,7 @@ public function get($path, $default = null) } if ($this->separator === null || $this->separator === '' || !\strpos($path, $this->separator)) { - return (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') + return (isset($this->data->$path) && $this->data->$path !== '') ? $this->data->$path : $default; } @@ -253,7 +253,7 @@ public function get($path, $default = null) #[\ReturnTypeWillChange] public function getIterator() { - return new \ArrayIterator($this->toArray()); + return new \ArrayIterator((array) $this->data); } /** @@ -592,7 +592,7 @@ public function remove($path) { // Cheap optimisation to direct remove the node if there is no separator if ($this->separator === null || $this->separator === '' || !\strpos($path, $this->separator)) { - $result = (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') + $result = (isset($this->data->$path) && $this->data->$path !== '') ? $this->data->$path : null; diff --git a/tests/RegistryTest.php b/tests/RegistryTest.php index e4514936..e8975562 100644 --- a/tests/RegistryTest.php +++ b/tests/RegistryTest.php @@ -569,6 +569,38 @@ public function testRemoveOffsetAsAnArray() $this->assertFalse(isset($instance['foo.bar'])); } + /** + * @testdox Mixed types are correctly stored and identified + * + * @covers \Joomla\Registry\Registry + */ + public function testMixedTypesInRegistry() + { + $instance = new Registry(); + $instance->set('string', 'hello'); + $instance->set('integer', 42); + $instance->set('array', ['a', 'b', 'c']); + $instance->set('object', new \stdClass()); + $instance->set('registry', new Registry()); + + $expected = [ + 'string' => ['type' => 'string'], + 'integer' => ['type' => 'integer'], + 'array' => ['type' => 'array'], + 'object' => ['type' => 'object', 'class' => \stdClass::class], + 'registry' => ['type' => 'object', 'class' => Registry::class], + ]; + + foreach ($instance as $key => $value) { + $this->assertArrayHasKey($key, $expected, "Unexpected key '$key' found."); + $this->assertSame($expected[$key]['type'], gettype($value), "Type mismatch for key '$key'."); + + if ($expected[$key]['type'] === 'object') { + $this->assertInstanceOf($expected[$key]['class'], $value, "Class mismatch for key '$key'."); + } + } + } + /** * @testdox A value is stored to the Registry *