1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Contract\Collection; |
4: | |
5: | use Salient\Contract\Core\Arrayable; |
6: | use Salient\Contract\Core\Jsonable; |
7: | use ArrayAccess; |
8: | use Countable; |
9: | use IteratorAggregate; |
10: | use JsonSerializable; |
11: | use OutOfRangeException; |
12: | |
13: | /** |
14: | * An array-like collection of items |
15: | * |
16: | * @api |
17: | * |
18: | * @template TKey of array-key |
19: | * @template TValue |
20: | * |
21: | * @extends ArrayAccess<TKey,TValue> |
22: | * @extends Arrayable<TKey,TValue|mixed[]> |
23: | * @extends IteratorAggregate<TKey,TValue> |
24: | */ |
25: | interface CollectionInterface extends |
26: | ArrayAccess, |
27: | Arrayable, |
28: | Countable, |
29: | IteratorAggregate, |
30: | JsonSerializable, |
31: | Jsonable |
32: | { |
33: | /** |
34: | * Pass the value of each item to the callback |
35: | */ |
36: | public const CALLBACK_USE_VALUE = 1; |
37: | |
38: | /** |
39: | * Pass the key of each item to the callback |
40: | */ |
41: | public const CALLBACK_USE_KEY = 2; |
42: | |
43: | /** |
44: | * Pass a key-value pair to the callback for each item |
45: | */ |
46: | public const CALLBACK_USE_BOTH = 3; |
47: | |
48: | /** |
49: | * Return the first item that satisfies the callback |
50: | */ |
51: | public const FIND_VALUE = 4; |
52: | |
53: | /** |
54: | * Return the key of the first item that satisfies the callback |
55: | */ |
56: | public const FIND_KEY = 8; |
57: | |
58: | /** |
59: | * @param Arrayable<TKey,TValue>|iterable<TKey,TValue> $items |
60: | */ |
61: | public function __construct($items = []); |
62: | |
63: | /** |
64: | * Get a new collection with no items |
65: | * |
66: | * @return static |
67: | */ |
68: | public static function empty(); |
69: | |
70: | /** |
71: | * Get a copy of the collection |
72: | * |
73: | * @return static |
74: | */ |
75: | public function copy(); |
76: | |
77: | /** |
78: | * Add or replace an item with a given key |
79: | * |
80: | * @param TKey $key |
81: | * @param TValue $value |
82: | * @return static |
83: | */ |
84: | public function set($key, $value); |
85: | |
86: | /** |
87: | * Check if an item with a given key exists |
88: | * |
89: | * @param TKey $key |
90: | */ |
91: | public function has($key): bool; |
92: | |
93: | /** |
94: | * Get the item with the given key |
95: | * |
96: | * @param TKey $key |
97: | * @return TValue |
98: | * @throws OutOfRangeException if there is no such item in the collection. |
99: | */ |
100: | public function get($key); |
101: | |
102: | /** |
103: | * Remove an item with a given key |
104: | * |
105: | * @param TKey $key |
106: | * @return static |
107: | */ |
108: | public function unset($key); |
109: | |
110: | /** |
111: | * Merge the collection with the given items |
112: | * |
113: | * @param Arrayable<TKey,TValue>|iterable<TKey,TValue> $items |
114: | * @return static |
115: | */ |
116: | public function merge($items); |
117: | |
118: | /** |
119: | * Sort items in the collection |
120: | * |
121: | * @return static |
122: | */ |
123: | public function sort(); |
124: | |
125: | /** |
126: | * Reverse the order of items in the collection |
127: | * |
128: | * @return static |
129: | */ |
130: | public function reverse(); |
131: | |
132: | /** |
133: | * Pass each item in the collection to a callback |
134: | * |
135: | * The callback's return values are discarded. |
136: | * |
137: | * @template T of TValue|TKey|array{TKey,TValue} |
138: | * |
139: | * @param callable(T, T|null $next, T|null $prev): mixed $callback |
140: | * @param int-mask-of<CollectionInterface::*> $mode |
141: | * @return $this |
142: | */ |
143: | public function forEach(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE); |
144: | |
145: | /** |
146: | * Pass each item in the collection to a callback and populate a new |
147: | * collection with its return values |
148: | * |
149: | * @template T of TValue|TKey|array{TKey,TValue} |
150: | * @template TReturn |
151: | * |
152: | * @param callable(T, T|null $next, T|null $prev): TReturn $callback |
153: | * @param int-mask-of<CollectionInterface::*> $mode |
154: | * @return static<TKey,TReturn> |
155: | */ |
156: | public function map(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE); |
157: | |
158: | /** |
159: | * Reduce the collection to items that satisfy a callback |
160: | * |
161: | * @template T of TValue|TKey|array{TKey,TValue} |
162: | * |
163: | * @param callable(T, T|null $next, T|null $prev): bool $callback |
164: | * @param int-mask-of<CollectionInterface::*> $mode |
165: | * @return static |
166: | */ |
167: | public function filter(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE); |
168: | |
169: | /** |
170: | * Get the first item that satisfies a callback, or null if there is no such |
171: | * item in the collection |
172: | * |
173: | * @template T of TValue|TKey|array{TKey,TValue} |
174: | * |
175: | * @param callable(T, T|null $next, T|null $prev): bool $callback |
176: | * @param int-mask-of<CollectionInterface::*> $mode |
177: | * @return ($mode is 8|9|10|11 ? TKey : TValue)|null |
178: | */ |
179: | public function find(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE | CollectionInterface::FIND_VALUE); |
180: | |
181: | /** |
182: | * Reduce the collection to items with keys in an array |
183: | * |
184: | * @param TKey[] $keys |
185: | * @return static |
186: | */ |
187: | public function only(array $keys); |
188: | |
189: | /** |
190: | * Reduce the collection to items with keys in an index |
191: | * |
192: | * @param array<TKey,true> $index |
193: | * @return static |
194: | */ |
195: | public function onlyIn(array $index); |
196: | |
197: | /** |
198: | * Reduce the collection to items with keys not in an array |
199: | * |
200: | * @param TKey[] $keys |
201: | * @return static |
202: | */ |
203: | public function except(array $keys); |
204: | |
205: | /** |
206: | * Reduce the collection to items with keys not in an index |
207: | * |
208: | * @param array<TKey,true> $index |
209: | * @return static |
210: | */ |
211: | public function exceptIn(array $index); |
212: | |
213: | /** |
214: | * Extract a slice of the collection |
215: | * |
216: | * @return static |
217: | */ |
218: | public function slice(int $offset, ?int $length = null); |
219: | |
220: | /** |
221: | * Check if a value is in the collection |
222: | * |
223: | * @param TValue $value |
224: | */ |
225: | public function hasValue($value, bool $strict = false): bool; |
226: | |
227: | /** |
228: | * Get the first key at which a value is found, or null if it's not in the |
229: | * collection |
230: | * |
231: | * @param TValue $value |
232: | * @return TKey|null |
233: | */ |
234: | public function keyOf($value, bool $strict = false); |
235: | |
236: | /** |
237: | * Get the first item equal but not necessarily identical to a value, or |
238: | * null if it's not in the collection |
239: | * |
240: | * @param TValue $value |
241: | * @return TValue|null |
242: | */ |
243: | public function firstOf($value); |
244: | |
245: | /** |
246: | * Get all items in the collection |
247: | * |
248: | * @return array<TKey,TValue> |
249: | */ |
250: | public function all(): array; |
251: | |
252: | /** |
253: | * Get the first item, or null if the collection is empty |
254: | * |
255: | * @return TValue|null |
256: | */ |
257: | public function first(); |
258: | |
259: | /** |
260: | * Get the last item, or null if the collection is empty |
261: | * |
262: | * @return TValue|null |
263: | */ |
264: | public function last(); |
265: | |
266: | /** |
267: | * Get the nth item (1-based), or null if there is no such item in the |
268: | * collection |
269: | * |
270: | * If `$n` is negative, the nth item from the end of the collection is |
271: | * returned. |
272: | * |
273: | * @return TValue|null |
274: | */ |
275: | public function nth(int $n); |
276: | |
277: | /** |
278: | * Pop an item off the end of the collection |
279: | * |
280: | * @param TValue|null $last Receives the value removed from the collection, |
281: | * or `null` if the collection is empty. |
282: | * @param-out TValue|null $last |
283: | * @return static |
284: | */ |
285: | public function pop(&$last = null); |
286: | |
287: | /** |
288: | * Shift an item off the beginning of the collection |
289: | * |
290: | * @param TValue|null $first Receives the value removed from the collection, |
291: | * or `null` if the collection is empty. |
292: | * @param-out TValue|null $first |
293: | * @return static |
294: | */ |
295: | public function shift(&$first = null); |
296: | } |
297: |