From 14ef0b75e548f6c2598fa9f70e3eae3e4a173b48 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Thu, 7 Feb 2008 18:02:18 +0000 Subject: [PATCH] Make StringHash compatible for all versions of PHP 5. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1533 48356398-32a2-884e-a903-53898d9a118a --- extras/ConfigSchema/StringHash.php | 4 ++++ extras/ConfigSchema/StringHashAdapter.php | 16 ++++++++-------- tests/ConfigSchema/StringHashTest.php | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/extras/ConfigSchema/StringHash.php b/extras/ConfigSchema/StringHash.php index 1e105907..d14d0495 100644 --- a/extras/ConfigSchema/StringHash.php +++ b/extras/ConfigSchema/StringHash.php @@ -3,6 +3,10 @@ /** * This is in almost every respect equivalent to an array except * that it keeps track of which keys were accessed. + * + * @warning For the sake of backwards compatibility with early versions + * of PHP 5, you must not use the $hash[$key] syntax; if you do + * our version of offsetGet is never called. */ class ConfigSchema_StringHash extends ArrayObject { diff --git a/extras/ConfigSchema/StringHashAdapter.php b/extras/ConfigSchema/StringHashAdapter.php index ccbe1ff3..2e5ffaec 100644 --- a/extras/ConfigSchema/StringHashAdapter.php +++ b/extras/ConfigSchema/StringHashAdapter.php @@ -26,36 +26,36 @@ class ConfigSchema_StringHashAdapter if (strpos($hash['ID'], '.') === false) { // This will cause problems if we decide to support nested // namespaces, but for now it's ok. - $schema->addNamespace($hash['ID'], $hash['DESCRIPTION']); + $schema->addNamespace($hash->offsetGet('ID'), $hash->offsetGet('DESCRIPTION')); $this->_findUnused($hash); return; } - list($ns, $directive) = explode('.', $hash['ID'], 2); + list($ns, $directive) = explode('.', $hash->offsetGet('ID'), 2); if (isset($hash['TYPE'], $hash['DEFAULT'], $hash['DESCRIPTION'])) { - $type = $hash['TYPE']; - $raw_default = $hash['DEFAULT']; + $type = $hash->offsetGet('TYPE'); + $raw_default = $hash->offsetGet('DEFAULT'); $default = eval("return $raw_default;"); - $description = $hash['DESCRIPTION']; + $description = $hash->offsetGet('DESCRIPTION'); $schema->add($ns, $directive, $default, $type, $description); } if (isset($hash['ALLOWED'])) { - $raw_allowed = $hash['ALLOWED']; + $raw_allowed = $hash->offsetGet('ALLOWED'); $allowed = eval("return array($raw_allowed);"); $schema->addAllowedValues($ns, $directive, $allowed); } // This must be after ALLOWED if (isset($hash['VALUE-ALIASES'])) { - $raw_value_aliases = $hash['VALUE-ALIASES']; + $raw_value_aliases = $hash->offsetGet('VALUE-ALIASES'); $value_aliases = eval("return array($raw_value_aliases);"); $schema->addValueAliases($ns, $directive, $value_aliases); } if (isset($hash['ALIASES'])) { - $raw_aliases = $hash['ALIASES']; + $raw_aliases = $hash->offsetGet('ALIASES'); $aliases = preg_split('/\s*,\s*/', $raw_aliases); foreach ($aliases as $alias) { list($alias_ns, $alias_directive) = explode('.', $alias, 2); diff --git a/tests/ConfigSchema/StringHashTest.php b/tests/ConfigSchema/StringHashTest.php index 138741ff..e49ba65c 100644 --- a/tests/ConfigSchema/StringHashTest.php +++ b/tests/ConfigSchema/StringHashTest.php @@ -9,7 +9,7 @@ class ConfigSchema_StringHashTest extends UnitTestCase 'key2' => 'value2' )); $this->assertIdentical($hash->getAccessed(), array()); - $t = $hash['key']; + $t = $hash->offsetGet('key'); $this->assertIdentical($hash->getAccessed(), array('key' => true)); $hash->resetAccessed(); $this->assertIdentical($hash->getAccessed(), array());