ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSamlMappedUserAttributeValueParser.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 private const string ATTR_REGEX = '/^(.*?)(\|(\d+))?$/';
24
28 public function __construct(
30 private array $userData
31 ) {
32 }
33
34 private function getValueIndex(): int
35 {
36 $index = 0;
37
38 $matches = [];
39 preg_match(self::ATTR_REGEX, $this->rule->getExternalAttribute(), $matches);
40
41 if (is_array($matches) && isset($matches[3]) && is_numeric($matches[3])) {
42 $index = (int) $matches[3];
43 }
44
45 return max($index, 0);
46 }
47
48 public function getAttributeKey(): string
49 {
50 $attribute = '';
51
52 $matches = [];
53 preg_match(self::ATTR_REGEX, $this->rule->getExternalAttribute(), $matches);
54
55 if (is_array($matches) && isset($matches[1]) && is_string($matches[1])) {
56 $attribute = $matches[1];
57 }
58
59 return $attribute;
60 }
61
62 public function parse(): string
63 {
64 $attributeKey = $this->getAttributeKey();
65
66 if (!array_key_exists($attributeKey, $this->userData)) {
67 throw new ilSamlException(sprintf(
68 "Configured external attribute of mapping '%s' -> '%s' does not exist in SAML attribute data.",
69 $this->rule->getAttribute(),
70 $this->rule->getExternalAttribute()
71 ));
72 }
73
74 $value = $this->userData[$attributeKey];
75
76 if (is_array($value)) {
77 $valueIndex = $this->getValueIndex();
78
79 if (!array_key_exists($valueIndex, $value)) {
80 throw new ilSamlException(sprintf(
81 "Configured external attribute of mapping '%s' -> '%s' does not exist in SAML attribute data.",
82 $this->rule->getAttribute(),
83 $this->rule->getExternalAttribute()
84 ));
85 }
86
87 $value = $value[$valueIndex];
88 }
89
90 if (!is_scalar($value)) {
91 throw new ilSamlException(sprintf(
92 "Could not parse a scalar value based on the user attribute mapping '%s' -> '%s'.",
93 $this->rule->getAttribute(),
94 $this->rule->getExternalAttribute()
95 ));
96 }
97
98 return (string) $value;
99 }
100}
__construct(private ilExternalAuthUserAttributeMappingRule $rule, private array $userData)