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