From 7c3c6b07fb515fe453c7de1621b61d5ae97e6951 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 18 Aug 2008 11:33:44 +0000
Subject: [PATCH] Utilities: add parseAttributes function.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@807 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Utilities.php | 41 ++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 313f09e4e..460abe4c5 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
-- 
GitLab