diff --git a/lib/SimpleSAML/Memcache.php b/lib/SimpleSAML/Memcache.php index 33d458457ba4b65c3ca03e9ae0678d12cd18a9b5..d420c33c00bd2e249b9493499b908ac8678bc228 100644 --- a/lib/SimpleSAML/Memcache.php +++ b/lib/SimpleSAML/Memcache.php @@ -403,7 +403,7 @@ class SimpleSAML_Memcache { throw new Exception('Failed to get memcache server status.'); } - $stats = SimpleSAML_Utilities::transposeArray($stats); + $stats = SimpleSAML_Utils_Arrays::transpose($stats); $ret = array_merge_recursive($ret, $stats); } diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index d209a64b6d7dd12cad455f3d6a558cbe06838c75..6add145a5d89b0b5edf23e0b0f8030c57bf4a240 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -706,6 +706,8 @@ class SimpleSAML_Utilities { * * @param $in Input two-dimensional array. * @return The transposed array. + * + * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML_Utils_Arrays::transpose() instead. */ public static function transposeArray($in) { assert('is_array($in)'); diff --git a/lib/SimpleSAML/Utils/Arrays.php b/lib/SimpleSAML/Utils/Arrays.php new file mode 100644 index 0000000000000000000000000000000000000000..0b567ad39643d212e80fe8c3ca135e45f9655d6f --- /dev/null +++ b/lib/SimpleSAML/Utils/Arrays.php @@ -0,0 +1,42 @@ +<?php + + +/** + * Array-related utility classes. + * + * @package SimpleSAMLphp + */ +class SimpleSAML_Utils_Arrays +{ + + /** + * This function transposes a two-dimensional array, so that $a['k1']['k2'] becomes $a['k2']['k1']. + * + * @param array $array The two-dimensional array to transpose. + * + * @return mixed The transposed array, or false if $array is not a valid two-dimensional array. + * + * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> + */ + public static function transpose($array) + { + if (!is_array($array)) { + return false; + } + + $ret = array(); + foreach ($array as $k1 => $a2) { + if (!is_array($a2)) { + return false; + } + + foreach ($a2 as $k2 => $v) { + if (!array_key_exists($k2, $ret)) { + $ret[$k2] = array(); + } + $ret[$k2][$k1] = $v; + } + } + return $ret; + } +} \ No newline at end of file diff --git a/tests/Utils/Arrays.php b/tests/Utils/Arrays.php new file mode 100644 index 0000000000000000000000000000000000000000..6b4f0fa9345fdb9e2ec0d77f04f0e39e4191a63f --- /dev/null +++ b/tests/Utils/Arrays.php @@ -0,0 +1,90 @@ +<?php + + +/** + * Class Utils_Arrays + */ +class Utils_Arrays extends PHPUnit_Framework_TestCase +{ + + + /** + * Test the transpose() function. + */ + public function testTranspose() + { + // check bad arrays + $this->assertEquals(false, SimpleSAML_Utils_Arrays::transpose(array('1', '2', '3')), + 'Invalid two-dimensional array was accepted'); + $this->assertEquals(false, SimpleSAML_Utils_Arrays::transpose(array('1' => 0, '2' => '0', '3' => array(0))), + 'Invalid elements on a two-dimensional array were accepted'); + + // check array with numerical keys + $array = array( + 'key1' => array( + 'value1' + ), + 'key2' => array( + 'value1', + 'value2' + ) + ); + $transposed = array( + array( + 'key1' => 'value1', + 'key2' => 'value1' + ), + array( + 'key2' => 'value2' + ) + ); + $this->assertEquals($transposed, SimpleSAML_Utils_Arrays::transpose($array), + 'Unexpected result of transpose()'); + + // check array with string keys + $array = array( + 'key1' => array( + 'subkey1' => 'value1' + ), + 'key2' => array( + 'subkey1' => 'value1', + 'subkey2' => 'value2' + ) + ); + $transposed = array( + 'subkey1' => array( + 'key1' => 'value1', + 'key2' => 'value1' + ), + 'subkey2' => array( + 'key2' => 'value2' + ) + ); + $this->assertEquals($transposed, SimpleSAML_Utils_Arrays::transpose($array), + 'Unexpected result of transpose()'); + + // check array with no keys in common between sub arrays + $array = array( + 'key1' => array( + 'subkey1' => 'value1' + ), + 'key2' => array( + 'subkey2' => 'value1', + 'subkey3' => 'value2' + ) + ); + $transposed = array( + 'subkey1' => array( + 'key1' => 'value1', + ), + 'subkey2' => array( + 'key2' => 'value1' + ), + 'subkey3' => array( + 'key2' => 'value2' + ) + ); + $this->assertEquals($transposed, SimpleSAML_Utils_Arrays::transpose($array), + 'Unexpected result of transpose()'); + } +} \ No newline at end of file diff --git a/www/admin/metadata-converter.php b/www/admin/metadata-converter.php index f91fed0eaa27fdf3e8995703b93cf3cba9fced21..dc447e3a63c2fabd8fd565f2ebe60dc50fe68bd2 100644 --- a/www/admin/metadata-converter.php +++ b/www/admin/metadata-converter.php @@ -25,7 +25,7 @@ if(array_key_exists('xmldata', $_POST)) { } /* Transpose from $entities[entityid][type] to $output[type][entityid]. */ - $output = SimpleSAML_Utilities::transposeArray($entities); + $output = SimpleSAML_Utils_Arrays::transpose($entities); /* Merge all metadata of each type to a single string which should be * added to the corresponding file.