diff options
author | Olav Morken <olav.morken@uninett.no> | 2008-08-18 11:33:44 +0000 |
---|---|---|
committer | Olav Morken <olav.morken@uninett.no> | 2008-08-18 11:33:44 +0000 |
commit | 7c3c6b07fb515fe453c7de1621b61d5ae97e6951 (patch) | |
tree | 48e2c975fe28ae5a109ecca0555a2613b1969254 | |
parent | d404aa0ba4f1564a506365f3bd0ff0a527251d13 (diff) | |
download | simplesamlphp-7c3c6b07fb515fe453c7de1621b61d5ae97e6951.zip simplesamlphp-7c3c6b07fb515fe453c7de1621b61d5ae97e6951.tar.gz simplesamlphp-7c3c6b07fb515fe453c7de1621b61d5ae97e6951.tar.bz2 |
Utilities: add parseAttributes function.
git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@807 44740490-163a-0410-bde0-09ae8108e29a
-rw-r--r-- | lib/SimpleSAML/Utilities.php | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 313f09e..460abe4 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -1209,6 +1209,47 @@ class SimpleSAML_Utilities { return $res; } + + /** + * Parse and validate 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 attributes we should parse and validate. + * @return array The parsed attributes. + */ + public static function parseAttributes($attributes) { + + if (!is_array($attributes)) { + throw new Exception('Attributes was not an array. Was: ' . var_export($attributes, TRUE)); + } + + $newAttrs = array(); + foreach ($attributes as $name => $values) { + if (!is_string($name)) { + throw new Exception('Invalid attribute name: ' . var_export($name, TRUE)); + } + + if (!is_array($values)) { + $values = array($values); + } + + foreach ($values as $value) { + if (!is_string($value)) { + throw new Exception('Invalid attribute value for attribute ' . $name . + ': ' . var_export($value, TRUE)); + } + } + + $newAttrs[$name] = $values; + } + + return $newAttrs; + } + } ?>
\ No newline at end of file |