Skip to content
Snippets Groups Projects
Commit 68d46fd5 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Refactor SimpleSAML_Utilities::transposeArray() to SimpleSAML_Utils_Arrays::transpose().

Schedule SimpleSAML_Utilities::transposeArray() for removal.
parent 7582458c
No related branches found
No related tags found
No related merge requests found
...@@ -403,7 +403,7 @@ class SimpleSAML_Memcache { ...@@ -403,7 +403,7 @@ class SimpleSAML_Memcache {
throw new Exception('Failed to get memcache server status.'); 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); $ret = array_merge_recursive($ret, $stats);
} }
......
...@@ -706,6 +706,8 @@ class SimpleSAML_Utilities { ...@@ -706,6 +706,8 @@ class SimpleSAML_Utilities {
* *
* @param $in Input two-dimensional array. * @param $in Input two-dimensional array.
* @return The transposed 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) { public static function transposeArray($in) {
assert('is_array($in)'); assert('is_array($in)');
......
<?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
<?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
...@@ -25,7 +25,7 @@ if(array_key_exists('xmldata', $_POST)) { ...@@ -25,7 +25,7 @@ if(array_key_exists('xmldata', $_POST)) {
} }
/* Transpose from $entities[entityid][type] to $output[type][entityid]. */ /* 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 /* Merge all metadata of each type to a single string which should be
* added to the corresponding file. * added to the corresponding file.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment