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

Refactor SimpleSAML_Utilities::parseAttributes() to SimpleSAML_Utils_Arrays::...

Refactor SimpleSAML_Utilities::parseAttributes() to SimpleSAML_Utils_Arrays:: normalizeAttributesArray().

Schedule SimpleSAML_Utilities:: parseAttributes() for removal.
parent 68d46fd5
No related branches found
No related tags found
No related merge requests found
...@@ -1248,6 +1248,8 @@ class SimpleSAML_Utilities { ...@@ -1248,6 +1248,8 @@ class SimpleSAML_Utilities {
* *
* @param array $attributes The attributes we should parse and validate. * @param array $attributes The attributes we should parse and validate.
* @return array The parsed attributes. * @return array The parsed attributes.
* @deprecated This method will be removed in SSP 2.0. Please use
* SimpleSAML_Utils_Arrays::normalizeAttributesArray() instead.
*/ */
public static function parseAttributes($attributes) { public static function parseAttributes($attributes) {
......
...@@ -9,6 +9,52 @@ ...@@ -9,6 +9,52 @@
class SimpleSAML_Utils_Arrays class SimpleSAML_Utils_Arrays
{ {
/**
* Validate and normalize an array with attributes.
*
* This function takes in an associative array with attributes, and parses and validates
* this array. On success, it will return a normalized array, where each attribute name
* is an index to an array of one or more strings. On failure an exception will be thrown.
* This exception will contain an message describing what is wrong.
*
* @param array $attributes The array containing attributes that we should validate and normalize.
*
* @return array The normalized attributes array.
* @throws SimpleSAML_Error_Exception If input is not an array, array keys are not strings or attribute values are
* not strings.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/
public static function normalizeAttributesArray($attributes)
{
if (!is_array($attributes)) {
throw new SimpleSAML_Error_Exception('Attributes was not an array. Was: '.print_r($attributes, true).'".');
}
$newAttrs = array();
foreach ($attributes as $name => $values) {
if (!is_string($name)) {
throw new SimpleSAML_Error_Exception('Invalid attribute name: "'.print_r($name, true).'".');
}
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $value) {
if (!is_string($value)) {
throw new SimpleSAML_Error_Exception('Invalid attribute value for attribute '.$name.
': "'.print_r($value, true).'".');
}
}
$newAttrs[$name] = $values;
}
return $newAttrs;
}
/** /**
* This function transposes a two-dimensional array, so that $a['k1']['k2'] becomes $a['k2']['k1']. * This function transposes a two-dimensional array, so that $a['k1']['k2'] becomes $a['k2']['k1'].
* *
......
...@@ -7,6 +7,51 @@ ...@@ -7,6 +7,51 @@
class Utils_Arrays extends PHPUnit_Framework_TestCase class Utils_Arrays extends PHPUnit_Framework_TestCase
{ {
/**
* Test the normalizeAttributesArray() function with input not being an array
*
* @expectedException SimpleSAML_Error_Exception
*/
public function testNormalizeAttributesArrayBadInput() {
SimpleSAML_Utils_Arrays::normalizeAttributesArray('string');
}
/**
* Test the normalizeAttributesArray() function with an array with non-string attribute names.
*
* @expectedException SimpleSAML_Error_Exception
*/
public function testNormalizeAttributesArrayBadKeys() {
SimpleSAML_Utils_Arrays::normalizeAttributesArray(array('attr1' => 'value1', 1 => 'value2'));
}
/**
* Test the normalizeAttributesArray() function with an array with non-string attribute values.
*
* @expectedException SimpleSAML_Error_Exception
*/
public function testNormalizeAttributesArrayBadValues() {
SimpleSAML_Utils_Arrays::normalizeAttributesArray(array('attr1' => 'value1', 'attr2' => 0));
}
/**
* Test the normalizeAttributesArray() function.
*/
public function testNormalizeAttributesArray() {
$attributes = array(
'key1' => 'value1',
'key2' => array('value2', 'value3'),
'key3' => 'value1'
);
$expected = array(
'key1' => array('value1'),
'key2' => array('value2', 'value3'),
'key3' => array('value1')
);
$this->assertEquals($expected, SimpleSAML_Utils_Arrays::normalizeAttributesArray($attributes),
'Attribute array normalization failed');
}
/** /**
* Test the transpose() function. * Test the transpose() function.
...@@ -14,9 +59,9 @@ class Utils_Arrays extends PHPUnit_Framework_TestCase ...@@ -14,9 +59,9 @@ class Utils_Arrays extends PHPUnit_Framework_TestCase
public function testTranspose() public function testTranspose()
{ {
// check bad arrays // check bad arrays
$this->assertEquals(false, SimpleSAML_Utils_Arrays::transpose(array('1', '2', '3')), $this->assertFalse(SimpleSAML_Utils_Arrays::transpose(array('1', '2', '3')),
'Invalid two-dimensional array was accepted'); 'Invalid two-dimensional array was accepted');
$this->assertEquals(false, SimpleSAML_Utils_Arrays::transpose(array('1' => 0, '2' => '0', '3' => array(0))), $this->assertFalse(SimpleSAML_Utils_Arrays::transpose(array('1' => 0, '2' => '0', '3' => array(0))),
'Invalid elements on a two-dimensional array were accepted'); 'Invalid elements on a two-dimensional array were accepted');
// check array with numerical keys // check array with numerical keys
......
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