$attributes * @param array $defaults Grundwerte (display_name, first_name, last_name, email) => Attribut * @return array */ public static function build(IdentityProvider $provider, array $attributes, array $defaults): array { $profile = []; foreach ($defaults as $field => $attribute) { $profile[$field] = self::value($attributes, $attribute); } foreach ($provider->attributeMap() as $target => $attribute) { $value = self::value($attributes, $attribute); if (str_starts_with($target, 'custom.')) { $profile['custom'][substr($target, 7)] = $value; } else { $profile[$target] = $value; } } return $profile; } /** * Wert eines Attributs als Text. Erst exakter Name (auch ohne Groß-/Kleinschreibung, * wie bei LDAP), dann Pfad mit Punkten für verschachtelte Claims. Listen werden mit * Komma verbunden. * * @param array $attributes */ public static function value(array $attributes, string $name): ?string { $value = self::raw($attributes, $name); if (is_array($value)) { $value = implode(', ', array_filter(array_map(fn ($v) => is_scalar($v) ? trim((string) $v) : null, $value), fn ($v) => $v !== null && $v !== '')); } elseif (is_bool($value)) { $value = $value ? 'ja' : 'nein'; } elseif (! is_scalar($value)) { $value = null; } $value = $value === null ? null : trim((string) $value); return $value === '' || ($value !== null && ! mb_check_encoding($value, 'UTF-8')) ? null : $value; } /** * Liste von Werten (z. B. Gruppen). * * @param array $attributes * @return list */ public static function values(array $attributes, string $name): array { $value = self::raw($attributes, $name); $list = is_array($value) ? $value : ($value === null ? [] : [$value]); return array_values(array_filter(array_map(fn ($v) => is_scalar($v) ? trim((string) $v) : '', $list), fn ($v) => $v !== '')); } /** * @param array $attributes */ public static function raw(array $attributes, string $name): mixed { if ($name === '') { return null; } if (array_key_exists($name, $attributes)) { return $attributes[$name]; } foreach ($attributes as $key => $value) { if (is_string($key) && strcasecmp($key, $name) === 0) { return $value; } } if (! str_contains($name, '.')) { return null; } $value = $attributes; foreach (explode('.', $name) as $segment) { if (! is_array($value) || ! array_key_exists($segment, $value)) { return null; } $value = $value[$segment]; } return $value; } }