Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
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 |
Collection.php
001 <?php
002 namespace GuzzleHttp;
003
004 /**
005 * Key value pair collection object
006 */
007 class Collection implements
008 \ArrayAccess,
009 \IteratorAggregate,
010 \Countable,
011 ToArrayInterface
012 {
013 use HasDataTrait;
014
015 /**
016 * @param array $data Associative array of data to set
017 */
018 public function __construct(array $data = [])
019 {
020 $this->data = $data;
021 }
022
023 /**
024 * Create a new collection from an array, validate the keys, and add default
025 * values where missing
026 *
027 * @param array $config Configuration values to apply.
028 * @param array $defaults Default parameters
029 * @param array $required Required parameter names
030 *
031 * @return self
032 * @throws \InvalidArgumentException if a parameter is missing
033 */
034 public static function fromConfig(
035 array $config = [],
036 array $defaults = [],
037 array $required = []
038 ) {
039 $data = $config + $defaults;
040
041 if ($missing = array_diff($required, array_keys($data))) {
042 throw new \InvalidArgumentException(
043 'Config is missing the following keys: ' .
044 implode(', ', $missing));
045 }
046
047 return new self($data);
048 }
049
050 /**
051 * Removes all key value pairs
052 */
053 public function clear()
054 {
055 $this->data = [];
056 }
057
058 /**
059 * Get a specific key value.
060 *
061 * @param string $key Key to retrieve.
062 *
063 * @return mixed|null Value of the key or NULL
064 */
065 public function get($key)
066 {
067 return isset($this->data[$key]) ? $this->data[$key] : null;
068 }
069
070 /**
071 * Set a key value pair
072 *
073 * @param string $key Key to set
074 * @param mixed $value Value to set
075 */
076 public function set($key, $value)
077 {
078 $this->data[$key] = $value;
079 }
080
081 /**
082 * Add a value to a key. If a key of the same name has already been added,
083 * the key value will be converted into an array and the new value will be
084 * pushed to the end of the array.
085 *
086 * @param string $key Key to add
087 * @param mixed $value Value to add to the key
088 */
089 public function add($key, $value)
090 {
091 if (!array_key_exists($key, $this->data)) {
092 $this->data[$key] = $value;
093 } elseif (is_array($this->data[$key])) {
094 $this->data[$key][] = $value;
095 } else {
096 $this->data[$key] = array($this->data[$key], $value);
097 }
098 }
099
100 /**
101 * Remove a specific key value pair
102 *
103 * @param string $key A key to remove
104 */
105 public function remove($key)
106 {
107 unset($this->data[$key]);
108 }
109
110 /**
111 * Get all keys in the collection
112 *
113 * @return array
114 */
115 public function getKeys()
116 {
117 return array_keys($this->data);
118 }
119
120 /**
121 * Returns whether or not the specified key is present.
122 *
123 * @param string $key The key for which to check the existence.
124 *
125 * @return bool
126 */
127 public function hasKey($key)
128 {
129 return array_key_exists($key, $this->data);
130 }
131
132 /**
133 * Checks if any keys contains a certain value
134 *
135 * @param string $value Value to search for
136 *
137 * @return mixed Returns the key if the value was found FALSE if the value
138 * was not found.
139 */
140 public function hasValue($value)
141 {
142 return array_search($value, $this->data, true);
143 }
144
145 /**
146 * Replace the data of the object with the value of an array
147 *
148 * @param array $data Associative array of data
149 */
150 public function replace(array $data)
151 {
152 $this->data = $data;
153 }
154
155 /**
156 * Add and merge in a Collection or array of key value pair data.
157 *
158 * @param Collection|array $data Associative array of key value pair data
159 */
160 public function merge($data)
161 {
162 foreach ($data as $key => $value) {
163 $this->add($key, $value);
164 }
165 }
166
167 /**
168 * Overwrite key value pairs in this collection with all of the data from
169 * an array or collection.
170 *
171 * @param array|\Traversable $data Values to override over this config
172 */
173 public function overwriteWith($data)
174 {
175 if (is_array($data)) {
176 $this->data = $data + $this->data;
177 } elseif ($data instanceof Collection) {
178 $this->data = $data->toArray() + $this->data;
179 } else {
180 foreach ($data as $key => $value) {
181 $this->data[$key] = $value;
182 }
183 }
184 }
185
186 /**
187 * Returns a Collection containing all the elements of the collection after
188 * applying the callback function to each one.
189 *
190 * The callable should accept three arguments:
191 * - (string) $key
192 * - (string) $value
193 * - (array) $context
194 *
195 * The callable must return a the altered or unaltered value.
196 *
197 * @param callable $closure Map function to apply
198 * @param array $context Context to pass to the callable
199 *
200 * @return Collection
201 */
202 public function map(callable $closure, array $context = [])
203 {
204 $collection = new static();
205 foreach ($this as $key => $value) {
206 $collection[$key] = $closure($key, $value, $context);
207 }
208
209 return $collection;
210 }
211
212 /**
213 * Iterates over each key value pair in the collection passing them to the
214 * callable. If the callable returns true, the current value from input is
215 * returned into the result Collection.
216 *
217 * The callable must accept two arguments:
218 * - (string) $key
219 * - (string) $value
220 *
221 * @param callable $closure Evaluation function
222 *
223 * @return Collection
224 */
225 public function filter(callable $closure)
226 {
227 $collection = new static();
228 foreach ($this->data as $key => $value) {
229 if ($closure($key, $value)) {
230 $collection[$key] = $value;
231 }
232 }
233
234 return $collection;
235 }
236 }
237