ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Attributes.php
Go to the documentation of this file.
1<?php
2namespace SimpleSAML\Utils;
3
11{
12
26 public static function getExpectedAttribute($attributes, $expected, $allow_multiple = false)
27 {
28 if (!is_array($attributes)) {
29 throw new \InvalidArgumentException(
30 'The attributes array is not an array, it is: '.print_r($attributes, true).'.'
31 );
32 }
33
34 if (!is_string($expected)) {
35 throw new \InvalidArgumentException(
36 'The expected attribute is not a string, it is: '.print_r($expected, true).'.'
37 );
38 }
39
40 if (!array_key_exists($expected, $attributes)) {
41 throw new \SimpleSAML_Error_Exception("No such attribute '".$expected."' found.");
42 }
43 $attribute = $attributes[$expected];
44
45 if (!is_array($attribute)) {
46 throw new \InvalidArgumentException('The attributes array is not normalized, values should be arrays.');
47 }
48
49 if (count($attribute) === 0) {
50 throw new \SimpleSAML_Error_Exception("Empty attribute '".$expected."'.'");
51
52 } elseif (count($attribute) > 1) {
53 if ($allow_multiple === false) {
54 throw new \SimpleSAML_Error_Exception(
55 'More than one value found for the attribute, multiple values not allowed.'
56 );
57 }
58 }
59 return reset($attribute);
60 }
61
62
80 public static function normalizeAttributesArray($attributes)
81 {
82 if (!is_array($attributes)) {
83 throw new \InvalidArgumentException(
84 'The attributes array is not an array, it is: '.print_r($attributes, true).'".'
85 );
86 }
87
88 $newAttrs = array();
89 foreach ($attributes as $name => $values) {
90 if (!is_string($name)) {
91 throw new \InvalidArgumentException('Invalid attribute name: "'.print_r($name, true).'".');
92 }
93
94 $values = Arrays::arrayize($values);
95
96 foreach ($values as $value) {
97 if (!is_string($value)) {
98 throw new \InvalidArgumentException(
99 'Invalid attribute value for attribute '.$name.': "'.print_r($value, true).'".'
100 );
101 }
102 }
103
104 $newAttrs[$name] = $values;
105 }
106
107 return $newAttrs;
108 }
109
110
123 public static function getAttributeNamespace($name, $defaultns)
124 {
125 $slash = strrpos($name, '/');
126 if ($slash !== false) {
127 $defaultns = substr($name, 0, $slash);
128 $name = substr($name, $slash + 1);
129 }
130 return array(htmlspecialchars($defaultns), htmlspecialchars($name));
131 }
132}
An exception for terminatinating execution or to throw for unit testing.
static arrayize($data, $index=0)
Put a non-array variable into an array.
Definition: Arrays.php:24
static getExpectedAttribute($attributes, $expected, $allow_multiple=false)
Look for an attribute in a normalized attributes array, failing if it's not there.
Definition: Attributes.php:26
static normalizeAttributesArray($attributes)
Validate and normalize an array with attributes.
Definition: Attributes.php:80
static getAttributeNamespace($name, $defaultns)
Extract an attribute's namespace, or revert to default.
Definition: Attributes.php:123
if($format !==null) $name
Definition: metadata.php:146
$attributes