ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ServerUrlList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Generator;
25
30class ServerUrlList implements \Stringable
31{
33 private array $entries;
34
38 public function __construct(array $entries = [])
39 {
40 $this->entries = array_values($entries);
41 }
42
47 public static function fromString(string $stored): self
48 {
49 $stored = trim($stored);
50 if ($stored === '') {
51 return new self([]);
52 }
53
54 $parts = array_map(
55 static fn(string $s): string => trim($s),
56 explode(',', $stored)
57 );
58
59 $entries = [];
60 foreach ($parts as $part) {
61 if ($part === '') {
62 continue;
63 }
64
65 try {
66 $entries[] = new URI($part);
67 } catch (\Throwable) {
68 $entries[] = $part;
69 }
70 }
71
72 return new self($entries);
73 }
74
76 public function count(): int
77 {
78 return \count($this->entries);
79 }
80
85 public function getConnectionStringAtIndex(int $index): string
86 {
87 if ($index < 0 || !\array_key_exists($index, $this->entries)) {
88 return '';
89 }
90
91 $el = $this->entries[$index];
92
93 return (string) $el;
94 }
95
99 public function toString(): string
100 {
101 $strings = array_map(
102 static fn(URI|string $el): string => $el instanceof URI ? (string) $el : (string) $el,
103 $this->entries
104 );
105
106 return implode(',', $strings);
107 }
108
109 public function __toString(): string
110 {
111 return $this->toString();
112 }
113
119 public function getInvalidParts(): array
120 {
121 $invalid = [];
122 foreach ($this->entries as $el) {
123 if (!($el instanceof URI)) {
124 $invalid[] = $el;
125 }
126 }
127
128 return $invalid;
129 }
130
135 public function rotate(): self
136 {
137 if (\count($this->entries) < 2) {
138 return $this;
139 }
140
141 $rotated = array_merge(
142 \array_slice($this->entries, 1),
143 [$this->entries[0]]
144 );
145
146 return new self($rotated);
147 }
148
153 public function withPrimaryAt(int $index): self
154 {
155 if ($index < 0 || !\array_key_exists($index, $this->entries)) {
156 return $this;
157 }
158
159 $entry = $this->entries[$index];
160 $rest = array_merge(
161 \array_slice($this->entries, 0, $index),
162 \array_slice($this->entries, $index + 1)
163 );
164
165 return new self(array_merge([$entry], $rest));
166 }
167
174 public function validUrls(): Generator
175 {
176 foreach ($this->entries as $index => $entry) {
177 if ($entry instanceof URI) {
178 yield $index => $entry;
179 }
180 }
181 }
182}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Value object representing a list of LDAP server URLs (primary plus fallbacks).
getInvalidParts()
Entries that could not be parsed as URI (for GUI validation messages).
rotate()
New list with first entry moved to the end (for persisted fallback rotation).
toString()
Convert to stored form (comma-separated string for database and form).
static fromString(string $stored)
Create from string representation (comma-separated, as stored in DB or form).
getConnectionStringAtIndex(int $index)
Connection string for ldap_connect() at the given index (0 = primary).
count()
Returns the number of entries (valid and invalid).
validUrls()
Iterate over valid URL entries only (invalid/raw string entries are skipped).
__construct(array $entries=[])
withPrimaryAt(int $index)
New list with the entry at $index moved to primary (index 0).
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61