diff options
author | Jaime Perez Crespo <jaime.perez@uninett.no> | 2015-09-01 13:30:58 +0200 |
---|---|---|
committer | Jaime Perez Crespo <jaime.perez@uninett.no> | 2015-09-01 13:30:58 +0200 |
commit | 60641a82c00244766d189f8ab3f16f5ba7b0ce02 (patch) | |
tree | bd9fe813af0c2cc123c365011886428f34a2fb92 /lib/SimpleSAML/Utils/Attributes.php | |
parent | a184fb3678760dfd82c500e6eda7c705b6717b49 (diff) | |
download | simplesamlphp-60641a82c00244766d189f8ab3f16f5ba7b0ce02.zip simplesamlphp-60641a82c00244766d189f8ab3f16f5ba7b0ce02.tar.gz simplesamlphp-60641a82c00244766d189f8ab3f16f5ba7b0ce02.tar.bz2 |
Add a new \SimpleSAML\Utils\Attributes class.
Diffstat (limited to 'lib/SimpleSAML/Utils/Attributes.php')
-rw-r--r-- | lib/SimpleSAML/Utils/Attributes.php | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/SimpleSAML/Utils/Attributes.php b/lib/SimpleSAML/Utils/Attributes.php new file mode 100644 index 0000000..6f67bac --- /dev/null +++ b/lib/SimpleSAML/Utils/Attributes.php @@ -0,0 +1,57 @@ +<?php +namespace SimpleSAML\Utils; + +/** + * Attribute-related utility methods. + * + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> + * @package SimpleSAML + */ +class Attributes +{ + + /** + * Look for an attribute in a normalized attributes array, failing if it's not there. + * + * @param array $attributes The normalized array containing attributes. + * @param string $expected The name of the attribute we are looking for. + * @param bool $allow_multiple Whether to allow multiple values in the attribute or not. + * + * @return mixed The value of the attribute we are expecting. If the attribute has multiple values and + * $allow_multiple is set to true, the first value will be returned. + * + * @throws \InvalidArgumentException If $attributes is not an array or $expected is not a string. + * @throws \SimpleSAML_Error_Exception If the expected attribute was not found in the attributes array. + */ + public static function getExpectedAttribute($attributes, $expected, $allow_multiple = false) + { + if (!is_array($attributes)) { + throw new \InvalidArgumentException( + 'The attributes array is not an array, it is: '.print_r($attributes, true).'.' + ); + } + + if (!is_string($expected)) { + throw new \InvalidArgumentException( + 'The expected attribute is not a string, it is: '.print_r($expected, true).'.' + ); + } + + if (!array_key_exists($expected, $attributes)) { + throw new \SimpleSAML_Error_Exception("No such attribute '".$expected."' found."); + } + $attribute = $attributes[$expected]; + + if (!is_array($attribute)) { + throw new \SimpleSAML_Error_Exception('The attributes array is not normalized, values should be arrays.'); + } + if (count($attribute) > 1) { + if ($allow_multiple === false) { + throw new \SimpleSAML_Error_Exception( + 'More than one value found for the attribute, multiple values not allowed.' + ); + } + } + return reset($attribute); + } +} |