Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
NormalizedCollection.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Configurator\Collections;
009
010 use ArrayAccess;
011 use InvalidArgumentException;
012 use RuntimeException;
013
014 class NormalizedCollection extends Collection implements ArrayAccess
015 {
016 /**
017 * @var string Action to take when add() is called with a key that already exists
018 */
019 protected $onDuplicateAction = 'error';
020
021 /**
022 * {@inheritdoc}
023 */
024 public function asConfig()
025 {
026 $config = parent::asConfig();
027 ksort($config);
028
029 return $config;
030 }
031
032 /**
033 * Query and set the action to take when add() is called with a key that already exists
034 *
035 * @param string|null $action If specified: either "error", "ignore" or "replace"
036 * @return string Old action
037 */
038 public function onDuplicate($action = null)
039 {
040 // Save the old action so it can be returned
041 $old = $this->onDuplicateAction;
042
043 if (func_num_args() && $action !== 'error' && $action !== 'ignore' && $action !== 'replace')
044 {
045 throw new InvalidArgumentException("Invalid onDuplicate action '" . $action . "'. Expected: 'error', 'ignore' or 'replace'");
046 }
047
048 $this->onDuplicateAction = $action;
049
050 return $old;
051 }
052
053 //==========================================================================
054 // Overridable methods
055 //==========================================================================
056
057 /**
058 * Return the exception that is thrown when creating an item using a key that already exists
059 *
060 * @param string $key Item's key
061 * @return RuntimeException
062 */
063 protected function getAlreadyExistsException($key)
064 {
065 return new RuntimeException("Item '" . $key . "' already exists");
066 }
067
068 /**
069 * Return the exception that is thrown when accessing an item that does not exist
070 *
071 * @param string $key Item's key
072 * @return RuntimeException
073 */
074 protected function getNotExistException($key)
075 {
076 return new RuntimeException("Item '" . $key . "' does not exist");
077 }
078
079 /**
080 * Normalize an item's key
081 *
082 * This method can be overridden to implement keys normalization or implement constraints
083 *
084 * @param string $key Original key
085 * @return string Normalized key
086 */
087 public function normalizeKey($key)
088 {
089 return $key;
090 }
091
092 /**
093 * Normalize a value for storage
094 *
095 * This method can be overridden to implement value normalization
096 *
097 * @param mixed $value Original value
098 * @return mixed Normalized value
099 */
100 public function normalizeValue($value)
101 {
102 return $value;
103 }
104
105 //==========================================================================
106 // Items access/manipulation
107 //==========================================================================
108
109 /**
110 * Add an item to this collection
111 *
112 * NOTE: relies on exists() to check the key for invalid values and on set() to normalize it
113 *
114 * @param string $key Item's key
115 * @param mixed $value Item's value
116 * @return mixed Normalized value
117 */
118 public function add($key, $value = null)
119 {
120 // Test whether this key is already in use
121 if ($this->exists($key))
122 {
123 // If the action is "ignore" we return the old value, if it's "error" we throw an
124 // exception. Otherwise, we keep going and replace the value
125 if ($this->onDuplicateAction === 'ignore')
126 {
127 return $this->get($key);
128 }
129 elseif ($this->onDuplicateAction === 'error')
130 {
131 throw $this->getAlreadyExistsException($key);
132 }
133 }
134
135 return $this->set($key, $value);
136 }
137
138 /**
139 * Test whether a given value is present in this collection
140 *
141 * @param mixed $value Original value
142 * @return bool Whether the normalized value was found in this collection
143 */
144 public function contains($value)
145 {
146 return in_array($this->normalizeValue($value), $this->items);
147 }
148
149 /**
150 * Delete an item from this collection
151 *
152 * @param string $key Item's key
153 * @return void
154 */
155 public function delete($key)
156 {
157 try
158 {
159 $key = $this->normalizeKey($key);
160
161 unset($this->items[$key]);
162 }
163 catch (InvalidArgumentException $e)
164 {
165 // Do nothing
166 }
167 }
168
169 /**
170 * Test whether an item of given key exists
171 *
172 * @param string $key Item's key
173 * @return bool Whether this key exists in this collection
174 */
175 public function exists($key)
176 {
177 try
178 {
179 $key = $this->normalizeKey($key);
180 }
181 catch (InvalidArgumentException $e)
182 {
183 return false;
184 }
185
186 return array_key_exists($key, $this->items);
187 }
188
189 /**
190 * Return a value from this collection
191 *
192 * @param string $key Item's key
193 * @return mixed Normalized value
194 */
195 public function get($key)
196 {
197 if (!$this->exists($key))
198 {
199 throw $this->getNotExistException($key);
200 }
201
202 $key = $this->normalizeKey($key);
203
204 return $this->items[$key];
205 }
206
207 /**
208 * Find the index of a given value
209 *
210 * Will return the first key associated with the given value, or FALSE if the value is not found
211 *
212 * @param mixed $value Original value
213 * @return mixed Index of the value, or FALSE if not found
214 */
215 public function indexOf($value)
216 {
217 return array_search($this->normalizeValue($value), $this->items);
218 }
219
220 /**
221 * Set and overwrite a value in this collection
222 *
223 * @param string $key Item's key
224 * @param mixed $value Item's value
225 * @return mixed Normalized value
226 */
227 public function set($key, $value)
228 {
229 $key = $this->normalizeKey($key);
230
231 $this->items[$key] = $this->normalizeValue($value);
232
233 return $this->items[$key];
234 }
235
236 //==========================================================================
237 // ArrayAccess stuff
238 //==========================================================================
239
240 /**
241 * @param string|integer $offset
242 * @return bool
243 */
244 public function offsetExists($offset): bool
245 {
246 return $this->exists($offset);
247 }
248
249 /**
250 * @param string|integer $offset
251 * @return mixed
252 */
253 #[\ReturnTypeWillChange]
254 public function offsetGet($offset)
255 {
256 return $this->get($offset);
257 }
258
259 /**
260 * @param string|integer $offset
261 * @param mixed $value
262 * @return void
263 */
264 public function offsetSet($offset, $value): void
265 {
266 $this->set($offset, $value);
267 }
268
269 /**
270 * @param string|integer $offset
271 * @return void
272 */
273 public function offsetUnset($offset): void
274 {
275 $this->delete($offset);
276 }
277 }