diff --git a/lib/Horde/String.php b/lib/Horde/String.php index 84dc527..c778993 100644 --- a/lib/Horde/String.php +++ b/lib/Horde/String.php @@ -70,7 +70,10 @@ public static function convertCharset($input, $from, $to, $force = false) if (is_array($input)) { $tmp = []; foreach ($input as $key => $val) { - $tmp[self::_convertCharset($key, $from, $to)] = self::convertCharset($val, $from, $to, $force); + $convertedKey = is_string($key) + ? self::_convertCharset($key, $from, $to) + : $key; + $tmp[$convertedKey] = self::convertCharset($val, $from, $to, $force); } return $tmp; } diff --git a/src/HordeString.php b/src/HordeString.php index 570f798..f53d57b 100644 --- a/src/HordeString.php +++ b/src/HordeString.php @@ -83,7 +83,10 @@ public static function convertCharset($input, $from, $to, $force = false) if (is_array($input)) { $tmp = []; foreach ($input as $key => $val) { - $tmp[self::_convertCharset($key, $from, $to)] = self::convertCharset($val, $from, $to, $force); + $convertedKey = is_string($key) + ? self::_convertCharset($key, $from, $to) + : $key; + $tmp[$convertedKey] = self::convertCharset($val, $from, $to, $force); } return $tmp; } diff --git a/test/HordeStringTest.php b/test/HordeStringTest.php index 4f287dd..5f8868b 100644 --- a/test/HordeStringTest.php +++ b/test/HordeStringTest.php @@ -816,10 +816,35 @@ public function testConvertCharset() HordeString::convertCharset(123, 'UTF-8', 'ISO-8859-1') ); - // Test array conversion + // Test array conversion with string keys $input = ['key' => 'tëst', 'ümläüt' => 'välüe']; $result = HordeString::convertCharset($input, 'UTF-8', 'ISO-8859-1'); $this->assertIsArray($result); + + // Test array conversion with integer keys (indexed array) + $input = [0 => 'hello', 1 => 'world', 2 => 'test']; + $result = HordeString::convertCharset($input, 'UTF-8', 'ISO-8859-1'); + $this->assertIsArray($result); + $this->assertEquals('hello', $result[0]); + $this->assertEquals('world', $result[1]); + $this->assertEquals('test', $result[2]); + + // Test array conversion with mixed keys + $input = [0 => 'indexed', 'name' => 'value', 1 => 'another']; + $result = HordeString::convertCharset($input, 'UTF-8', 'ISO-8859-1'); + $this->assertIsArray($result); + $this->assertEquals('indexed', $result[0]); + $this->assertEquals('value', $result['name']); + $this->assertEquals('another', $result[1]); + + // Test nested array with integer keys + $input = [0 => 'first', 'nested' => [0 => 'inner', 1 => 'values']]; + $result = HordeString::convertCharset($input, 'UTF-8', 'ISO-8859-1'); + $this->assertIsArray($result); + $this->assertEquals('first', $result[0]); + $this->assertIsArray($result['nested']); + $this->assertEquals('inner', $result['nested'][0]); + $this->assertEquals('values', $result['nested'][1]); } public function testTrimUtf8Bom() @@ -949,4 +974,25 @@ public function testRiposWithInvalidOffset() $result = HordeString::ripos('Some random string', 'some', 50, 'UTF-8'); $this->assertFalse($result); } + + /** + * Test that convertCharset doesn't throw TypeError when array has integer keys. + * + * This test ensures that arrays with integer keys (indexed arrays) are + * handled correctly. Previously, the code called _convertCharset() on + * array keys without checking if they were strings, causing TypeError + * when integer keys were passed to a method expecting string. + */ + public function testConvertCharsetWithIntegerArrayKeysDoesNotThrowTypeError() + { + // This would throw TypeError before the fix due to strict_types + $arr = [0 => 'tëst', 1 => 'wörld', 2 => 'dätä']; + $result = HordeString::convertCharset($arr, 'UTF-8', 'ISO-8859-1'); + + $this->assertIsArray($result); + $this->assertCount(3, $result); + $this->assertArrayHasKey(0, $result); + $this->assertArrayHasKey(1, $result); + $this->assertArrayHasKey(2, $result); + } }