From 68d46fd5f7fb9a2dc30c388f8c6243a16e02719c Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Wed, 15 Apr 2015 17:06:10 +0200 Subject: [PATCH] Refactor SimpleSAML_Utilities::transposeArray() to SimpleSAML_Utils_Arrays::transpose(). Schedule SimpleSAML_Utilities::transposeArray() for removal. --- lib/SimpleSAML/Memcache.php | 2 +- lib/SimpleSAML/Utilities.php | 2 + lib/SimpleSAML/Utils/Arrays.php | 42 +++++++++++++++ tests/Utils/Arrays.php | 90 ++++++++++++++++++++++++++++++++ www/admin/metadata-converter.php | 2 +- 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 lib/SimpleSAML/Utils/Arrays.php create mode 100644 tests/Utils/Arrays.php diff --git a/lib/SimpleSAML/Memcache.php b/lib/SimpleSAML/Memcache.php index 33d458457..d420c33c0 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 d209a64b6..6add145a5 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 000000000..0b567ad39 --- /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 000000000..6b4f0fa93 --- /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 f91fed0ea..dc447e3a6 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. -- GitLab