• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aimeos / map / 13672483176

05 Mar 2025 09:16AM UTC coverage: 97.619% (+0.3%) from 97.294%
13672483176

push

github

aimeos
Removed deprecated inString() from TOC

779 of 798 relevant lines covered (97.62%)

49.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.61
/src/Map.php
1
<?php
2

3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @author Taylor Otwell, Aimeos.org developers
6
 */
7

8

9
namespace Aimeos;
10

11

12
/**
13
 * Handling and operating on a list of elements easily
14
 * Inspired by Laravel Collection class, PHP map data structure and Javascript
15
 *
16
 * @template-implements \ArrayAccess<int|string,mixed>
17
 * @template-implements \IteratorAggregate<int|string,mixed>
18
 */
19
class Map implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
20
{
21
        /**
22
         * @var array<string,\Closure>
23
         */
24
        protected static $methods = [];
25

26
        /**
27
         * @var string
28
         */
29
        protected static $delim = '/';
30

31
        /**
32
         * @var array<int|string,mixed>|\Closure|iterable|mixed
33
         */
34
        protected $list;
35

36
        /**
37
         * @var string
38
         */
39
        protected $sep = '/';
40

41

42
        /**
43
         * Creates a new map.
44
         *
45
         * Returns a new map instance containing the list of elements. In case of
46
         * an empty array or null, the map object will contain an empty list.
47
         *
48
         * @param mixed $elements List of elements or single value
49
         */
50
        public function __construct( $elements = [] )
51
        {
52
                $this->sep = self::$delim;
3,240✔
53
                $this->list = $elements;
3,240✔
54
        }
810✔
55

56

57
        /**
58
         * Handles static calls to custom methods for the class.
59
         *
60
         * Calls a custom method added by Map::method() statically. The called method
61
         * has no access to the internal array because no object is available.
62
         *
63
         * Examples:
64
         *  Map::method( 'foo', function( $arg1, $arg2 ) {} );
65
         *  Map::foo( $arg1, $arg2 );
66
         *
67
         * @param string $name Method name
68
         * @param array<mixed> $params List of parameters
69
         * @return mixed Result from called function or new map with results from the element methods
70
         *
71
         * @throws \BadMethodCallException
72
         */
73
        public static function __callStatic( string $name, array $params )
74
        {
75
                if( !isset( static::$methods[$name] ) ) {
16✔
76
                        throw new \BadMethodCallException( sprintf( 'Method %s::%s does not exist.', static::class, $name ) );
8✔
77
                }
78

79
                return call_user_func_array( \Closure::bind( static::$methods[$name], null, static::class ), $params );
8✔
80
        }
81

82

83
        /**
84
         * Handles dynamic calls to custom methods for the class.
85
         *
86
         * Calls a custom method added by Map::method(). The called method
87
         * has access to the internal array by using $this->list().
88
         *
89
         * Examples:
90
         *  Map::method( 'case', function( $case = CASE_LOWER ) {
91
         *      return new static( array_change_key_case( $this->list(), $case ) );
92
         *  } );
93
         *  Map::from( ['a' => 'bar'] )->case( CASE_UPPER );
94
         *
95
         *  $item = new MyClass(); // with method setId() and getCode()
96
         *  Map::from( [$item, $item] )->setId( null )->getCode();
97
         *
98
         * Results:
99
         * The first example will return ['A' => 'bar'].
100
         *
101
         * The second one will call the setId() method of each element in the map and use
102
         * their return values to create a new map. On the new map, the getCode() method
103
         * is called for every element and its return values are also stored in a new map.
104
         * This last map is then returned.
105
         * If this applies to all elements, an empty map is returned. The map keys from the
106
         * original map are preserved in the returned map.
107
         *
108
         * @param string $name Method name
109
         * @param array<mixed> $params List of parameters
110
         * @return mixed|self Result from called function or new map with results from the element methods
111
         */
112
        public function __call( string $name, array $params )
113
        {
114
                if( isset( static::$methods[$name] ) ) {
32✔
115
                        return call_user_func_array( static::$methods[$name]->bindTo( $this, static::class ), $params );
16✔
116
                }
117

118
                $result = [];
16✔
119

120
                foreach( $this->list() as $key => $item )
16✔
121
                {
122
                        if( is_object( $item ) ) {
8✔
123
                                $result[$key] = $item->{$name}( ...$params );
8✔
124
                        }
125
                }
126

127
                return new static( $result );
16✔
128
        }
129

130

131
        /**
132
         * Returns the elements as a plain array.
133
         *
134
         * @return array<int|string,mixed> Plain array
135
         */
136
        public function __toArray() : array
137
        {
138
                return $this->list = $this->array( $this->list );
8✔
139
        }
140

141

142
        /**
143
         * Sets or returns the seperator for paths to values in multi-dimensional arrays or objects.
144
         *
145
         * The static method only changes the separator for new maps created afterwards.
146
         * Already existing maps will continue to use the previous separator. To change
147
         * the separator of an existing map, use the sep() method instead.
148
         *
149
         * Examples:
150
         *  Map::delimiter( '.' );
151
         *  Map::from( ['foo' => ['bar' => 'baz']] )->get( 'foo.bar' );
152
         *
153
         * Results:
154
         *  '/'
155
         *  'baz'
156
         *
157
         * @param string|null $char Separator character, e.g. "." for "key.to.value" instead of "key/to/value"
158
         * @return string Separator used up to now
159
         */
160
        public static function delimiter( ?string $char = null ) : string
161
        {
162
                $old = self::$delim;
8✔
163

164
                if( $char ) {
8✔
165
                        self::$delim = $char;
8✔
166
                }
167

168
                return $old;
8✔
169
        }
170

171

172
        /**
173
         * Creates a new map with the string splitted by the delimiter.
174
         *
175
         * This method creates a lazy Map and the string is split after calling
176
         * another method that operates on the Map contents.
177
         *
178
         * Examples:
179
         *  Map::explode( ',', 'a,b,c' );
180
         *  Map::explode( '<-->', 'a a<-->b b<-->c c' );
181
         *  Map::explode( '', 'string' );
182
         *  Map::explode( '|', 'a|b|c', 2 );
183
         *  Map::explode( '', 'string', 2 );
184
         *  Map::explode( '|', 'a|b|c|d', -2 );
185
         *  Map::explode( '', 'string', -3 );
186
         *
187
         * Results:
188
         *  ['a', 'b', 'c']
189
         *  ['a a', 'b b', 'c c']
190
         *  ['s', 't', 'r', 'i', 'n', 'g']
191
         *  ['a', 'b|c']
192
         *  ['s', 't', 'ring']
193
         *  ['a', 'b']
194
         *  ['s', 't', 'r']
195
         *
196
         * A limit of "0" is treated the same as "1". If limit is negative, the rest of
197
         * the string is dropped and not part of the returned map.
198
         *
199
         * @param string $delimiter Delimiter character, string or empty string
200
         * @param string $string String to split
201
         * @param int $limit Maximum number of element with the last element containing the rest of the string
202
         * @return self<int|string,mixed> New map with splitted parts
203
         */
204
        public static function explode( string $delimiter, string $string, int $limit = PHP_INT_MAX ) : self
205
        {
206
                if( $delimiter !== '' ) {
64✔
207
                        return new static( explode( $delimiter, $string, $limit ) );
32✔
208
                }
209

210
                $limit = $limit ?: 1;
32✔
211
                $parts = mb_str_split( $string );
32✔
212

213
                if( $limit < 1 ) {
32✔
214
                        return new static( array_slice( $parts, 0, $limit ) );
8✔
215
                }
216

217
                if( $limit < count( $parts ) )
24✔
218
                {
219
                        $result = array_slice( $parts, 0, $limit );
8✔
220
                        $result[] = join( '', array_slice( $parts, $limit ) );
8✔
221

222
                        return new static( $result );
8✔
223
                }
224

225
                return new static( $parts );
16✔
226
        }
227

228

229
        /**
230
         * Creates a new map filled with given value.
231
         *
232
         * Exapmles:
233
         *  Map::fill( 3, 'a' );
234
         *  Map::fill( 3, 'a', 2 );
235
         *  Map::fill( 3, 'a', -2 );
236
         *
237
         * Results:
238
         * The first example will return [0 => 'a', 1 => 'a', 2 => 'a']. The second
239
         * example will return [2 => 'a', 3 => 'a', 4 => 'a'] and the last one
240
         * [-2 => 'a', -1 => 'a', 0 => 'a'] (PHP 8) or [-2 => 'a', 0 => 'a', 1 => 'a'] (PHP 7).
241
         *
242
         * @param int $num Number of elements to create
243
         * @param mixed $value Value to fill the map with
244
         * @param int $start Start index for the elements
245
         * @return self<int|string,mixed> New map with filled elements
246
         */
247
        public static function fill( int $num, $value, int $start = 0 ) : self
248
        {
249
                return new static( array_fill( $start, $num, $value ) );
16✔
250
        }
251

252

253
        /**
254
         * Creates a new map instance if the value isn't one already.
255
         *
256
         * Examples:
257
         *  Map::from( [] );
258
         *  Map::from( null );
259
         *  Map::from( 'a' );
260
         *  Map::from( new Map() );
261
         *  Map::from( new ArrayObject() );
262
         *
263
         * Results:
264
         * A new map instance containing the list of elements. In case of an empty
265
         * array or null, the map object will contain an empty list. If a map object
266
         * is passed, it will be returned instead of creating a new instance.
267
         *
268
         * @param mixed $elements List of elements or single element
269
         * @return self<int|string,mixed> New map object
270
         */
271
        public static function from( $elements = [] ) : self
272
        {
273
                if( $elements instanceof self ) {
1,496✔
274
                        return $elements;
24✔
275
                }
276

277
                return new static( $elements );
1,496✔
278
        }
279

280

281
        /**
282
         * Creates a new map instance from a JSON string.
283
         *
284
         * This method creates a lazy Map and the string is decoded after calling
285
         * another method that operates on the Map contents. Thus, the exception in
286
         * case of an error isn't thrown immediately but after calling the next method.
287
         *
288
         * Examples:
289
         *  Map::fromJson( '["a", "b"]' );
290
         *  Map::fromJson( '{"a": "b"}' );
291
         *  Map::fromJson( '""' );
292
         *
293
         * Results:
294
         *  ['a', 'b']
295
         *  ['a' => 'b']
296
         *  ['']
297
         *
298
         * There are several options available for decoding the JSON string:
299
         * {@link https://www.php.net/manual/en/function.json-decode.php}
300
         * The parameter can be a single JSON_* constant or a bitmask of several
301
         * constants combine by bitwise OR (|), e.g.:
302
         *
303
         *  JSON_BIGINT_AS_STRING|JSON_INVALID_UTF8_IGNORE
304
         *
305
         * @param int $options Combination of JSON_* constants
306
         * @return self<int|string,mixed> New map from decoded JSON string
307
         * @throws \RuntimeException If the passed JSON string is invalid
308
         */
309
        public static function fromJson( string $json, int $options = JSON_BIGINT_AS_STRING ) : self
310
        {
311
                if( ( $result = json_decode( $json, true, 512, $options ) ) !== null ) {
32✔
312
                        return new static( $result );
24✔
313
                }
314

315
                throw new \RuntimeException( 'Not a valid JSON string: ' . $json );
8✔
316
        }
317

318

319
        /**
320
         * Registers a custom method or returns the existing one.
321
         *
322
         * The registed method has access to the class properties if called non-static.
323
         *
324
         * Examples:
325
         *  Map::method( 'foo', function( $arg1, $arg2 ) {
326
         *      return $this->list();
327
         *  } );
328
         *
329
         * Dynamic calls have access to the class properties:
330
         *  Map::from( ['bar'] )->foo( $arg1, $arg2 );
331
         *
332
         * Static calls yield an error because $this->elements isn't available:
333
         *  Map::foo( $arg1, $arg2 );
334
         *
335
         * @param string $method Method name
336
         * @param \Closure|null $fcn Anonymous function or NULL to return the closure if available
337
         * @return \Closure|null Registered anonymous function or NULL if none has been registered
338
         */
339
        public static function method( string $method, ?\Closure $fcn = null ) : ?\Closure
340
        {
341
                if( $fcn ) {
24✔
342
                        self::$methods[$method] = $fcn;
24✔
343
                }
344

345
                return self::$methods[$method] ?? null;
24✔
346
        }
347

348

349
        /**
350
         * Creates a new map by invoking the closure the given number of times.
351
         *
352
         * This method creates a lazy Map and the entries are generated after calling
353
         * another method that operates on the Map contents. Thus, the passed callback
354
         * is not called immediately!
355
         *
356
         * Examples:
357
         *  Map::times( 3, function( $num ) {
358
         *    return $num * 10;
359
         *  } );
360
         *  Map::times( 3, function( $num, &$key ) {
361
         *    $key = $num * 2;
362
         *    return $num * 5;
363
         *  } );
364
         *  Map::times( 2, function( $num ) {
365
         *    return new \stdClass();
366
         *  } );
367
         *
368
         * Results:
369
         *  [0 => 0, 1 => 10, 2 => 20]
370
         *  [0 => 0, 2 => 5, 4 => 10]
371
         *  [0 => new \stdClass(), 1 => new \stdClass()]
372
         *
373
         * @param int $num Number of times the function is called
374
         * @param \Closure $callback Function with (value, key) parameters and returns new value
375
         * @return self<int|string,mixed> New map with the generated elements
376
         */
377
        public static function times( int $num, \Closure $callback ) : self
378
        {
379
                $list = [];
24✔
380

381
                for( $i = 0; $i < $num; $i++ ) {
24✔
382
                        $key = $i;
24✔
383
                        $list[$key] = $callback( $i, $key );
24✔
384
                }
385

386
                return new static( $list );
24✔
387
        }
388

389

390
        /**
391
         * Returns the elements after the given one.
392
         *
393
         * Examples:
394
         *  Map::from( ['a' => 1, 'b' => 0] )->after( 1 );
395
         *  Map::from( [0 => 'b', 1 => 'a'] )->after( 'b' );
396
         *  Map::from( [0 => 'b', 1 => 'a'] )->after( 'c' );
397
         *  Map::from( ['a', 'c', 'b'] )->after( function( $item, $key ) {
398
         *      return $item >= 'c';
399
         *  } );
400
         *
401
         * Results:
402
         *  ['b' => 0]
403
         *  [1 => 'a']
404
         *  []
405
         *  [2 => 'b']
406
         *
407
         * The keys are preserved using this method.
408
         *
409
         * @param \Closure|int|string $value Value or function with (item, key) parameters
410
         * @return self<int|string,mixed> New map with the elements after the given one
411
         */
412
        public function after( $value ) : self
413
        {
414
                if( ( $pos = $this->pos( $value ) ) === null ) {
32✔
415
                        return new static();
8✔
416
                }
417

418
                return new static( array_slice( $this->list(), $pos + 1, null, true ) );
24✔
419
        }
420

421

422
        /**
423
         * Returns the elements as a plain array.
424
         *
425
         * @return array<int|string,mixed> Plain array
426
         */
427
        public function all() : array
428
        {
429
                return $this->list = $this->array( $this->list );
×
430
        }
431

432

433
        /**
434
         * Tests if at least one element satisfies the callback function.
435
         *
436
         * Examples:
437
         *  Map::from( ['a', 'b'] )->any( function( $item, $key ) {
438
         *    return $item === 'a';
439
         *  } );
440
         *  Map::from( ['a', 'b'] )->any( function( $item, $key ) {
441
         *    return !is_string( $item );
442
         *  } );
443
         *
444
         * Results:
445
         * The first example will return TRUE while the last one will return FALSE
446
         *
447
         * @param \Closure $callback Anonymous function with (item, key) parameter
448
         * @return bool TRUE if at least one element satisfies the callback function, FALSE if not
449
         */
450
        public function any( \Closure $callback ) : bool
451
        {
452
                if( function_exists( 'array_any' ) ) {
8✔
453
                        return array_any( $this->list(), $callback );
×
454
                }
455

456
                foreach( $this->list() as $key => $item )
8✔
457
                {
458
                        if( $callback( $item, $key ) ) {
8✔
459
                                return true;
8✔
460
                        }
461
                }
462

463
                return false;
8✔
464
        }
465

466

467
        /**
468
         * Sorts all elements in reverse order and maintains the key association.
469
         *
470
         * Examples:
471
         *  Map::from( ['b' => 0, 'a' => 1] )->arsort();
472
         *  Map::from( ['a', 'b'] )->arsort();
473
         *  Map::from( [0 => 'C', 1 => 'b'] )->arsort();
474
         *  Map::from( [0 => 'C', 1 => 'b'] )->arsort( SORT_STRING|SORT_FLAG_CASE );
475
         *
476
         * Results:
477
         *  ['a' => 1, 'b' => 0]
478
         *  ['b', 'a']
479
         *  [1 => 'b', 0 => 'C']
480
         *  [0 => 'C', 1 => 'b'] // because 'C' -> 'c' and 'c' > 'b'
481
         *
482
         * The parameter modifies how the values are compared. Possible parameter values are:
483
         * - SORT_REGULAR : compare elements normally (don't change types)
484
         * - SORT_NUMERIC : compare elements numerically
485
         * - SORT_STRING : compare elements as strings
486
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
487
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
488
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
489
         *
490
         * The keys are preserved using this method and no new map is created.
491
         *
492
         * @param int $options Sort options for arsort()
493
         * @return self<int|string,mixed> Updated map for fluid interface
494
         */
495
        public function arsort( int $options = SORT_REGULAR ) : self
496
        {
497
                arsort( $this->list(), $options );
32✔
498
                return $this;
32✔
499
        }
500

501

502
        /**
503
         * Sorts a copy of all elements in reverse order and maintains the key association.
504
         *
505
         * Examples:
506
         *  Map::from( ['b' => 0, 'a' => 1] )->arsorted();
507
         *  Map::from( ['a', 'b'] )->arsorted();
508
         *  Map::from( [0 => 'C', 1 => 'b'] )->arsorted();
509
         *  Map::from( [0 => 'C', 1 => 'b'] )->arsorted( SORT_STRING|SORT_FLAG_CASE );
510
         *
511
         * Results:
512
         *  ['a' => 1, 'b' => 0]
513
         *  ['b', 'a']
514
         *  [1 => 'b', 0 => 'C']
515
         *  [0 => 'C', 1 => 'b'] // because 'C' -> 'c' and 'c' > 'b'
516
         *
517
         * The parameter modifies how the values are compared. Possible parameter values are:
518
         * - SORT_REGULAR : compare elements normally (don't change types)
519
         * - SORT_NUMERIC : compare elements numerically
520
         * - SORT_STRING : compare elements as strings
521
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
522
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
523
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
524
         *
525
         * The keys are preserved using this method and a new map is created.
526
         *
527
         * @param int $options Sort options for arsort()
528
         * @return self<int|string,mixed> Updated map for fluid interface
529
         */
530
        public function arsorted( int $options = SORT_REGULAR ) : self
531
        {
532
                return ( clone $this )->arsort( $options );
8✔
533
        }
534

535

536
        /**
537
         * Sorts all elements and maintains the key association.
538
         *
539
         * Examples:
540
         *  Map::from( ['a' => 1, 'b' => 0] )->asort();
541
         *  Map::from( [0 => 'b', 1 => 'a'] )->asort();
542
         *  Map::from( [0 => 'C', 1 => 'b'] )->asort();
543
         *  Map::from( [0 => 'C', 1 => 'b'] )->arsort( SORT_STRING|SORT_FLAG_CASE );
544
         *
545
         * Results:
546
         *  ['b' => 0, 'a' => 1]
547
         *  [1 => 'a', 0 => 'b']
548
         *  [0 => 'C', 1 => 'b'] // because 'C' < 'b'
549
         *  [1 => 'b', 0 => 'C'] // because 'C' -> 'c' and 'c' > 'b'
550
         *
551
         * The parameter modifies how the values are compared. Possible parameter values are:
552
         * - SORT_REGULAR : compare elements normally (don't change types)
553
         * - SORT_NUMERIC : compare elements numerically
554
         * - SORT_STRING : compare elements as strings
555
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
556
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
557
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
558
         *
559
         * The keys are preserved using this method and no new map is created.
560
         *
561
         * @param int $options Sort options for asort()
562
         * @return self<int|string,mixed> Updated map for fluid interface
563
         */
564
        public function asort( int $options = SORT_REGULAR ) : self
565
        {
566
                asort( $this->list(), $options );
32✔
567
                return $this;
32✔
568
        }
569

570

571
        /**
572
         * Sorts a copy of all elements and maintains the key association.
573
         *
574
         * Examples:
575
         *  Map::from( ['a' => 1, 'b' => 0] )->asorted();
576
         *  Map::from( [0 => 'b', 1 => 'a'] )->asorted();
577
         *  Map::from( [0 => 'C', 1 => 'b'] )->asorted();
578
         *  Map::from( [0 => 'C', 1 => 'b'] )->asorted( SORT_STRING|SORT_FLAG_CASE );
579
         *
580
         * Results:
581
         *  ['b' => 0, 'a' => 1]
582
         *  [1 => 'a', 0 => 'b']
583
         *  [0 => 'C', 1 => 'b'] // because 'C' < 'b'
584
         *  [1 => 'b', 0 => 'C'] // because 'C' -> 'c' and 'c' > 'b'
585
         *
586
         * The parameter modifies how the values are compared. Possible parameter values are:
587
         * - SORT_REGULAR : compare elements normally (don't change types)
588
         * - SORT_NUMERIC : compare elements numerically
589
         * - SORT_STRING : compare elements as strings
590
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
591
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
592
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
593
         *
594
         * The keys are preserved using this method and a new map is created.
595
         *
596
         * @param int $options Sort options for asort()
597
         * @return self<int|string,mixed> Updated map for fluid interface
598
         */
599
        public function asorted( int $options = SORT_REGULAR ) : self
600
        {
601
                return ( clone $this )->asort( $options );
8✔
602
        }
603

604

605
        /**
606
         * Returns the value at the given position.
607
         *
608
         * Examples:
609
         *  Map::from( [1, 3, 5] )->at( 0 );
610
         *  Map::from( [1, 3, 5] )->at( 1 );
611
         *  Map::from( [1, 3, 5] )->at( -1 );
612
         *  Map::from( [1, 3, 5] )->at( 3 );
613
         *
614
         * Results:
615
         * The first line will return "1", the second one "3", the third one "5" and
616
         * the last one NULL.
617
         *
618
         * The position starts from zero and a position of "0" returns the first element
619
         * of the map, "1" the second and so on. If the position is negative, the
620
         * sequence will start from the end of the map.
621
         *
622
         * @param int $pos Position of the value in the map
623
         * @return mixed|null Value at the given position or NULL if no value is available
624
         */
625
        public function at( int $pos )
626
        {
627
                $pair = array_slice( $this->list(), $pos, 1 );
8✔
628
                return !empty( $pair ) ? current( $pair ) : null;
8✔
629
        }
630

631

632
        /**
633
         * Returns the average of all integer and float values in the map.
634
         *
635
         * Examples:
636
         *  Map::from( [1, 3, 5] )->avg();
637
         *  Map::from( [1, null, 5] )->avg();
638
         *  Map::from( [1, 'sum', 5] )->avg();
639
         *  Map::from( [['p' => 30], ['p' => 50], ['p' => 10]] )->avg( 'p' );
640
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->avg( 'i/p' );
641
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->avg( fn( $val, $key ) => $val['i']['p'] ?? null );
642
         *  Map::from( [['p' => 30], ['p' => 50], ['p' => 10]] )->avg( fn( $val, $key ) => $key < 1 ? $val : null );
643
         *
644
         * Results:
645
         * The first and second line will return "3", the third one "2", the forth
646
         * one "30", the fifth and sixth one "40" and the last one "30".
647
         *
648
         * Non-numeric values will be removed before calculation.
649
         *
650
         * NULL values are treated as 0, non-numeric values will generate an error.
651
         *
652
         * This does also work for multi-dimensional arrays by passing the keys
653
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
654
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
655
         * public properties of objects or objects implementing __isset() and __get() methods.
656
         *
657
         * @param Closure|string|null $col Closure, key or path to the values in the nested array or object to compute the average for
658
         * @return float Average of all elements or 0 if there are no elements in the map
659
         */
660
        public function avg( $col = null ) : float
661
        {
662
                $list = $this->list();
24✔
663
                $vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list, 'is_numeric' );
24✔
664

665
                return !empty( $vals ) ? array_sum( $vals ) / count( $vals ) : 0;
24✔
666
        }
667

668

669
        /**
670
         * Returns the elements before the given one.
671
         *
672
         * Examples:
673
         *  Map::from( ['a' => 1, 'b' => 0] )->before( 0 );
674
         *  Map::from( [0 => 'b', 1 => 'a'] )->before( 'a' );
675
         *  Map::from( [0 => 'b', 1 => 'a'] )->before( 'b' );
676
         *  Map::from( ['a', 'c', 'b'] )->before( function( $item, $key ) {
677
         *      return $key >= 1;
678
         *  } );
679
         *
680
         * Results:
681
         *  ['a' => 1]
682
         *  [0 => 'b']
683
         *  []
684
         *  [0 => 'a']
685
         *
686
         * The keys are preserved using this method.
687
         *
688
         * @param \Closure|int|string $value Value or function with (item, key) parameters
689
         * @return self<int|string,mixed> New map with the elements before the given one
690
         */
691
        public function before( $value ) : self
692
        {
693
                return new static( array_slice( $this->list(), 0, $this->pos( $value ), true ) );
32✔
694
        }
695

696

697
        /**
698
         * Returns an element by key and casts it to boolean if possible.
699
         *
700
         * Examples:
701
         *  Map::from( ['a' => true] )->bool( 'a' );
702
         *  Map::from( ['a' => '1'] )->bool( 'a' );
703
         *  Map::from( ['a' => 1.1] )->bool( 'a' );
704
         *  Map::from( ['a' => '10'] )->bool( 'a' );
705
         *  Map::from( ['a' => 'abc'] )->bool( 'a' );
706
         *  Map::from( ['a' => ['b' => ['c' => true]]] )->bool( 'a/b/c' );
707
         *  Map::from( [] )->bool( 'c', function() { return rand( 1, 2 ); } );
708
         *  Map::from( [] )->bool( 'a', true );
709
         *
710
         *  Map::from( [] )->bool( 'b' );
711
         *  Map::from( ['b' => ''] )->bool( 'b' );
712
         *  Map::from( ['b' => null] )->bool( 'b' );
713
         *  Map::from( ['b' => [true]] )->bool( 'b' );
714
         *  Map::from( ['b' => resource] )->bool( 'b' );
715
         *  Map::from( ['b' => new \stdClass] )->bool( 'b' );
716
         *
717
         *  Map::from( [] )->bool( 'c', new \Exception( 'error' ) );
718
         *
719
         * Results:
720
         * The first eight examples will return TRUE while the 9th to 14th example
721
         * returns FALSE. The last example will throw an exception.
722
         *
723
         * This does also work for multi-dimensional arrays by passing the keys
724
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
725
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
726
         * public properties of objects or objects implementing __isset() and __get() methods.
727
         *
728
         * @param int|string $key Key or path to the requested item
729
         * @param mixed $default Default value if key isn't found (will be casted to bool)
730
         * @return bool Value from map or default value
731
         */
732
        public function bool( $key, $default = false ) : bool
733
        {
734
                return (bool) ( is_scalar( $val = $this->get( $key, $default ) ) ? $val : $default );
24✔
735
        }
736

737

738
        /**
739
         * Calls the given method on all items and returns the result.
740
         *
741
         * This method can call methods on the map entries that are also implemented
742
         * by the map object itself and are therefore not reachable when using the
743
         * magic __call() method.
744
         *
745
         * Examples:
746
         *  $item = new MyClass(); // implements methods get() and toArray()
747
         *  Map::from( [$item, $item] )->call( 'get', ['myprop'] );
748
         *  Map::from( [$item, $item] )->call( 'toArray' );
749
         *
750
         * Results:
751
         * The first example will return ['...', '...'] while the second one returns [[...], [...]].
752
         *
753
         * If some entries are not objects, they will be skipped. The map keys from the
754
         * original map are preserved in the returned map.
755
         *
756
         * @param string $name Method name
757
         * @param array<mixed> $params List of parameters
758
         * @return self<int|string,mixed> New map with results from all elements
759
         */
760
        public function call( string $name, array $params = [] ) : self
761
        {
762
                $result = [];
8✔
763

764
                foreach( $this->list() as $key => $item )
8✔
765
                {
766
                        if( is_object( $item ) ) {
8✔
767
                                $result[$key] = $item->{$name}( ...$params );
8✔
768
                        }
769
                }
770

771
                return new static( $result );
8✔
772
        }
773

774

775
        /**
776
         * Casts all entries to the passed type.
777
         *
778
         * Examples:
779
         *  Map::from( [true, 1, 1.0, 'yes'] )->cast();
780
         *  Map::from( [true, 1, 1.0, 'yes'] )->cast( 'bool' );
781
         *  Map::from( [true, 1, 1.0, 'yes'] )->cast( 'int' );
782
         *  Map::from( [true, 1, 1.0, 'yes'] )->cast( 'float' );
783
         *  Map::from( [new stdClass, new stdClass] )->cast( 'array' );
784
         *  Map::from( [[], []] )->cast( 'object' );
785
         *
786
         * Results:
787
         * The examples will return (in this order):
788
         * ['1', '1', '1.0', 'yes']
789
         * [true, true, true, true]
790
         * [1, 1, 1, 0]
791
         * [1.0, 1.0, 1.0, 0.0]
792
         * [[], []]
793
         * [new stdClass, new stdClass]
794
         *
795
         * Casting arrays and objects to scalar values won't return anything useful!
796
         *
797
         * @param string $type Type to cast the values to ("string", "bool", "int", "float", "array", "object")
798
         * @return self<int|string,mixed> Updated map with casted elements
799
         */
800
        public function cast( string $type = 'string' ) : self
801
        {
802
                foreach( $this->list() as &$item )
8✔
803
                {
804
                        switch( $type )
1✔
805
                        {
806
                                case 'bool': $item = (bool) $item; break;
8✔
807
                                case 'int': $item = (int) $item; break;
8✔
808
                                case 'float': $item = (float) $item; break;
8✔
809
                                case 'string': $item = (string) $item; break;
8✔
810
                                case 'array': $item = (array) $item; break;
8✔
811
                                case 'object': $item = (object) $item; break;
8✔
812
                        }
813
                }
814

815
                return $this;
8✔
816
        }
817

818

819
        /**
820
         * Chunks the map into arrays with the given number of elements.
821
         *
822
         * Examples:
823
         *  Map::from( [0, 1, 2, 3, 4] )->chunk( 3 );
824
         *  Map::from( ['a' => 0, 'b' => 1, 'c' => 2] )->chunk( 2 );
825
         *
826
         * Results:
827
         *  [[0, 1, 2], [3, 4]]
828
         *  [['a' => 0, 'b' => 1], ['c' => 2]]
829
         *
830
         * The last chunk may contain less elements than the given number.
831
         *
832
         * The sub-arrays of the returned map are plain PHP arrays. If you need Map
833
         * objects, then wrap them with Map::from() when you iterate over the map.
834
         *
835
         * @param int $size Maximum size of the sub-arrays
836
         * @param bool $preserve Preserve keys in new map
837
         * @return self<int|string,mixed> New map with elements chunked in sub-arrays
838
         * @throws \InvalidArgumentException If size is smaller than 1
839
         */
840
        public function chunk( int $size, bool $preserve = false ) : self
841
        {
842
                if( $size < 1 ) {
24✔
843
                        throw new \InvalidArgumentException( 'Chunk size must be greater or equal than 1' );
8✔
844
                }
845

846
                return new static( array_chunk( $this->list(), $size, $preserve ) );
16✔
847
        }
848

849

850
        /**
851
         * Removes all elements from the current map.
852
         *
853
         * @return self<int|string,mixed> Updated map for fluid interface
854
         */
855
        public function clear() : self
856
        {
857
                $this->list = [];
32✔
858
                return $this;
32✔
859
        }
860

861

862
        /**
863
         * Clones the map and all objects within.
864
         *
865
         * Examples:
866
         *  Map::from( [new \stdClass, new \stdClass] )->clone();
867
         *
868
         * Results:
869
         *   [new \stdClass, new \stdClass]
870
         *
871
         * The objects within the Map are NOT the same as before but new cloned objects.
872
         * This is different to copy(), which doesn't clone the objects within.
873
         *
874
         * The keys are preserved using this method.
875
         *
876
         * @return self<int|string,mixed> New map with cloned objects
877
         */
878
        public function clone() : self
879
        {
880
                $list = [];
8✔
881

882
                foreach( $this->list() as $key => $item ) {
8✔
883
                        $list[$key] = is_object( $item ) ? clone $item : $item;
8✔
884
                }
885

886
                return new static( $list );
8✔
887
        }
888

889

890
        /**
891
         * Returns the values of a single column/property from an array of arrays or objects in a new map.
892
         *
893
         * Examples:
894
         *  Map::from( [['id' => 'i1', 'val' => 'v1'], ['id' => 'i2', 'val' => 'v2']] )->col( 'val' );
895
         *  Map::from( [['id' => 'i1', 'val' => 'v1'], ['id' => 'i2', 'val' => 'v2']] )->col( 'val', 'id' );
896
         *  Map::from( [['id' => 'i1', 'val' => 'v1'], ['id' => 'i2', 'val' => 'v2']] )->col( null, 'id' );
897
         *  Map::from( [['id' => 'ix', 'val' => 'v1'], ['id' => 'ix', 'val' => 'v2']] )->col( null, 'id' );
898
         *  Map::from( [['foo' => ['bar' => 'one', 'baz' => 'two']]] )->col( 'foo/baz', 'foo/bar' );
899
         *  Map::from( [['foo' => ['bar' => 'one']]] )->col( 'foo/baz', 'foo/bar' );
900
         *  Map::from( [['foo' => ['baz' => 'two']]] )->col( 'foo/baz', 'foo/bar' );
901
         *
902
         * Results:
903
         *  ['v1', 'v2']
904
         *  ['i1' => 'v1', 'i2' => 'v2']
905
         *  ['i1' => ['id' => 'i1', 'val' => 'v1'], 'i2' => ['id' => 'i2', 'val' => 'v2']]
906
         *  ['ix' => ['id' => 'ix', 'val' => 'v2']]
907
         *  ['one' => 'two']
908
         *  ['one' => null]
909
         *  ['two']
910
         *
911
         * If $indexcol is omitted, it's value is NULL or not set, the result will be indexed from 0-n.
912
         * Items with the same value for $indexcol will overwrite previous items and only the last
913
         * one will be part of the resulting map.
914
         *
915
         * This does also work to map values from multi-dimensional arrays by passing the keys
916
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
917
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
918
         * public properties of objects or objects implementing __isset() and __get() methods.
919
         *
920
         * @param string|null $valuecol Name or path of the value property
921
         * @param string|null $indexcol Name or path of the index property
922
         * @return self<int|string,mixed> New map with mapped entries
923
         */
924
        public function col( ?string $valuecol = null, ?string $indexcol = null ) : self
925
        {
926
                $vparts = explode( $this->sep, (string) $valuecol );
72✔
927
                $iparts = explode( $this->sep, (string) $indexcol );
72✔
928

929
                if( ( $valuecol === null || count( $vparts ) === 1 )
72✔
930
                        && ( $indexcol === null || count( $iparts ) === 1 )
72✔
931
                ) {
932
                        return new static( array_column( $this->list(), $valuecol, $indexcol ) );
48✔
933
                }
934

935
                $list = [];
24✔
936

937
                foreach( $this->list() as $key => $item )
24✔
938
                {
939
                        $v = $valuecol ? $this->val( $item, $vparts ) : $item;
24✔
940

941
                        if( $indexcol && ( $k = (string) $this->val( $item, $iparts ) ) ) {
24✔
942
                                $list[$k] = $v;
16✔
943
                        } else {
944
                                $list[$key] = $v;
10✔
945
                        }
946
                }
947

948
                return new static( $list );
24✔
949
        }
950

951

952
        /**
953
         * Collapses all sub-array elements recursively to a new map overwriting existing keys.
954
         *
955
         * Examples:
956
         *  Map::from( [0 => ['a' => 0, 'b' => 1], 1 => ['c' => 2, 'd' => 3]] )->collapse();
957
         *  Map::from( [0 => ['a' => 0, 'b' => 1], 1 => ['a' => 2]] )->collapse();
958
         *  Map::from( [0 => [0 => 0, 1 => 1], 1 => [0 => ['a' => 2, 0 => 3], 1 => 4]] )->collapse();
959
         *  Map::from( [0 => [0 => 0, 'a' => 1], 1 => [0 => ['b' => 2, 0 => 3], 1 => 4]] )->collapse( 1 );
960
         *  Map::from( [0 => [0 => 0, 'a' => 1], 1 => Map::from( [0 => ['b' => 2, 0 => 3], 1 => 4] )] )->collapse();
961
         *
962
         * Results:
963
         *  ['a' => 0, 'b' => 1, 'c' => 2, 'd' => 3]
964
         *  ['a' => 2, 'b' => 1]
965
         *  [0 => 3, 1 => 4, 'a' => 2]
966
         *  [0 => ['b' => 2, 0 => 3], 1 => 4, 'a' => 1]
967
         *  [0 => 3, 'a' => 1, 'b' => 2, 1 => 4]
968
         *
969
         * The keys are preserved and already existing elements will be overwritten.
970
         * This is also true for numeric keys! A value smaller than 1 for depth will
971
         * return the same map elements. Collapsing does also work if elements
972
         * implement the "Traversable" interface (which the Map object does).
973
         *
974
         * This method is similar than flat() but replaces already existing elements.
975
         *
976
         * @param int|null $depth Number of levels to collapse for multi-dimensional arrays or NULL for all
977
         * @return self<int|string,mixed> New map with all sub-array elements added into it recursively, up to the specified depth
978
         * @throws \InvalidArgumentException If depth must be greater or equal than 0 or NULL
979
         */
980
        public function collapse( ?int $depth = null ) : self
981
        {
982
                if( $depth < 0 ) {
48✔
983
                        throw new \InvalidArgumentException( 'Depth must be greater or equal than 0 or NULL' );
8✔
984
                }
985

986
                $result = [];
40✔
987
                $this->kflatten( $this->list(), $result, $depth ?? 0x7fffffff );
40✔
988
                return new static( $result );
40✔
989
        }
990

991

992
        /**
993
         * Combines the values of the map as keys with the passed elements as values.
994
         *
995
         * Examples:
996
         *  Map::from( ['name', 'age'] )->combine( ['Tom', 29] );
997
         *
998
         * Results:
999
         *  ['name' => 'Tom', 'age' => 29]
1000
         *
1001
         * @param iterable<int|string,mixed> $values Values of the new map
1002
         * @return self<int|string,mixed> New map
1003
         */
1004
        public function combine( iterable $values ) : self
1005
        {
1006
                return new static( array_combine( $this->list(), $this->array( $values ) ) );
8✔
1007
        }
1008

1009

1010
        /**
1011
         * Compares the value against all map elements.
1012
         *
1013
         * This method is an alias for strCompare().
1014
         *
1015
         * @param string $value Value to compare map elements to
1016
         * @param bool $case TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
1017
         * @return bool TRUE If at least one element matches, FALSE if value is not in map
1018
         * @deprecated Use strCompare() method instead
1019
         */
1020
        public function compare( string $value, bool $case = true ) : bool
1021
        {
1022
                return $this->strCompare( $value, $case );
8✔
1023
        }
1024

1025

1026
        /**
1027
         * Pushs all of the given elements onto the map with new keys without creating a new map.
1028
         *
1029
         * Examples:
1030
         *  Map::from( ['foo'] )->concat( new Map( ['bar'] ));
1031
         *
1032
         * Results:
1033
         *  ['foo', 'bar']
1034
         *
1035
         * The keys of the passed elements are NOT preserved!
1036
         *
1037
         * @param iterable<int|string,mixed> $elements List of elements
1038
         * @return self<int|string,mixed> Updated map for fluid interface
1039
         */
1040
        public function concat( iterable $elements ) : self
1041
        {
1042
                $this->list();
16✔
1043

1044
                foreach( $elements as $item ) {
16✔
1045
                        $this->list[] = $item;
16✔
1046
                }
1047

1048
                return $this;
16✔
1049
        }
1050

1051

1052
        /**
1053
         * Determines if an item exists in the map.
1054
         *
1055
         * This method combines the power of the where() method with some() to check
1056
         * if the map contains at least one of the passed values or conditions.
1057
         *
1058
         * Examples:
1059
         *  Map::from( ['a', 'b'] )->contains( 'a' );
1060
         *  Map::from( ['a', 'b'] )->contains( ['a', 'c'] );
1061
         *  Map::from( ['a', 'b'] )->contains( function( $item, $key ) {
1062
         *    return $item === 'a'
1063
         *  } );
1064
         *  Map::from( [['type' => 'name']] )->contains( 'type', 'name' );
1065
         *  Map::from( [['type' => 'name']] )->contains( 'type', '==', 'name' );
1066
         *
1067
         * Results:
1068
         * All method calls will return TRUE because at least "a" is included in the
1069
         * map or there's a "type" key with a value "name" like in the last two
1070
         * examples.
1071
         *
1072
         * Check the where() method for available operators.
1073
         *
1074
         * @param \Closure|iterable|mixed $values Anonymous function with (item, key) parameter, element or list of elements to test against
1075
         * @param string|null $op Operator used for comparison
1076
         * @param mixed $value Value used for comparison
1077
         * @return bool TRUE if at least one element is available in map, FALSE if the map contains none of them
1078
         */
1079
        public function contains( $key, ?string $operator = null, $value = null ) : bool
1080
        {
1081
                if( $operator === null ) {
16✔
1082
                        return $this->some( $key );
8✔
1083
                }
1084

1085
                if( $value === null ) {
8✔
1086
                        return !$this->where( $key, '==', $operator )->isEmpty();
8✔
1087
                }
1088

1089
                return !$this->where( $key, $operator, $value )->isEmpty();
8✔
1090
        }
1091

1092

1093
        /**
1094
         * Creates a new map with the same elements.
1095
         *
1096
         * Both maps share the same array until one of the map objects modifies the
1097
         * array. Then, the array is copied and the copy is modfied (copy on write).
1098
         *
1099
         * @return self<int|string,mixed> New map
1100
         */
1101
        public function copy() : self
1102
        {
1103
                return clone $this;
24✔
1104
        }
1105

1106

1107
        /**
1108
         * Counts the total number of elements in the map.
1109
         *
1110
         * @return int Number of elements
1111
         */
1112
        public function count() : int
1113
        {
1114
                return count( $this->list() );
72✔
1115
        }
1116

1117

1118
        /**
1119
         * Counts how often the same values are in the map.
1120
         *
1121
         * Examples:
1122
         *  Map::from( [1, 'foo', 2, 'foo', 1] )->countBy();
1123
         *  Map::from( [1.11, 3.33, 3.33, 9.99] )->countBy();
1124
         *  Map::from( [['i' => ['p' => 1.11]], ['i' => ['p' => 3.33]], ['i' => ['p' => 3.33]]] )->countBy( 'i/p' );
1125
         *  Map::from( ['a@gmail.com', 'b@yahoo.com', 'c@gmail.com'] )->countBy( function( $email ) {
1126
         *    return substr( strrchr( $email, '@' ), 1 );
1127
         *  } );
1128
         *
1129
         * Results:
1130
         *  [1 => 2, 'foo' => 2, 2 => 1]
1131
         *  ['1.11' => 1, '3.33' => 2, '9.99' => 1]
1132
         *  ['1.11' => 1, '3.33' => 2]
1133
         *  ['gmail.com' => 2, 'yahoo.com' => 1]
1134
         *
1135
         * Counting values does only work for integers and strings because these are
1136
         * the only types allowed as array keys. All elements are casted to strings
1137
         * if no callback is passed. Custom callbacks need to make sure that only
1138
         * string or integer values are returned!
1139
         *
1140
         * @param \Closure|string|null $col Key as "key1/key2/key3" or function with (value, key) parameters returning the values for counting
1141
         * @return self<int|string,mixed> New map with values as keys and their count as value
1142
         */
1143
        public function countBy( $col = null ) : self
1144
        {
1145
                if( !( $col instanceof \Closure ) )
32✔
1146
                {
1147
                        $parts = $col ? explode( $this->sep, (string) $col ) : [];
24✔
1148

1149
                        $col = function( $item ) use ( $parts ) {
18✔
1150
                                return (string) $this->val( $item, $parts );
24✔
1151
                        };
24✔
1152
                }
1153

1154
                return new static( array_count_values( array_map( $col, $this->list() ) ) );
32✔
1155
        }
1156

1157

1158
        /**
1159
         * Dumps the map content and terminates the script.
1160
         *
1161
         * The dd() method is very helpful to see what are the map elements passed
1162
         * between two map methods in a method call chain. It stops execution of the
1163
         * script afterwards to avoid further output.
1164
         *
1165
         * Examples:
1166
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->sort()->dd();
1167
         *
1168
         * Results:
1169
         *  Array
1170
         *  (
1171
         *      [0] => bar
1172
         *      [1] => foo
1173
         *  )
1174
         *
1175
         * @param callable|null $callback Function receiving the map elements as parameter (optional)
1176
         */
1177
        public function dd( ?callable $callback = null ) : void
1178
        {
1179
                $this->dump( $callback );
×
1180
                exit( 1 );
×
1181
        }
1182

1183

1184
        /**
1185
         * Returns the keys/values in the map whose values are not present in the passed elements in a new map.
1186
         *
1187
         * Examples:
1188
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->diff( ['bar'] );
1189
         *
1190
         * Results:
1191
         *  ['a' => 'foo']
1192
         *
1193
         * If a callback is passed, the given function will be used to compare the values.
1194
         * The function must accept two parameters (value A and B) and must return
1195
         * -1 if value A is smaller than value B, 0 if both are equal and 1 if value A is
1196
         * greater than value B. Both, a method name and an anonymous function can be passed:
1197
         *
1198
         *  Map::from( [0 => 'a'] )->diff( [0 => 'A'], 'strcasecmp' );
1199
         *  Map::from( ['b' => 'a'] )->diff( ['B' => 'A'], 'strcasecmp' );
1200
         *  Map::from( ['b' => 'a'] )->diff( ['c' => 'A'], function( $valA, $valB ) {
1201
         *      return strtolower( $valA ) <=> strtolower( $valB );
1202
         *  } );
1203
         *
1204
         * All examples will return an empty map because both contain the same values
1205
         * when compared case insensitive.
1206
         *
1207
         * The keys are preserved using this method.
1208
         *
1209
         * @param iterable<int|string,mixed> $elements List of elements
1210
         * @param  callable|null $callback Function with (valueA, valueB) parameters and returns -1 (<), 0 (=) and 1 (>)
1211
         * @return self<int|string,mixed> New map
1212
         */
1213
        public function diff( iterable $elements, ?callable $callback = null ) : self
1214
        {
1215
                if( $callback ) {
24✔
1216
                        return new static( array_udiff( $this->list(), $this->array( $elements ), $callback ) );
8✔
1217
                }
1218

1219
                return new static( array_diff( $this->list(), $this->array( $elements ) ) );
24✔
1220
        }
1221

1222

1223
        /**
1224
         * Returns the keys/values in the map whose keys AND values are not present in the passed elements in a new map.
1225
         *
1226
         * Examples:
1227
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->diffAssoc( new Map( ['foo', 'b' => 'bar'] ) );
1228
         *
1229
         * Results:
1230
         *  ['a' => 'foo']
1231
         *
1232
         * If a callback is passed, the given function will be used to compare the values.
1233
         * The function must accept two parameters (value A and B) and must return
1234
         * -1 if value A is smaller than value B, 0 if both are equal and 1 if value A is
1235
         * greater than value B. Both, a method name and an anonymous function can be passed:
1236
         *
1237
         *  Map::from( [0 => 'a'] )->diffAssoc( [0 => 'A'], 'strcasecmp' );
1238
         *  Map::from( ['b' => 'a'] )->diffAssoc( ['B' => 'A'], 'strcasecmp' );
1239
         *  Map::from( ['b' => 'a'] )->diffAssoc( ['c' => 'A'], function( $valA, $valB ) {
1240
         *      return strtolower( $valA ) <=> strtolower( $valB );
1241
         *  } );
1242
         *
1243
         * The first example will return an empty map because both contain the same
1244
         * values when compared case insensitive. The second and third example will return
1245
         * an empty map because 'A' is part of the passed array but the keys doesn't match
1246
         * ("b" vs. "B" and "b" vs. "c").
1247
         *
1248
         * The keys are preserved using this method.
1249
         *
1250
         * @param iterable<int|string,mixed> $elements List of elements
1251
         * @param  callable|null $callback Function with (valueA, valueB) parameters and returns -1 (<), 0 (=) and 1 (>)
1252
         * @return self<int|string,mixed> New map
1253
         */
1254
        public function diffAssoc( iterable $elements, ?callable $callback = null ) : self
1255
        {
1256
                if( $callback ) {
16✔
1257
                        return new static( array_diff_uassoc( $this->list(), $this->array( $elements ), $callback ) );
8✔
1258
                }
1259

1260
                return new static( array_diff_assoc( $this->list(), $this->array( $elements ) ) );
16✔
1261
        }
1262

1263

1264
        /**
1265
         * Returns the key/value pairs from the map whose keys are not present in the passed elements in a new map.
1266
         *
1267
         * Examples:
1268
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->diffKeys( new Map( ['foo', 'b' => 'baz'] ) );
1269
         *
1270
         * Results:
1271
         *  ['a' => 'foo']
1272
         *
1273
         * If a callback is passed, the given function will be used to compare the keys.
1274
         * The function must accept two parameters (key A and B) and must return
1275
         * -1 if key A is smaller than key B, 0 if both are equal and 1 if key A is
1276
         * greater than key B. Both, a method name and an anonymous function can be passed:
1277
         *
1278
         *  Map::from( [0 => 'a'] )->diffKeys( [0 => 'A'], 'strcasecmp' );
1279
         *  Map::from( ['b' => 'a'] )->diffKeys( ['B' => 'X'], 'strcasecmp' );
1280
         *  Map::from( ['b' => 'a'] )->diffKeys( ['c' => 'a'], function( $keyA, $keyB ) {
1281
         *      return strtolower( $keyA ) <=> strtolower( $keyB );
1282
         *  } );
1283
         *
1284
         * The first and second example will return an empty map because both contain
1285
         * the same keys when compared case insensitive. The third example will return
1286
         * ['b' => 'a'] because the keys doesn't match ("b" vs. "c").
1287
         *
1288
         * The keys are preserved using this method.
1289
         *
1290
         * @param iterable<int|string,mixed> $elements List of elements
1291
         * @param  callable|null $callback Function with (keyA, keyB) parameters and returns -1 (<), 0 (=) and 1 (>)
1292
         * @return self<int|string,mixed> New map
1293
         */
1294
        public function diffKeys( iterable $elements, ?callable $callback = null ) : self
1295
        {
1296
                if( $callback ) {
16✔
1297
                        return new static( array_diff_ukey( $this->list(), $this->array( $elements ), $callback ) );
8✔
1298
                }
1299

1300
                return new static( array_diff_key( $this->list(), $this->array( $elements ) ) );
16✔
1301
        }
1302

1303

1304
        /**
1305
         * Dumps the map content using the given function (print_r by default).
1306
         *
1307
         * The dump() method is very helpful to see what are the map elements passed
1308
         * between two map methods in a method call chain.
1309
         *
1310
         * Examples:
1311
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->dump()->asort()->dump( 'var_dump' );
1312
         *
1313
         * Results:
1314
         *  Array
1315
         *  (
1316
         *      [a] => foo
1317
         *      [b] => bar
1318
         *  )
1319
         *  array(1) {
1320
         *    ["b"]=>
1321
         *    string(3) "bar"
1322
         *    ["a"]=>
1323
         *    string(3) "foo"
1324
         *  }
1325
         *
1326
         * @param callable|null $callback Function receiving the map elements as parameter (optional)
1327
         * @return self<int|string,mixed> Same map for fluid interface
1328
         */
1329
        public function dump( ?callable $callback = null ) : self
1330
        {
1331
                $callback ? $callback( $this->list() ) : print_r( $this->list() );
8✔
1332
                return $this;
8✔
1333
        }
1334

1335

1336
        /**
1337
         * Returns the duplicate values from the map.
1338
         *
1339
         * For nested arrays, you have to pass the name of the column of the nested
1340
         * array which should be used to check for duplicates.
1341
         *
1342
         * Examples:
1343
         *  Map::from( [1, 2, '1', 3] )->duplicates()
1344
         *  Map::from( [['p' => '1'], ['p' => 1], ['p' => 2]] )->duplicates( 'p' )
1345
         *  Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->duplicates( 'i/p' )
1346
         *  Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->duplicates( fn( $item, $key ) => $item['i']['p'] )
1347
         *
1348
         * Results:
1349
         *  [2 => '1']
1350
         *  [1 => ['p' => 1]]
1351
         *  [1 => ['i' => ['p' => 1]]]
1352
         *  [1 => ['i' => ['p' => 1]]]
1353
         *
1354
         * This does also work for multi-dimensional arrays by passing the keys
1355
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
1356
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
1357
         * public properties of objects or objects implementing __isset() and __get() methods.
1358
         *
1359
         * The keys are preserved using this method.
1360
         *
1361
         * @param \Closure|string|null $col Key, path of the nested array or anonymous function with ($item, $key) parameters returning the value for comparison
1362
         * @return self<int|string,mixed> New map
1363
         */
1364
        public function duplicates( $col = null ) : self
1365
        {
1366
                $list = $map = $this->list();
32✔
1367

1368
                if( $col !== null ) {
32✔
1369
                        $map = array_map( $this->mapper( $col ), array_values( $list ), array_keys( $list ) );
24✔
1370
                }
1371

1372
                return new static( array_diff_key( $list, array_unique( $map ) ) );
32✔
1373
        }
1374

1375

1376
        /**
1377
         * Executes a callback over each entry until FALSE is returned.
1378
         *
1379
         * Examples:
1380
         *  $result = [];
1381
         *  Map::from( [0 => 'a', 1 => 'b'] )->each( function( $value, $key ) use ( &$result ) {
1382
         *      $result[$key] = strtoupper( $value );
1383
         *      return false;
1384
         *  } );
1385
         *
1386
         * The $result array will contain [0 => 'A'] because FALSE is returned
1387
         * after the first entry and all other entries are then skipped.
1388
         *
1389
         * @param \Closure $callback Function with (value, key) parameters and returns TRUE/FALSE
1390
         * @return self<int|string,mixed> Same map for fluid interface
1391
         */
1392
        public function each( \Closure $callback ) : self
1393
        {
1394
                foreach( $this->list() as $key => $item )
16✔
1395
                {
1396
                        if( $callback( $item, $key ) === false ) {
16✔
1397
                                break;
9✔
1398
                        }
1399
                }
1400

1401
                return $this;
16✔
1402
        }
1403

1404

1405
        /**
1406
         * Determines if the map is empty or not.
1407
         *
1408
         * Examples:
1409
         *  Map::from( [] )->empty();
1410
         *  Map::from( ['a'] )->empty();
1411
         *
1412
         * Results:
1413
         *  The first example returns TRUE while the second returns FALSE
1414
         *
1415
         * The method is equivalent to isEmpty().
1416
         *
1417
         * @return bool TRUE if map is empty, FALSE if not
1418
         */
1419
        public function empty() : bool
1420
        {
1421
                return empty( $this->list() );
16✔
1422
        }
1423

1424

1425
        /**
1426
         * Tests if the passed elements are equal to the elements in the map.
1427
         *
1428
         * Examples:
1429
         *  Map::from( ['a'] )->equals( ['a', 'b'] );
1430
         *  Map::from( ['a', 'b'] )->equals( ['b'] );
1431
         *  Map::from( ['a', 'b'] )->equals( ['b', 'a'] );
1432
         *
1433
         * Results:
1434
         * The first and second example will return FALSE, the third example will return TRUE
1435
         *
1436
         * The method differs to is() in the fact that it doesn't care about the keys
1437
         * by default. The elements are only loosely compared and the keys are ignored.
1438
         *
1439
         * Values are compared by their string values:
1440
         * (string) $item1 === (string) $item2
1441
         *
1442
         * @param iterable<int|string,mixed> $elements List of elements to test against
1443
         * @return bool TRUE if both are equal, FALSE if not
1444
         */
1445
        public function equals( iterable $elements ) : bool
1446
        {
1447
                $list = $this->list();
48✔
1448
                $elements = $this->array( $elements );
48✔
1449

1450
                return array_diff( $list, $elements ) === [] && array_diff( $elements, $list ) === [];
48✔
1451
        }
1452

1453

1454
        /**
1455
         * Verifies that all elements pass the test of the given callback.
1456
         *
1457
         * Examples:
1458
         *  Map::from( [0 => 'a', 1 => 'b'] )->every( function( $value, $key ) {
1459
         *      return is_string( $value );
1460
         *  } );
1461
         *
1462
         *  Map::from( [0 => 'a', 1 => 100] )->every( function( $value, $key ) {
1463
         *      return is_string( $value );
1464
         *  } );
1465
         *
1466
         * The first example will return TRUE because all values are a string while
1467
         * the second example will return FALSE.
1468
         *
1469
         * @param \Closure $callback Function with (value, key) parameters and returns TRUE/FALSE
1470
         * @return bool True if all elements pass the test, false if if fails for at least one element
1471
         */
1472
        public function every( \Closure $callback ) : bool
1473
        {
1474
                foreach( $this->list() as $key => $item )
8✔
1475
                {
1476
                        if( $callback( $item, $key ) === false ) {
8✔
1477
                                return false;
8✔
1478
                        }
1479
                }
1480

1481
                return true;
8✔
1482
        }
1483

1484

1485
        /**
1486
         * Returns a new map without the passed element keys.
1487
         *
1488
         * Examples:
1489
         *  Map::from( ['a' => 1, 'b' => 2, 'c' => 3] )->except( 'b' );
1490
         *  Map::from( [1 => 'a', 2 => 'b', 3 => 'c'] )->except( [1, 3] );
1491
         *
1492
         * Results:
1493
         *  ['a' => 1, 'c' => 3]
1494
         *  [2 => 'b']
1495
         *
1496
         * The keys in the result map are preserved.
1497
         *
1498
         * @param iterable<string|int>|array<string|int>|string|int $keys List of keys to remove
1499
         * @return self<int|string,mixed> New map
1500
         */
1501
        public function except( $keys ) : self
1502
        {
1503
                return ( clone $this )->remove( $keys );
8✔
1504
        }
1505

1506

1507
        /**
1508
         * Applies a filter to all elements of the map and returns a new map.
1509
         *
1510
         * Examples:
1511
         *  Map::from( [null, 0, 1, '', '0', 'a'] )->filter();
1512
         *  Map::from( [2 => 'a', 6 => 'b', 13 => 'm', 30 => 'z'] )->filter( function( $value, $key ) {
1513
         *      return $key < 10 && $value < 'n';
1514
         *  } );
1515
         *
1516
         * Results:
1517
         *  [1, 'a']
1518
         *  ['a', 'b']
1519
         *
1520
         * If no callback is passed, all values which are empty, null or false will be
1521
         * removed if their value converted to boolean is FALSE:
1522
         *  (bool) $value === false
1523
         *
1524
         * The keys in the result map are preserved.
1525
         *
1526
         * @param  callable|null $callback Function with (item, key) parameters and returns TRUE/FALSE
1527
         * @return self<int|string,mixed> New map
1528
         */
1529
        public function filter( ?callable $callback = null ) : self
1530
        {
1531
                // PHP 7.x compatibility
1532
                if( $callback ) {
80✔
1533
                        return new static( array_filter( $this->list(), $callback, ARRAY_FILTER_USE_BOTH ) );
72✔
1534
                }
1535

1536
                return new static( array_filter( $this->list() ) );
8✔
1537
        }
1538

1539

1540
        /**
1541
         * Returns the first/last matching element where the callback returns TRUE.
1542
         *
1543
         * Examples:
1544
         *  Map::from( ['a', 'c', 'e'] )->find( function( $value, $key ) {
1545
         *      return $value >= 'b';
1546
         *  } );
1547
         *  Map::from( ['a', 'c', 'e'] )->find( function( $value, $key ) {
1548
         *      return $value >= 'b';
1549
         *  }, null, true );
1550
         *  Map::from( [] )->find( function( $value, $key ) {
1551
         *      return $value >= 'b';
1552
         *  }, fn() => 'none' );
1553
         *  Map::from( [] )->find( function( $value, $key ) {
1554
         *      return $value >= 'b';
1555
         *  }, new \Exception( 'error' ) );
1556
         *
1557
         * Results:
1558
         * The first example will return 'c' while the second will return 'e' (last element).
1559
         * The third and forth one will return "none" and the last one will throw the exception.
1560
         *
1561
         * @param \Closure $callback Function with (value, key) parameters and returns TRUE/FALSE
1562
         * @param mixed $default Default value, closure or exception if the callback only returns FALSE
1563
         * @param bool $reverse TRUE to test elements from back to front, FALSE for front to back (default)
1564
         * @return mixed First matching value, passed default value or an exception
1565
         */
1566
        public function find( \Closure $callback, $default = null, bool $reverse = false )
1567
        {
1568
                $list = $this->list();
40✔
1569

1570
                if( !empty( $list ) )
40✔
1571
                {
1572
                        if( $reverse )
40✔
1573
                        {
1574
                                $value = end( $list );
16✔
1575
                                $key = key( $list );
16✔
1576

1577
                                do
1578
                                {
1579
                                        if( $callback( $value, $key ) ) {
16✔
1580
                                                return $value;
8✔
1581
                                        }
1582
                                }
1583
                                while( ( $value = prev( $list ) ) !== false && ( $key = key( $list ) ) !== null );
16✔
1584

1585
                                reset( $list );
8✔
1586
                        }
1587
                        else
1588
                        {
1589
                                foreach( $list as $key => $value )
24✔
1590
                                {
1591
                                        if( $callback( $value, $key ) ) {
24✔
1592
                                                return $value;
10✔
1593
                                        }
1594
                                }
1595
                        }
1596
                }
1597

1598
                if( $default instanceof \Closure ) {
24✔
1599
                        return $default();
8✔
1600
                }
1601

1602
                if( $default instanceof \Throwable ) {
16✔
1603
                        throw $default;
8✔
1604
                }
1605

1606
                return $default;
8✔
1607
        }
1608

1609

1610
        /**
1611
         * Returns the first/last key where the callback returns TRUE.
1612
         *
1613
         * Examples:
1614
         *  Map::from( ['a', 'c', 'e'] )->findKey( function( $value, $key ) {
1615
         *      return $value >= 'b';
1616
         *  } );
1617
         *  Map::from( ['a', 'c', 'e'] )->findKey( function( $value, $key ) {
1618
         *      return $value >= 'b';
1619
         *  }, null, true );
1620
         *  Map::from( [] )->findKey( function( $value, $key ) {
1621
         *      return $value >= 'b';
1622
         *  }, 'none' );
1623
         *  Map::from( [] )->findKey( function( $value, $key ) {
1624
         *      return $value >= 'b';
1625
         *  }, fn() => 'none' );
1626
         *  Map::from( [] )->findKey( function( $value, $key ) {
1627
         *      return $value >= 'b';
1628
         *  }, new \Exception( 'error' ) );
1629
         *
1630
         * Results:
1631
         * The first example will return '1' while the second will return '2' (last element).
1632
         * The third and forth one will return "none" and the last one will throw the exception.
1633
         *
1634
         * @param \Closure $callback Function with (value, key) parameters and returns TRUE/FALSE
1635
         * @param mixed $default Default value, closure or exception if the callback only returns FALSE
1636
         * @param bool $reverse TRUE to test elements from back to front, FALSE for front to back (default)
1637
         * @return mixed Key of first matching element, passed default value or an exception
1638
         */
1639
        public function findKey( \Closure $callback, $default = null, bool $reverse = false )
1640
        {
1641
                $list = $this->list();
40✔
1642

1643
                if( !empty( $list ) )
40✔
1644
                {
1645
                        if( $reverse )
16✔
1646
                        {
1647
                                $value = end( $list );
8✔
1648
                                $key = key( $list );
8✔
1649

1650
                                do
1651
                                {
1652
                                        if( $callback( $value, $key ) ) {
8✔
1653
                                                return $key;
8✔
1654
                                        }
1655
                                }
1656
                                while( ( $value = prev( $list ) ) !== false && ( $key = key( $list ) ) !== null );
×
1657

1658
                                reset( $list );
×
1659
                        }
1660
                        else
1661
                        {
1662
                                foreach( $list as $key => $value )
8✔
1663
                                {
1664
                                        if( $callback( $value, $key ) ) {
8✔
1665
                                                return $key;
8✔
1666
                                        }
1667
                                }
1668
                        }
1669
                }
1670

1671
                if( $default instanceof \Closure ) {
24✔
1672
                        return $default();
8✔
1673
                }
1674

1675
                if( $default instanceof \Throwable ) {
16✔
1676
                        throw $default;
8✔
1677
                }
1678

1679
                return $default;
8✔
1680
        }
1681

1682

1683
        /**
1684
         * Returns the first element from the map.
1685
         *
1686
         * Examples:
1687
         *  Map::from( ['a', 'b'] )->first();
1688
         *  Map::from( [] )->first( 'x' );
1689
         *  Map::from( [] )->first( new \Exception( 'error' ) );
1690
         *  Map::from( [] )->first( function() { return rand(); } );
1691
         *
1692
         * Results:
1693
         * The first example will return 'b' and the second one 'x'. The third example
1694
         * will throw the exception passed if the map contains no elements. In the
1695
         * fourth example, a random value generated by the closure function will be
1696
         * returned.
1697
         *
1698
         * Using this method doesn't affect the internal array pointer.
1699
         *
1700
         * @param mixed $default Default value, closure or exception if the map contains no elements
1701
         * @return mixed First value of map, (generated) default value or an exception
1702
         */
1703
        public function first( $default = null )
1704
        {
1705
                if( !empty( $this->list() ) ) {
72✔
1706
                        return current( array_slice( $this->list(), 0, 1 ) );
40✔
1707
                }
1708

1709
                if( $default instanceof \Closure ) {
40✔
1710
                        return $default();
8✔
1711
                }
1712

1713
                if( $default instanceof \Throwable ) {
32✔
1714
                        throw $default;
8✔
1715
                }
1716

1717
                return $default;
24✔
1718
        }
1719

1720

1721
        /**
1722
         * Returns the first key from the map.
1723
         *
1724
         * Examples:
1725
         *  Map::from( ['a' => 1, 'b' => 2] )->firstKey();
1726
         *  Map::from( [] )->firstKey( 'x' );
1727
         *  Map::from( [] )->firstKey( new \Exception( 'error' ) );
1728
         *  Map::from( [] )->firstKey( function() { return rand(); } );
1729
         *
1730
         * Results:
1731
         * The first example will return 'a' and the second one 'x', the third one will throw
1732
         * an exception and the last one will return a random value.
1733
         *
1734
         * Using this method doesn't affect the internal array pointer.
1735
         *
1736
         * @param mixed $default Default value, closure or exception if the map contains no elements
1737
         * @return mixed First key of map, (generated) default value or an exception
1738
         */
1739
        public function firstKey( $default = null )
1740
        {
1741
                $list = $this->list();
40✔
1742

1743
                // PHP 7.x compatibility
1744
                if( function_exists( 'array_key_first' ) ) {
40✔
1745
                        $key = array_key_first( $list );
40✔
1746
                } else {
1747
                        $key = key( array_slice( $list, 0, 1, true ) );
×
1748
                }
1749

1750
                if( $key !== null ) {
40✔
1751
                        return $key;
8✔
1752
                }
1753

1754
                if( $default instanceof \Closure ) {
32✔
1755
                        return $default();
8✔
1756
                }
1757

1758
                if( $default instanceof \Throwable ) {
24✔
1759
                        throw $default;
8✔
1760
                }
1761

1762
                return $default;
16✔
1763
        }
1764

1765

1766
        /**
1767
         * Creates a new map with all sub-array elements added recursively withput overwriting existing keys.
1768
         *
1769
         * Examples:
1770
         *  Map::from( [[0, 1], [2, 3]] )->flat();
1771
         *  Map::from( [[0, 1], [[2, 3], 4]] )->flat();
1772
         *  Map::from( [[0, 1], [[2, 3], 4]] )->flat( 1 );
1773
         *  Map::from( [[0, 1], Map::from( [[2, 3], 4] )] )->flat();
1774
         *
1775
         * Results:
1776
         *  [0, 1, 2, 3]
1777
         *  [0, 1, 2, 3, 4]
1778
         *  [0, 1, [2, 3], 4]
1779
         *  [0, 1, 2, 3, 4]
1780
         *
1781
         * The keys are not preserved and the new map elements will be numbered from
1782
         * 0-n. A value smaller than 1 for depth will return the same map elements
1783
         * indexed from 0-n. Flattening does also work if elements implement the
1784
         * "Traversable" interface (which the Map object does).
1785
         *
1786
         * This method is similar than collapse() but doesn't replace existing elements.
1787
         * Keys are NOT preserved using this method!
1788
         *
1789
         * @param int|null $depth Number of levels to flatten multi-dimensional arrays or NULL for all
1790
         * @return self<int|string,mixed> New map with all sub-array elements added into it recursively, up to the specified depth
1791
         * @throws \InvalidArgumentException If depth must be greater or equal than 0 or NULL
1792
         */
1793
        public function flat( ?int $depth = null ) : self
1794
        {
1795
                if( $depth < 0 ) {
48✔
1796
                        throw new \InvalidArgumentException( 'Depth must be greater or equal than 0 or NULL' );
8✔
1797
                }
1798

1799
                $result = [];
40✔
1800
                $this->flatten( $this->list(), $result, $depth ?? 0x7fffffff );
40✔
1801
                return new static( $result );
40✔
1802
        }
1803

1804

1805
        /**
1806
         * Exchanges the keys with their values and vice versa.
1807
         *
1808
         * Examples:
1809
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->flip();
1810
         *
1811
         * Results:
1812
         *  ['X' => 'a', 'Y' => 'b']
1813
         *
1814
         * @return self<int|string,mixed> New map with keys as values and values as keys
1815
         */
1816
        public function flip() : self
1817
        {
1818
                return new static( array_flip( $this->list() ) );
8✔
1819
        }
1820

1821

1822
        /**
1823
         * Returns an element by key and casts it to float if possible.
1824
         *
1825
         * Examples:
1826
         *  Map::from( ['a' => true] )->float( 'a' );
1827
         *  Map::from( ['a' => 1] )->float( 'a' );
1828
         *  Map::from( ['a' => '1.1'] )->float( 'a' );
1829
         *  Map::from( ['a' => '10'] )->float( 'a' );
1830
         *  Map::from( ['a' => ['b' => ['c' => 1.1]]] )->float( 'a/b/c' );
1831
         *  Map::from( [] )->float( 'c', function() { return 1.1; } );
1832
         *  Map::from( [] )->float( 'a', 1.1 );
1833
         *
1834
         *  Map::from( [] )->float( 'b' );
1835
         *  Map::from( ['b' => ''] )->float( 'b' );
1836
         *  Map::from( ['b' => null] )->float( 'b' );
1837
         *  Map::from( ['b' => 'abc'] )->float( 'b' );
1838
         *  Map::from( ['b' => [1]] )->float( 'b' );
1839
         *  Map::from( ['b' => #resource] )->float( 'b' );
1840
         *  Map::from( ['b' => new \stdClass] )->float( 'b' );
1841
         *
1842
         *  Map::from( [] )->float( 'c', new \Exception( 'error' ) );
1843
         *
1844
         * Results:
1845
         * The first eight examples will return the float values for the passed keys
1846
         * while the 9th to 14th example returns 0. The last example will throw an exception.
1847
         *
1848
         * This does also work for multi-dimensional arrays by passing the keys
1849
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
1850
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
1851
         * public properties of objects or objects implementing __isset() and __get() methods.
1852
         *
1853
         * @param int|string $key Key or path to the requested item
1854
         * @param mixed $default Default value if key isn't found (will be casted to float)
1855
         * @return float Value from map or default value
1856
         */
1857
        public function float( $key, $default = 0.0 ) : float
1858
        {
1859
                return (float) ( is_scalar( $val = $this->get( $key, $default ) ) ? $val : $default );
24✔
1860
        }
1861

1862

1863
        /**
1864
         * Returns an element from the map by key.
1865
         *
1866
         * Examples:
1867
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->get( 'a' );
1868
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->get( 'c', 'Z' );
1869
         *  Map::from( ['a' => ['b' => ['c' => 'Y']]] )->get( 'a/b/c' );
1870
         *  Map::from( [] )->get( 'Y', new \Exception( 'error' ) );
1871
         *  Map::from( [] )->get( 'Y', function() { return rand(); } );
1872
         *
1873
         * Results:
1874
         * The first example will return 'X', the second 'Z' and the third 'Y'. The forth
1875
         * example will throw the exception passed if the map contains no elements. In
1876
         * the fifth example, a random value generated by the closure function will be
1877
         * returned.
1878
         *
1879
         * This does also work for multi-dimensional arrays by passing the keys
1880
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
1881
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
1882
         * public properties of objects or objects implementing __isset() and __get() methods.
1883
         *
1884
         * @param int|string $key Key or path to the requested item
1885
         * @param mixed $default Default value if no element matches
1886
         * @return mixed Value from map or default value
1887
         */
1888
        public function get( $key, $default = null )
1889
        {
1890
                $list = $this->list();
184✔
1891

1892
                if( array_key_exists( $key, $list ) ) {
184✔
1893
                        return $list[$key];
48✔
1894
                }
1895

1896
                if( ( $v = $this->val( $list, explode( $this->sep, (string) $key ) ) ) !== null ) {
168✔
1897
                        return $v;
56✔
1898
                }
1899

1900
                if( $default instanceof \Closure ) {
144✔
1901
                        return $default();
48✔
1902
                }
1903

1904
                if( $default instanceof \Throwable ) {
96✔
1905
                        throw $default;
48✔
1906
                }
1907

1908
                return $default;
48✔
1909
        }
1910

1911

1912
        /**
1913
         * Returns an iterator for the elements.
1914
         *
1915
         * This method will be used by e.g. foreach() to loop over all entries:
1916
         *  foreach( Map::from( ['a', 'b'] ) as $value )
1917
         *
1918
         * @return \ArrayIterator<int|string,mixed> Iterator for map elements
1919
         */
1920
        public function getIterator() : \ArrayIterator
1921
        {
1922
                return new \ArrayIterator( $this->list() );
40✔
1923
        }
1924

1925

1926
        /**
1927
         * Returns only items which matches the regular expression.
1928
         *
1929
         * All items are converted to string first before they are compared to the
1930
         * regular expression. Thus, fractions of ".0" will be removed in float numbers
1931
         * which may result in unexpected results.
1932
         *
1933
         * Examples:
1934
         *  Map::from( ['ab', 'bc', 'cd'] )->grep( '/b/' );
1935
         *  Map::from( ['ab', 'bc', 'cd'] )->grep( '/a/', PREG_GREP_INVERT );
1936
         *  Map::from( [1.5, 0, 1.0, 'a'] )->grep( '/^(\d+)?\.\d+$/' );
1937
         *
1938
         * Results:
1939
         *  ['ab', 'bc']
1940
         *  ['bc', 'cd']
1941
         *  [1.5] // float 1.0 is converted to string "1"
1942
         *
1943
         * The keys are preserved using this method.
1944
         *
1945
         * @param string $pattern Regular expression pattern, e.g. "/ab/"
1946
         * @param int $flags PREG_GREP_INVERT to return elements not matching the pattern
1947
         * @return self<int|string,mixed> New map containing only the matched elements
1948
         */
1949
        public function grep( string $pattern, int $flags = 0 ) : self
1950
        {
1951
                if( ( $result = preg_grep( $pattern, $this->list(), $flags ) ) === false )
32✔
1952
                {
1953
                        switch( preg_last_error() )
8✔
1954
                        {
1955
                                case PREG_INTERNAL_ERROR: $msg = 'Internal error'; break;
8✔
1956
                                case PREG_BACKTRACK_LIMIT_ERROR: $msg = 'Backtrack limit error'; break;
×
1957
                                case PREG_RECURSION_LIMIT_ERROR: $msg = 'Recursion limit error'; break;
×
1958
                                case PREG_BAD_UTF8_ERROR: $msg = 'Bad UTF8 error'; break;
×
1959
                                case PREG_BAD_UTF8_OFFSET_ERROR: $msg = 'Bad UTF8 offset error'; break;
×
1960
                                case PREG_JIT_STACKLIMIT_ERROR: $msg = 'JIT stack limit error'; break;
×
1961
                                default: $msg = 'Unknown error';
×
1962
                        }
1963

1964
                        throw new \RuntimeException( 'Regular expression error: ' . $msg );
8✔
1965
                }
1966

1967
                return new static( $result );
24✔
1968
        }
1969

1970

1971
        /**
1972
         * Groups associative array elements or objects by the passed key or closure.
1973
         *
1974
         * Instead of overwriting items with the same keys like to the col() method
1975
         * does, groupBy() keeps all entries in sub-arrays. It's preserves the keys
1976
         * of the orignal map entries too.
1977
         *
1978
         * Examples:
1979
         *  $list = [
1980
         *    10 => ['aid' => 123, 'code' => 'x-abc'],
1981
         *    20 => ['aid' => 123, 'code' => 'x-def'],
1982
         *    30 => ['aid' => 456, 'code' => 'x-def']
1983
         *  ];
1984
         *  Map::from( $list )->groupBy( 'aid' );
1985
         *  Map::from( $list )->groupBy( function( $item, $key ) {
1986
         *    return substr( $item['code'], -3 );
1987
         *  } );
1988
         *  Map::from( $list )->groupBy( 'xid' );
1989
         *
1990
         * Results:
1991
         *  [
1992
         *    123 => [10 => ['aid' => 123, 'code' => 'x-abc'], 20 => ['aid' => 123, 'code' => 'x-def']],
1993
         *    456 => [30 => ['aid' => 456, 'code' => 'x-def']]
1994
         *  ]
1995
         *  [
1996
         *    'abc' => [10 => ['aid' => 123, 'code' => 'x-abc']],
1997
         *    'def' => [20 => ['aid' => 123, 'code' => 'x-def'], 30 => ['aid' => 456, 'code' => 'x-def']]
1998
         *  ]
1999
         *  [
2000
         *    '' => [
2001
         *      10 => ['aid' => 123, 'code' => 'x-abc'],
2002
         *      20 => ['aid' => 123, 'code' => 'x-def'],
2003
         *      30 => ['aid' => 456, 'code' => 'x-def']
2004
         *    ]
2005
         *  ]
2006
         *
2007
         * In case the passed key doesn't exist in one or more items, these items
2008
         * are stored in a sub-array using an empty string as key.
2009
         *
2010
         * This does also work for multi-dimensional arrays by passing the keys
2011
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
2012
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
2013
         * public properties of objects or objects implementing __isset() and __get() methods.
2014
         *
2015
         * @param  \Closure|string|int $key Closure function with (item, idx) parameters returning the key or the key itself to group by
2016
         * @return self<int|string,mixed> New map with elements grouped by the given key
2017
         */
2018
        public function groupBy( $key ) : self
2019
        {
2020
                $result = [];
32✔
2021

2022
                if( is_callable( $key ) )
32✔
2023
                {
2024
                        foreach( $this->list() as $idx => $item )
8✔
2025
                        {
2026
                                $keyval = (string) $key( $item, $idx );
8✔
2027
                                $result[$keyval][$idx] = $item;
8✔
2028
                        }
2029
                }
2030
                else
2031
                {
2032
                        $parts = explode( $this->sep, (string) $key );
24✔
2033

2034
                        foreach( $this->list() as $idx => $item )
24✔
2035
                        {
2036
                                $keyval = (string) $this->val( $item, $parts );
24✔
2037
                                $result[$keyval][$idx] = $item;
24✔
2038
                        }
2039
                }
2040

2041
                return new static( $result );
32✔
2042
        }
2043

2044

2045
        /**
2046
         * Determines if a key or several keys exists in the map.
2047
         *
2048
         * If several keys are passed as array, all keys must exist in the map for
2049
         * TRUE to be returned.
2050
         *
2051
         * Examples:
2052
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->has( 'a' );
2053
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->has( ['a', 'b'] );
2054
         *  Map::from( ['a' => ['b' => ['c' => 'Y']]] )->has( 'a/b/c' );
2055
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->has( 'c' );
2056
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->has( ['a', 'c'] );
2057
         *  Map::from( ['a' => 'X', 'b' => 'Y'] )->has( 'X' );
2058
         *
2059
         * Results:
2060
         * The first three examples will return TRUE while the other ones will return FALSE
2061
         *
2062
         * This does also work for multi-dimensional arrays by passing the keys
2063
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
2064
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
2065
         * public properties of objects or objects implementing __isset() and __get() methods.
2066
         *
2067
         * @param array<int|string>|int|string $key Key of the requested item or list of keys
2068
         * @return bool TRUE if key or keys are available in map, FALSE if not
2069
         */
2070
        public function has( $key ) : bool
2071
        {
2072
                $list = $this->list();
24✔
2073

2074
                foreach( (array) $key as $entry )
24✔
2075
                {
2076
                        if( array_key_exists( $entry, $list ) === false
24✔
2077
                                && $this->val( $list, explode( $this->sep, (string) $entry ) ) === null
24✔
2078
                        ) {
2079
                                return false;
24✔
2080
                        }
2081
                }
2082

2083
                return true;
24✔
2084
        }
2085

2086

2087
        /**
2088
         * Executes callbacks depending on the condition.
2089
         *
2090
         * If callbacks for "then" and/or "else" are passed, these callbacks will be
2091
         * executed and their returned value is passed back within a Map object. In
2092
         * case no "then" or "else" closure is given, the method will return the same
2093
         * map object.
2094
         *
2095
         * Examples:
2096
         *  Map::from( [] )->if( strpos( 'abc', 'b' ) !== false, function( $map ) {
2097
         *    echo 'found';
2098
         *  } );
2099
         *
2100
         *  Map::from( [] )->if( function( $map ) {
2101
         *    return $map->empty();
2102
         *  }, function( $map ) {
2103
         *    echo 'then';
2104
         *  } );
2105
         *
2106
         *  Map::from( ['a'] )->if( function( $map ) {
2107
         *    return $map->empty();
2108
         *  }, function( $map ) {
2109
         *    echo 'then';
2110
         *  }, function( $map ) {
2111
         *    echo 'else';
2112
         *  } );
2113
         *
2114
         *  Map::from( ['a', 'b'] )->if( true, function( $map ) {
2115
         *    return $map->push( 'c' );
2116
         *  } );
2117
         *
2118
         *  Map::from( ['a', 'b'] )->if( false, null, function( $map ) {
2119
         *    return $map->pop();
2120
         *  } );
2121
         *
2122
         * Results:
2123
         * The first example returns "found" while the second one returns "then" and
2124
         * the third one "else". The forth one will return ['a', 'b', 'c'] while the
2125
         * fifth one will return 'b', which is turned into a map of ['b'] again.
2126
         *
2127
         * Since PHP 7.4, you can also pass arrow function like `fn($map) => $map->has('c')`
2128
         * (a short form for anonymous closures) as parameters. The automatically have access
2129
         * to previously defined variables but can not modify them. Also, they can not have
2130
         * a void return type and must/will always return something. Details about
2131
         * [PHP arrow functions](https://www.php.net/manual/en/functions.arrow.php)
2132
         *
2133
         * @param \Closure|bool $condition Boolean or function with (map) parameter returning a boolean
2134
         * @param \Closure|null $then Function with (map, condition) parameter (optional)
2135
         * @param \Closure|null $else Function with (map, condition) parameter (optional)
2136
         * @return self<int|string,mixed> New map
2137
         */
2138
        public function if( $condition, ?\Closure $then = null, ?\Closure $else = null ) : self
2139
        {
2140
                if( $condition instanceof \Closure ) {
64✔
2141
                        $condition = $condition( $this );
16✔
2142
                }
2143

2144
                if( $condition ) {
64✔
2145
                        return $then ? static::from( $then( $this, $condition ) ) : $this;
40✔
2146
                } elseif( $else ) {
24✔
2147
                        return static::from( $else( $this, $condition ) );
24✔
2148
                }
2149

2150
                return $this;
×
2151
        }
2152

2153

2154
        /**
2155
         * Executes callbacks depending if the map contains elements or not.
2156
         *
2157
         * If callbacks for "then" and/or "else" are passed, these callbacks will be
2158
         * executed and their returned value is passed back within a Map object. In
2159
         * case no "then" or "else" closure is given, the method will return the same
2160
         * map object.
2161
         *
2162
         * Examples:
2163
         *  Map::from( ['a'] )->ifAny( function( $map ) {
2164
         *    $map->push( 'b' );
2165
         *  } );
2166
         *
2167
         *  Map::from( [] )->ifAny( null, function( $map ) {
2168
         *    return $map->push( 'b' );
2169
         *  } );
2170
         *
2171
         *  Map::from( ['a'] )->ifAny( function( $map ) {
2172
         *    return 'c';
2173
         *  } );
2174
         *
2175
         * Results:
2176
         * The first example returns a Map containing ['a', 'b'] because the the initial
2177
         * Map is not empty. The second one returns  a Map with ['b'] because the initial
2178
         * Map is empty and the "else" closure is used. The last example returns ['c']
2179
         * as new map content.
2180
         *
2181
         * Since PHP 7.4, you can also pass arrow function like `fn($map) => $map->has('c')`
2182
         * (a short form for anonymous closures) as parameters. The automatically have access
2183
         * to previously defined variables but can not modify them. Also, they can not have
2184
         * a void return type and must/will always return something. Details about
2185
         * [PHP arrow functions](https://www.php.net/manual/en/functions.arrow.php)
2186
         *
2187
         * @param \Closure|null $then Function with (map, condition) parameter (optional)
2188
         * @param \Closure|null $else Function with (map, condition) parameter (optional)
2189
         * @return self<int|string,mixed> New map
2190
         */
2191
        public function ifAny( ?\Closure $then = null, ?\Closure $else = null ) : self
2192
        {
2193
                return $this->if( !empty( $this->list() ), $then, $else );
24✔
2194
        }
2195

2196

2197
        /**
2198
         * Executes callbacks depending if the map is empty or not.
2199
         *
2200
         * If callbacks for "then" and/or "else" are passed, these callbacks will be
2201
         * executed and their returned value is passed back within a Map object. In
2202
         * case no "then" or "else" closure is given, the method will return the same
2203
         * map object.
2204
         *
2205
         * Examples:
2206
         *  Map::from( [] )->ifEmpty( function( $map ) {
2207
         *    $map->push( 'a' );
2208
         *  } );
2209
         *
2210
         *  Map::from( ['a'] )->ifEmpty( null, function( $map ) {
2211
         *    return $map->push( 'b' );
2212
         *  } );
2213
         *
2214
         * Results:
2215
         * The first example returns a Map containing ['a'] because the the initial Map
2216
         * is empty. The second one returns  a Map with ['a', 'b'] because the initial
2217
         * Map is not empty and the "else" closure is used.
2218
         *
2219
         * Since PHP 7.4, you can also pass arrow function like `fn($map) => $map->has('c')`
2220
         * (a short form for anonymous closures) as parameters. The automatically have access
2221
         * to previously defined variables but can not modify them. Also, they can not have
2222
         * a void return type and must/will always return something. Details about
2223
         * [PHP arrow functions](https://www.php.net/manual/en/functions.arrow.php)
2224
         *
2225
         * @param \Closure|null $then Function with (map, condition) parameter (optional)
2226
         * @param \Closure|null $else Function with (map, condition) parameter (optional)
2227
         * @return self<int|string,mixed> New map
2228
         */
2229
        public function ifEmpty( ?\Closure $then = null, ?\Closure $else = null ) : self
2230
        {
2231
                return $this->if( empty( $this->list() ), $then, $else );
×
2232
        }
2233

2234

2235
        /**
2236
         * Tests if all entries in the map are objects implementing the given interface.
2237
         *
2238
         * Examples:
2239
         *  Map::from( [new Map(), new Map()] )->implements( '\Countable' );
2240
         *  Map::from( [new Map(), new stdClass()] )->implements( '\Countable' );
2241
         *  Map::from( [new Map(), 123] )->implements( '\Countable' );
2242
         *  Map::from( [new Map(), 123] )->implements( '\Countable', true );
2243
         *  Map::from( [new Map(), 123] )->implements( '\Countable', '\RuntimeException' );
2244
         *
2245
         * Results:
2246
         *  The first example returns TRUE while the second and third one return FALSE.
2247
         *  The forth example will throw an UnexpectedValueException while the last one
2248
         *  throws a RuntimeException.
2249
         *
2250
         * @param string $interface Name of the interface that must be implemented
2251
         * @param \Throwable|bool $throw Passing TRUE or an exception name will throw the exception instead of returning FALSE
2252
         * @return bool TRUE if all entries implement the interface or FALSE if at least one doesn't
2253
         * @throws \UnexpectedValueException|\Throwable If one entry doesn't implement the interface
2254
         */
2255
        public function implements( string $interface, $throw = false ) : bool
2256
        {
2257
                foreach( $this->list() as $entry )
24✔
2258
                {
2259
                        if( !( $entry instanceof $interface ) )
24✔
2260
                        {
2261
                                if( $throw )
24✔
2262
                                {
2263
                                        $name = is_string( $throw ) ? $throw : '\UnexpectedValueException';
16✔
2264
                                        throw new $name( "Does not implement $interface: " . print_r( $entry, true ) );
16✔
2265
                                }
2266

2267
                                return false;
10✔
2268
                        }
2269
                }
2270

2271
                return true;
8✔
2272
        }
2273

2274

2275
        /**
2276
         * Tests if the passed element or elements are part of the map.
2277
         *
2278
         * Examples:
2279
         *  Map::from( ['a', 'b'] )->in( 'a' );
2280
         *  Map::from( ['a', 'b'] )->in( ['a', 'b'] );
2281
         *  Map::from( ['a', 'b'] )->in( 'x' );
2282
         *  Map::from( ['a', 'b'] )->in( ['a', 'x'] );
2283
         *  Map::from( ['1', '2'] )->in( 2, true );
2284
         *
2285
         * Results:
2286
         * The first and second example will return TRUE while the other ones will return FALSE
2287
         *
2288
         * @param mixed|array $element Element or elements to search for in the map
2289
         * @param bool $strict TRUE to check the type too, using FALSE '1' and 1 will be the same
2290
         * @return bool TRUE if all elements are available in map, FALSE if not
2291
         */
2292
        public function in( $element, bool $strict = false ) : bool
2293
        {
2294
                if( !is_array( $element ) ) {
32✔
2295
                        return in_array( $element, $this->list(), $strict );
32✔
2296
                };
2297

2298
                foreach( $element as $entry )
8✔
2299
                {
2300
                        if( in_array( $entry, $this->list(), $strict ) === false ) {
8✔
2301
                                return false;
8✔
2302
                        }
2303
                }
2304

2305
                return true;
8✔
2306
        }
2307

2308

2309
        /**
2310
         * Tests if the passed element or elements are part of the map.
2311
         *
2312
         * This method is an alias for in(). For performance reasons, in() should be
2313
         * preferred because it uses one method call less than includes().
2314
         *
2315
         * @param mixed|array $element Element or elements to search for in the map
2316
         * @param bool $strict TRUE to check the type too, using FALSE '1' and 1 will be the same
2317
         * @return bool TRUE if all elements are available in map, FALSE if not
2318
         * @see in() - Underlying method with same parameters and return value but better performance
2319
         */
2320
        public function includes( $element, bool $strict = false ) : bool
2321
        {
2322
                return $this->in( $element, $strict );
8✔
2323
        }
2324

2325

2326
        /**
2327
         * Returns the numerical index of the given key.
2328
         *
2329
         * Examples:
2330
         *  Map::from( [4 => 'a', 8 => 'b'] )->index( '8' );
2331
         *  Map::from( [4 => 'a', 8 => 'b'] )->index( function( $key ) {
2332
         *      return $key == '8';
2333
         *  } );
2334
         *
2335
         * Results:
2336
         * Both examples will return "1" because the value "b" is at the second position
2337
         * and the returned index is zero based so the first item has the index "0".
2338
         *
2339
         * @param \Closure|string|int $value Key to search for or function with (key) parameters return TRUE if key is found
2340
         * @return int|null Position of the found value (zero based) or NULL if not found
2341
         */
2342
        public function index( $value ) : ?int
2343
        {
2344
                if( $value instanceof \Closure )
32✔
2345
                {
2346
                        $pos = 0;
16✔
2347

2348
                        foreach( $this->list() as $key => $item )
16✔
2349
                        {
2350
                                if( $value( $key ) ) {
8✔
2351
                                        return $pos;
8✔
2352
                                }
2353

2354
                                ++$pos;
8✔
2355
                        }
2356

2357
                        return null;
8✔
2358
                }
2359

2360
                $pos = array_search( $value, array_keys( $this->list() ) );
16✔
2361
                return $pos !== false ? $pos : null;
16✔
2362
        }
2363

2364

2365
        /**
2366
         * Inserts the value or values after the given element.
2367
         *
2368
         * Examples:
2369
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertAfter( 'foo', 'baz' );
2370
         *  Map::from( ['foo', 'bar'] )->insertAfter( 'foo', ['baz', 'boo'] );
2371
         *  Map::from( ['foo', 'bar'] )->insertAfter( null, 'baz' );
2372
         *
2373
         * Results:
2374
         *  ['a' => 'foo', 0 => 'baz', 'b' => 'bar']
2375
         *  ['foo', 'baz', 'boo', 'bar']
2376
         *  ['foo', 'bar', 'baz']
2377
         *
2378
         * Numerical array indexes are not preserved.
2379
         *
2380
         * @param mixed $element Element after the value is inserted
2381
         * @param mixed $value Element or list of elements to insert
2382
         * @return self<int|string,mixed> Updated map for fluid interface
2383
         */
2384
        public function insertAfter( $element, $value ) : self
2385
        {
2386
                $position = ( $element !== null && ( $pos = $this->pos( $element ) ) !== null ? $pos : count( $this->list() ) );
24✔
2387
                array_splice( $this->list(), $position + 1, 0, $this->array( $value ) );
24✔
2388

2389
                return $this;
24✔
2390
        }
2391

2392

2393
        /**
2394
         * Inserts the item at the given position in the map.
2395
         *
2396
         * Examples:
2397
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertAt( 0, 'baz' );
2398
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertAt( 1, 'baz', 'c' );
2399
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertAt( 4, 'baz' );
2400
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertAt( -1, 'baz', 'c' );
2401
         *
2402
         * Results:
2403
         *  [0 => 'baz', 'a' => 'foo', 'b' => 'bar']
2404
         *  ['a' => 'foo', 'c' => 'baz', 'b' => 'bar']
2405
         *  ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']
2406
         *  ['a' => 'foo', 'c' => 'baz', 'b' => 'bar']
2407
         *
2408
         * @param int $pos Position the element it should be inserted at
2409
         * @param mixed $element Element to be inserted
2410
         * @param mixed|null $key Element key or NULL to assign an integer key automatically
2411
         * @return self<int|string,mixed> Updated map for fluid interface
2412
         */
2413
        public function insertAt( int $pos, $element, $key = null ) : self
2414
        {
2415
                if( $key !== null )
40✔
2416
                {
2417
                        $list = $this->list();
16✔
2418

2419
                        $this->list = array_merge(
16✔
2420
                                array_slice( $list, 0, $pos, true ),
16✔
2421
                                [$key => $element],
16✔
2422
                                array_slice( $list, $pos, null, true )
16✔
2423
                        );
12✔
2424
                }
2425
                else
2426
                {
2427
                        array_splice( $this->list(), $pos, 0, [$element] );
24✔
2428
                }
2429

2430
                return $this;
40✔
2431
        }
2432

2433

2434
        /**
2435
         * Inserts the value or values before the given element.
2436
         *
2437
         * Examples:
2438
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertBefore( 'bar', 'baz' );
2439
         *  Map::from( ['foo', 'bar'] )->insertBefore( 'bar', ['baz', 'boo'] );
2440
         *  Map::from( ['foo', 'bar'] )->insertBefore( null, 'baz' );
2441
         *
2442
         * Results:
2443
         *  ['a' => 'foo', 0 => 'baz', 'b' => 'bar']
2444
         *  ['foo', 'baz', 'boo', 'bar']
2445
         *  ['foo', 'bar', 'baz']
2446
         *
2447
         * Numerical array indexes are not preserved.
2448
         *
2449
         * @param mixed $element Element before the value is inserted
2450
         * @param mixed $value Element or list of elements to insert
2451
         * @return self<int|string,mixed> Updated map for fluid interface
2452
         */
2453
        public function insertBefore( $element, $value ) : self
2454
        {
2455
                $position = ( $element !== null && ( $pos = $this->pos( $element ) ) !== null ? $pos : count( $this->list() ) );
24✔
2456
                array_splice( $this->list(), $position, 0, $this->array( $value ) );
24✔
2457

2458
                return $this;
24✔
2459
        }
2460

2461

2462
        /**
2463
         * Tests if the passed value or values are part of the strings in the map.
2464
         *
2465
         * Examples:
2466
         *  Map::from( ['abc'] )->inString( 'c' );
2467
         *  Map::from( ['abc'] )->inString( 'bc' );
2468
         *  Map::from( [12345] )->inString( '23' );
2469
         *  Map::from( [123.4] )->inString( 23.4 );
2470
         *  Map::from( [12345] )->inString( false );
2471
         *  Map::from( [12345] )->inString( true );
2472
         *  Map::from( [false] )->inString( false );
2473
         *  Map::from( ['abc'] )->inString( '' );
2474
         *  Map::from( [''] )->inString( false );
2475
         *  Map::from( ['abc'] )->inString( 'BC', false );
2476
         *  Map::from( ['abc', 'def'] )->inString( ['de', 'xy'] );
2477
         *  Map::from( ['abc', 'def'] )->inString( ['E', 'x'] );
2478
         *  Map::from( ['abc', 'def'] )->inString( 'E' );
2479
         *  Map::from( [23456] )->inString( true );
2480
         *  Map::from( [false] )->inString( 0 );
2481
         *
2482
         * Results:
2483
         * The first eleven examples will return TRUE while the last four will return FALSE
2484
         *
2485
         * All scalar values (bool, float, int and string) are casted to string values before
2486
         * comparing to the given value. Non-scalar values in the map are ignored.
2487
         *
2488
         * @param array|string $value Value or values to compare the map elements, will be casted to string type
2489
         * @param bool $case TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
2490
         * @return bool TRUE If at least one element matches, FALSE if value is not in any string of the map
2491
         * @deprecated Use multi-byte aware strContains() instead
2492
         */
2493
        public function inString( $value, bool $case = true ) : bool
2494
        {
2495
                $fcn = $case ? 'strpos' : 'stripos';
8✔
2496

2497
                foreach( (array) $value as $val )
8✔
2498
                {
2499
                        if( (string) $val === '' ) {
8✔
2500
                                return true;
8✔
2501
                        }
2502

2503
                        foreach( $this->list() as $item )
8✔
2504
                        {
2505
                                if( is_scalar( $item ) && $fcn( (string) $item, (string) $val ) !== false ) {
8✔
2506
                                        return true;
8✔
2507
                                }
2508
                        }
2509
                }
2510

2511
                return false;
8✔
2512
        }
2513

2514

2515
        /**
2516
         * Returns an element by key and casts it to integer if possible.
2517
         *
2518
         * Examples:
2519
         *  Map::from( ['a' => true] )->int( 'a' );
2520
         *  Map::from( ['a' => '1'] )->int( 'a' );
2521
         *  Map::from( ['a' => 1.1] )->int( 'a' );
2522
         *  Map::from( ['a' => '10'] )->int( 'a' );
2523
         *  Map::from( ['a' => ['b' => ['c' => 1]]] )->int( 'a/b/c' );
2524
         *  Map::from( [] )->int( 'c', function() { return rand( 1, 1 ); } );
2525
         *  Map::from( [] )->int( 'a', 1 );
2526
         *
2527
         *  Map::from( [] )->int( 'b' );
2528
         *  Map::from( ['b' => ''] )->int( 'b' );
2529
         *  Map::from( ['b' => 'abc'] )->int( 'b' );
2530
         *  Map::from( ['b' => null] )->int( 'b' );
2531
         *  Map::from( ['b' => [1]] )->int( 'b' );
2532
         *  Map::from( ['b' => #resource] )->int( 'b' );
2533
         *  Map::from( ['b' => new \stdClass] )->int( 'b' );
2534
         *
2535
         *  Map::from( [] )->int( 'c', new \Exception( 'error' ) );
2536
         *
2537
         * Results:
2538
         * The first seven examples will return 1 while the 8th to 14th example
2539
         * returns 0. The last example will throw an exception.
2540
         *
2541
         * This does also work for multi-dimensional arrays by passing the keys
2542
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
2543
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
2544
         * public properties of objects or objects implementing __isset() and __get() methods.
2545
         *
2546
         * @param int|string $key Key or path to the requested item
2547
         * @param mixed $default Default value if key isn't found (will be casted to integer)
2548
         * @return int Value from map or default value
2549
         */
2550
        public function int( $key, $default = 0 ) : int
2551
        {
2552
                return (int) ( is_scalar( $val = $this->get( $key, $default ) ) ? $val : $default );
24✔
2553
        }
2554

2555

2556
        /**
2557
         * Returns all values in a new map that are available in both, the map and the given elements.
2558
         *
2559
         * Examples:
2560
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->intersect( ['bar'] );
2561
         *
2562
         * Results:
2563
         *  ['b' => 'bar']
2564
         *
2565
         * If a callback is passed, the given function will be used to compare the values.
2566
         * The function must accept two parameters (value A and B) and must return
2567
         * -1 if value A is smaller than value B, 0 if both are equal and 1 if value A is
2568
         * greater than value B. Both, a method name and an anonymous function can be passed:
2569
         *
2570
         *  Map::from( [0 => 'a'] )->intersect( [0 => 'A'], 'strcasecmp' );
2571
         *  Map::from( ['b' => 'a'] )->intersect( ['B' => 'A'], 'strcasecmp' );
2572
         *  Map::from( ['b' => 'a'] )->intersect( ['c' => 'A'], function( $valA, $valB ) {
2573
         *      return strtolower( $valA ) <=> strtolower( $valB );
2574
         *  } );
2575
         *
2576
         * All examples will return a map containing ['a'] because both contain the same
2577
         * values when compared case insensitive.
2578
         *
2579
         * The keys are preserved using this method.
2580
         *
2581
         * @param iterable<int|string,mixed> $elements List of elements
2582
         * @param  callable|null $callback Function with (valueA, valueB) parameters and returns -1 (<), 0 (=) and 1 (>)
2583
         * @return self<int|string,mixed> New map
2584
         */
2585
        public function intersect( iterable $elements, ?callable $callback = null ) : self
2586
        {
2587
                $list = $this->list();
16✔
2588
                $elements = $this->array( $elements );
16✔
2589

2590
                if( $callback ) {
16✔
2591
                        return new static( array_uintersect( $list, $elements, $callback ) );
8✔
2592
                }
2593

2594
                return new static( array_intersect( $list, $elements ) );
8✔
2595
        }
2596

2597

2598
        /**
2599
         * Returns all values in a new map that are available in both, the map and the given elements while comparing the keys too.
2600
         *
2601
         * Examples:
2602
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->intersectAssoc( new Map( ['foo', 'b' => 'bar'] ) );
2603
         *
2604
         * Results:
2605
         *  ['a' => 'foo']
2606
         *
2607
         * If a callback is passed, the given function will be used to compare the values.
2608
         * The function must accept two parameters (value A and B) and must return
2609
         * -1 if value A is smaller than value B, 0 if both are equal and 1 if value A is
2610
         * greater than value B. Both, a method name and an anonymous function can be passed:
2611
         *
2612
         *  Map::from( [0 => 'a'] )->intersectAssoc( [0 => 'A'], 'strcasecmp' );
2613
         *  Map::from( ['b' => 'a'] )->intersectAssoc( ['B' => 'A'], 'strcasecmp' );
2614
         *  Map::from( ['b' => 'a'] )->intersectAssoc( ['c' => 'A'], function( $valA, $valB ) {
2615
         *      return strtolower( $valA ) <=> strtolower( $valB );
2616
         *  } );
2617
         *
2618
         * The first example will return [0 => 'a'] because both contain the same
2619
         * values when compared case insensitive. The second and third example will return
2620
         * an empty map because the keys doesn't match ("b" vs. "B" and "b" vs. "c").
2621
         *
2622
         * The keys are preserved using this method.
2623
         *
2624
         * @param iterable<int|string,mixed> $elements List of elements
2625
         * @param  callable|null $callback Function with (valueA, valueB) parameters and returns -1 (<), 0 (=) and 1 (>)
2626
         * @return self<int|string,mixed> New map
2627
         */
2628
        public function intersectAssoc( iterable $elements, ?callable $callback = null ) : self
2629
        {
2630
                $elements = $this->array( $elements );
40✔
2631

2632
                if( $callback ) {
40✔
2633
                        return new static( array_uintersect_assoc( $this->list(), $elements, $callback ) );
8✔
2634
                }
2635

2636
                return new static( array_intersect_assoc( $this->list(), $elements ) );
32✔
2637
        }
2638

2639

2640
        /**
2641
         * Returns all values in a new map that are available in both, the map and the given elements by comparing the keys only.
2642
         *
2643
         * Examples:
2644
         *  Map::from( ['a' => 'foo', 'b' => 'bar'] )->intersectKeys( new Map( ['foo', 'b' => 'baz'] ) );
2645
         *
2646
         * Results:
2647
         *  ['b' => 'bar']
2648
         *
2649
         * If a callback is passed, the given function will be used to compare the keys.
2650
         * The function must accept two parameters (key A and B) and must return
2651
         * -1 if key A is smaller than key B, 0 if both are equal and 1 if key A is
2652
         * greater than key B. Both, a method name and an anonymous function can be passed:
2653
         *
2654
         *  Map::from( [0 => 'a'] )->intersectKeys( [0 => 'A'], 'strcasecmp' );
2655
         *  Map::from( ['b' => 'a'] )->intersectKeys( ['B' => 'X'], 'strcasecmp' );
2656
         *  Map::from( ['b' => 'a'] )->intersectKeys( ['c' => 'a'], function( $keyA, $keyB ) {
2657
         *      return strtolower( $keyA ) <=> strtolower( $keyB );
2658
         *  } );
2659
         *
2660
         * The first example will return a map with [0 => 'a'] and the second one will
2661
         * return a map with ['b' => 'a'] because both contain the same keys when compared
2662
         * case insensitive. The third example will return an empty map because the keys
2663
         * doesn't match ("b" vs. "c").
2664
         *
2665
         * The keys are preserved using this method.
2666
         *
2667
         * @param iterable<int|string,mixed> $elements List of elements
2668
         * @param  callable|null $callback Function with (keyA, keyB) parameters and returns -1 (<), 0 (=) and 1 (>)
2669
         * @return self<int|string,mixed> New map
2670
         */
2671
        public function intersectKeys( iterable $elements, ?callable $callback = null ) : self
2672
        {
2673
                $elements = $this->array( $elements );
24✔
2674

2675
                if( $callback ) {
24✔
2676
                        return new static( array_intersect_ukey( $this->list(), $elements, $callback ) );
8✔
2677
                }
2678

2679
                return new static( array_intersect_key( $this->list(), $elements ) );
16✔
2680
        }
2681

2682

2683
        /**
2684
         * Tests if the map consists of the same keys and values
2685
         *
2686
         * Examples:
2687
         *  Map::from( ['a', 'b'] )->is( ['b', 'a'] );
2688
         *  Map::from( ['a', 'b'] )->is( ['b', 'a'], true );
2689
         *  Map::from( [1, 2] )->is( ['1', '2'] );
2690
         *
2691
         * Results:
2692
         *  The first example returns TRUE while the second and third one returns FALSE
2693
         *
2694
         * @param iterable<int|string,mixed> $list List of key/value pairs to compare with
2695
         * @param bool $strict TRUE for comparing order of elements too, FALSE for key/values only
2696
         * @return bool TRUE if given list is equal, FALSE if not
2697
         */
2698
        public function is( iterable $list, bool $strict = false ) : bool
2699
        {
2700
                $list = $this->array( $list );
24✔
2701

2702
                if( $strict ) {
24✔
2703
                        return $this->list() === $list;
16✔
2704
                }
2705

2706
                return $this->list() == $list;
8✔
2707
        }
2708

2709

2710
        /**
2711
         * Determines if the map is empty or not.
2712
         *
2713
         * Examples:
2714
         *  Map::from( [] )->isEmpty();
2715
         *  Map::from( ['a'] )->isEmpty();
2716
         *
2717
         * Results:
2718
         *  The first example returns TRUE while the second returns FALSE
2719
         *
2720
         * The method is equivalent to empty().
2721
         *
2722
         * @return bool TRUE if map is empty, FALSE if not
2723
         */
2724
        public function isEmpty() : bool
2725
        {
2726
                return empty( $this->list() );
32✔
2727
        }
2728

2729

2730
        /**
2731
         * Checks if the map contains a list of subsequentially numbered keys.
2732
         *
2733
         * Examples:
2734
         * Map::from( [] )->isList();
2735
         * Map::from( [1, 3, 2] )->isList();
2736
         * Map::from( [0 => 1, 1 => 2, 2 => 3] )->isList();
2737
         * Map::from( [1 => 1, 2 => 2, 3 => 3] )->isList();
2738
         * Map::from( [0 => 1, 2 => 2, 3 => 3] )->isList();
2739
         * Map::from( ['a' => 1, 1 => 2, 'c' => 3] )->isList();
2740
         *
2741
         * Results:
2742
         * The first three examples return TRUE while the last three return FALSE
2743
         *
2744
         * @return bool TRUE if the map is a list, FALSE if not
2745
         */
2746
        public function isList() : bool
2747
        {
2748
                $i = -1;
8✔
2749

2750
                foreach( $this->list() as $k => $v )
8✔
2751
                {
2752
                        if( $k !== ++$i ) {
8✔
2753
                                return false;
8✔
2754
                        }
2755
                }
2756

2757
                return true;
8✔
2758
        }
2759

2760

2761
        /**
2762
         * Determines if all entries are numeric values.
2763
         *
2764
         * Examples:
2765
         *  Map::from( [] )->isNumeric();
2766
         *  Map::from( [1] )->isNumeric();
2767
         *  Map::from( [1.1] )->isNumeric();
2768
         *  Map::from( [010] )->isNumeric();
2769
         *  Map::from( [0x10] )->isNumeric();
2770
         *  Map::from( [0b10] )->isNumeric();
2771
         *  Map::from( ['010'] )->isNumeric();
2772
         *  Map::from( ['10'] )->isNumeric();
2773
         *  Map::from( ['10.1'] )->isNumeric();
2774
         *  Map::from( [' 10 '] )->isNumeric();
2775
         *  Map::from( ['10e2'] )->isNumeric();
2776
         *  Map::from( ['0b10'] )->isNumeric();
2777
         *  Map::from( ['0x10'] )->isNumeric();
2778
         *  Map::from( ['null'] )->isNumeric();
2779
         *  Map::from( [null] )->isNumeric();
2780
         *  Map::from( [true] )->isNumeric();
2781
         *  Map::from( [[]] )->isNumeric();
2782
         *  Map::from( [''] )->isNumeric();
2783
         *
2784
         * Results:
2785
         *  The first eleven examples return TRUE while the last seven return FALSE
2786
         *
2787
         * @return bool TRUE if all map entries are numeric values, FALSE if not
2788
         */
2789
        public function isNumeric() : bool
2790
        {
2791
                foreach( $this->list() as $val )
8✔
2792
                {
2793
                        if( !is_numeric( $val ) ) {
8✔
2794
                                return false;
8✔
2795
                        }
2796
                }
2797

2798
                return true;
8✔
2799
        }
2800

2801

2802
        /**
2803
         * Determines if all entries are objects.
2804
         *
2805
         * Examples:
2806
         *  Map::from( [] )->isObject();
2807
         *  Map::from( [new stdClass] )->isObject();
2808
         *  Map::from( [1] )->isObject();
2809
         *
2810
         * Results:
2811
         *  The first two examples return TRUE while the last one return FALSE
2812
         *
2813
         * @return bool TRUE if all map entries are objects, FALSE if not
2814
         */
2815
        public function isObject() : bool
2816
        {
2817
                foreach( $this->list() as $val )
8✔
2818
                {
2819
                        if( !is_object( $val ) ) {
8✔
2820
                                return false;
8✔
2821
                        }
2822
                }
2823

2824
                return true;
8✔
2825
        }
2826

2827

2828
        /**
2829
         * Determines if all entries are scalar values.
2830
         *
2831
         * Examples:
2832
         *  Map::from( [] )->isScalar();
2833
         *  Map::from( [1] )->isScalar();
2834
         *  Map::from( [1.1] )->isScalar();
2835
         *  Map::from( ['abc'] )->isScalar();
2836
         *  Map::from( [true, false] )->isScalar();
2837
         *  Map::from( [new stdClass] )->isScalar();
2838
         *  Map::from( [#resource] )->isScalar();
2839
         *  Map::from( [null] )->isScalar();
2840
         *  Map::from( [[1]] )->isScalar();
2841
         *
2842
         * Results:
2843
         *  The first five examples return TRUE while the others return FALSE
2844
         *
2845
         * @return bool TRUE if all map entries are scalar values, FALSE if not
2846
         */
2847
        public function isScalar() : bool
2848
        {
2849
                foreach( $this->list() as $val )
8✔
2850
                {
2851
                        if( !is_scalar( $val ) ) {
8✔
2852
                                return false;
8✔
2853
                        }
2854
                }
2855

2856
                return true;
8✔
2857
        }
2858

2859

2860
        /**
2861
         * Determines if all entries are string values.
2862
         *
2863
         * Examples:
2864
         *  Map::from( ['abc'] )->isString();
2865
         *  Map::from( [] )->isString();
2866
         *  Map::from( [1] )->isString();
2867
         *  Map::from( [1.1] )->isString();
2868
         *  Map::from( [true, false] )->isString();
2869
         *  Map::from( [new stdClass] )->isString();
2870
         *  Map::from( [#resource] )->isString();
2871
         *  Map::from( [null] )->isString();
2872
         *  Map::from( [[1]] )->isString();
2873
         *
2874
         * Results:
2875
         *  The first two examples return TRUE while the others return FALSE
2876
         *
2877
         * @return bool TRUE if all map entries are string values, FALSE if not
2878
         */
2879
        public function isString() : bool
2880
        {
2881
                foreach( $this->list() as $val )
8✔
2882
                {
2883
                        if( !is_string( $val ) ) {
8✔
2884
                                return false;
8✔
2885
                        }
2886
                }
2887

2888
                return true;
8✔
2889
        }
2890

2891

2892
        /**
2893
         * Concatenates the string representation of all elements.
2894
         *
2895
         * Objects that implement __toString() does also work, otherwise (and in case
2896
         * of arrays) a PHP notice is generated. NULL and FALSE values are treated as
2897
         * empty strings.
2898
         *
2899
         * Examples:
2900
         *  Map::from( ['a', 'b', false] )->join();
2901
         *  Map::from( ['a', 'b', null, false] )->join( '-' );
2902
         *
2903
         * Results:
2904
         * The first example will return "ab" while the second one will return "a-b--"
2905
         *
2906
         * @param string $glue Character or string added between elements
2907
         * @return string String of concatenated map elements
2908
         */
2909
        public function join( string $glue = '' ) : string
2910
        {
2911
                return implode( $glue, $this->list() );
8✔
2912
        }
2913

2914

2915
        /**
2916
         * Specifies the data which should be serialized to JSON by json_encode().
2917
         *
2918
         * Examples:
2919
         *   json_encode( Map::from( ['a', 'b'] ) );
2920
         *   json_encode( Map::from( ['a' => 0, 'b' => 1] ) );
2921
         *
2922
         * Results:
2923
         *   ["a", "b"]
2924
         *   {"a":0,"b":1}
2925
         *
2926
         * @return array<int|string,mixed> Data to serialize to JSON
2927
         */
2928
        #[\ReturnTypeWillChange]
2929
        public function jsonSerialize()
2930
        {
2931
                return $this->list = $this->array( $this->list );
8✔
2932
        }
2933

2934

2935
        /**
2936
         * Returns the keys of the all elements in a new map object.
2937
         *
2938
         * Examples:
2939
         *  Map::from( ['a', 'b'] );
2940
         *  Map::from( ['a' => 0, 'b' => 1] );
2941
         *
2942
         * Results:
2943
         * The first example returns a map containing [0, 1] while the second one will
2944
         * return a map with ['a', 'b'].
2945
         *
2946
         * @return self<int|string,mixed> New map
2947
         */
2948
        public function keys() : self
2949
        {
2950
                return new static( array_keys( $this->list() ) );
8✔
2951
        }
2952

2953

2954
        /**
2955
         * Sorts the elements by their keys in reverse order.
2956
         *
2957
         * Examples:
2958
         *  Map::from( ['b' => 0, 'a' => 1] )->krsort();
2959
         *  Map::from( [1 => 'a', 0 => 'b'] )->krsort();
2960
         *
2961
         * Results:
2962
         *  ['a' => 1, 'b' => 0]
2963
         *  [0 => 'b', 1 => 'a']
2964
         *
2965
         * The parameter modifies how the keys are compared. Possible values are:
2966
         * - SORT_REGULAR : compare elements normally (don't change types)
2967
         * - SORT_NUMERIC : compare elements numerically
2968
         * - SORT_STRING : compare elements as strings
2969
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
2970
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
2971
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
2972
         *
2973
         * The keys are preserved using this method and no new map is created.
2974
         *
2975
         * @param int $options Sort options for krsort()
2976
         * @return self<int|string,mixed> Updated map for fluid interface
2977
         */
2978
        public function krsort( int $options = SORT_REGULAR ) : self
2979
        {
2980
                krsort( $this->list(), $options );
24✔
2981
                return $this;
24✔
2982
        }
2983

2984

2985
        /**
2986
         * Sorts a copy of the elements by their keys in reverse order.
2987
         *
2988
         * Examples:
2989
         *  Map::from( ['b' => 0, 'a' => 1] )->krsorted();
2990
         *  Map::from( [1 => 'a', 0 => 'b'] )->krsorted();
2991
         *
2992
         * Results:
2993
         *  ['a' => 1, 'b' => 0]
2994
         *  [0 => 'b', 1 => 'a']
2995
         *
2996
         * The parameter modifies how the keys are compared. Possible values are:
2997
         * - SORT_REGULAR : compare elements normally (don't change types)
2998
         * - SORT_NUMERIC : compare elements numerically
2999
         * - SORT_STRING : compare elements as strings
3000
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
3001
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
3002
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
3003
         *
3004
         * The keys are preserved using this method and a new map is created.
3005
         *
3006
         * @param int $options Sort options for krsort()
3007
         * @return self<int|string,mixed> Updated map for fluid interface
3008
         */
3009
        public function krsorted( int $options = SORT_REGULAR ) : self
3010
        {
3011
                return ( clone $this )->krsort();
8✔
3012
        }
3013

3014

3015
        /**
3016
         * Sorts the elements by their keys.
3017
         *
3018
         * Examples:
3019
         *  Map::from( ['b' => 0, 'a' => 1] )->ksort();
3020
         *  Map::from( [1 => 'a', 0 => 'b'] )->ksort();
3021
         *
3022
         * Results:
3023
         *  ['a' => 1, 'b' => 0]
3024
         *  [0 => 'b', 1 => 'a']
3025
         *
3026
         * The parameter modifies how the keys are compared. Possible values are:
3027
         * - SORT_REGULAR : compare elements normally (don't change types)
3028
         * - SORT_NUMERIC : compare elements numerically
3029
         * - SORT_STRING : compare elements as strings
3030
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
3031
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
3032
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
3033
         *
3034
         * The keys are preserved using this method and no new map is created.
3035
         *
3036
         * @param int $options Sort options for ksort()
3037
         * @return self<int|string,mixed> Updated map for fluid interface
3038
         */
3039
        public function ksort( int $options = SORT_REGULAR ) : self
3040
        {
3041
                ksort( $this->list(), $options );
24✔
3042
                return $this;
24✔
3043
        }
3044

3045

3046
        /**
3047
         * Sorts a copy of the elements by their keys.
3048
         *
3049
         * Examples:
3050
         *  Map::from( ['b' => 0, 'a' => 1] )->ksorted();
3051
         *  Map::from( [1 => 'a', 0 => 'b'] )->ksorted();
3052
         *
3053
         * Results:
3054
         *  ['a' => 1, 'b' => 0]
3055
         *  [0 => 'b', 1 => 'a']
3056
         *
3057
         * The parameter modifies how the keys are compared. Possible values are:
3058
         * - SORT_REGULAR : compare elements normally (don't change types)
3059
         * - SORT_NUMERIC : compare elements numerically
3060
         * - SORT_STRING : compare elements as strings
3061
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
3062
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
3063
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
3064
         *
3065
         * The keys are preserved using this method and no new map is created.
3066
         *
3067
         * @param int $options Sort options for ksort()
3068
         * @return self<int|string,mixed> Updated map for fluid interface
3069
         */
3070
        public function ksorted( int $options = SORT_REGULAR ) : self
3071
        {
3072
                return ( clone $this )->ksort();
8✔
3073
        }
3074

3075

3076
        /**
3077
         * Returns the last element from the map.
3078
         *
3079
         * Examples:
3080
         *  Map::from( ['a', 'b'] )->last();
3081
         *  Map::from( [] )->last( 'x' );
3082
         *  Map::from( [] )->last( new \Exception( 'error' ) );
3083
         *  Map::from( [] )->last( function() { return rand(); } );
3084
         *
3085
         * Results:
3086
         * The first example will return 'b' and the second one 'x'. The third example
3087
         * will throw the exception passed if the map contains no elements. In the
3088
         * fourth example, a random value generated by the closure function will be
3089
         * returned.
3090
         *
3091
         * Using this method doesn't affect the internal array pointer.
3092
         *
3093
         * @param mixed $default Default value or exception if the map contains no elements
3094
         * @return mixed Last value of map, (generated) default value or an exception
3095
         */
3096
        public function last( $default = null )
3097
        {
3098
                if( !empty( $this->list() ) ) {
48✔
3099
                        return current( array_slice( $this->list(), -1, 1 ) );
16✔
3100
                }
3101

3102
                if( $default instanceof \Closure ) {
32✔
3103
                        return $default();
8✔
3104
                }
3105

3106
                if( $default instanceof \Throwable ) {
24✔
3107
                        throw $default;
8✔
3108
                }
3109

3110
                return $default;
16✔
3111
        }
3112

3113

3114
        /**
3115
         * Returns the last key from the map.
3116
         *
3117
         * Examples:
3118
         *  Map::from( ['a' => 1, 'b' => 2] )->lastKey();
3119
         *  Map::from( [] )->lastKey( 'x' );
3120
         *  Map::from( [] )->lastKey( new \Exception( 'error' ) );
3121
         *  Map::from( [] )->lastKey( function() { return rand(); } );
3122
         *
3123
         * Results:
3124
         * The first example will return 'a' and the second one 'x', the third one will throw
3125
         * an exception and the last one will return a random value.
3126
         *
3127
         * Using this method doesn't affect the internal array pointer.
3128
         *
3129
         * @param mixed $default Default value, closure or exception if the map contains no elements
3130
         * @return mixed Last key of map, (generated) default value or an exception
3131
         */
3132
        public function lastKey( $default = null )
3133
        {
3134
                $list = $this->list();
40✔
3135

3136
                // PHP 7.x compatibility
3137
                if( function_exists( 'array_key_last' ) ) {
40✔
3138
                        $key = array_key_last( $list );
40✔
3139
                } else {
3140
                        $key = key( array_slice( $list, -1, 1, true ) );
×
3141
                }
3142

3143
                if( $key !== null ) {
40✔
3144
                        return $key;
8✔
3145
                }
3146

3147
                if( $default instanceof \Closure ) {
32✔
3148
                        return $default();
8✔
3149
                }
3150

3151
                if( $default instanceof \Throwable ) {
24✔
3152
                        throw $default;
8✔
3153
                }
3154

3155
                return $default;
16✔
3156
        }
3157

3158

3159
        /**
3160
         * Removes the passed characters from the left of all strings.
3161
         *
3162
         * Examples:
3163
         *  Map::from( [" abc\n", "\tcde\r\n"] )->ltrim();
3164
         *  Map::from( ["a b c", "cbxa"] )->ltrim( 'abc' );
3165
         *
3166
         * Results:
3167
         * The first example will return ["abc\n", "cde\r\n"] while the second one will return [" b c", "xa"].
3168
         *
3169
         * @param string $chars List of characters to trim
3170
         * @return self<int|string,mixed> Updated map for fluid interface
3171
         */
3172
        public function ltrim( string $chars = " \n\r\t\v\x00" ) : self
3173
        {
3174
                foreach( $this->list() as &$entry )
8✔
3175
                {
3176
                        if( is_string( $entry ) ) {
8✔
3177
                                $entry = ltrim( $entry, $chars );
8✔
3178
                        }
3179
                }
3180

3181
                return $this;
8✔
3182
        }
3183

3184

3185
        /**
3186
         * Maps new values to the existing keys using the passed function and returns a new map for the result.
3187
         *
3188
         * Examples:
3189
         *  Map::from( ['a' => 2, 'b' => 4] )->map( function( $value, $key ) {
3190
         *      return $value * 2;
3191
         *  } );
3192
         *
3193
         * Results:
3194
         *  ['a' => 4, 'b' => 8]
3195
         *
3196
         * The keys are preserved using this method.
3197
         *
3198
         * @param callable $callback Function with (value, key) parameters and returns computed result
3199
         * @return self<int|string,mixed> New map with the original keys and the computed values
3200
         * @see rekey() - Changes the keys according to the passed function
3201
         * @see transform() - Creates new key/value pairs using the passed function and returns a new map for the result
3202
         */
3203
        public function map( callable $callback ) : self
3204
        {
3205
                $list = $this->list();
8✔
3206
                $keys = array_keys( $list );
8✔
3207
                $map = array_map( $callback, array_values( $list ), $keys );
8✔
3208

3209
                return new static( array_combine( $keys, $map ) );
8✔
3210
        }
3211

3212

3213
        /**
3214
         * Returns the maximum value of all elements.
3215
         *
3216
         * Examples:
3217
         *  Map::from( [1, 3, 2, 5, 4] )->max()
3218
         *  Map::from( ['bar', 'foo', 'baz'] )->max()
3219
         *  Map::from( [['p' => 30], ['p' => 50], ['p' => 10]] )->max( 'p' )
3220
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->max( 'i/p' )
3221
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->max( fn( $val, $key ) => $val['i']['p'] ?? null )
3222
         *  Map::from( [50, 10, 30] )->max( fn( $val, $key ) => $key > 0 ? $val : null )
3223
         *
3224
         * Results:
3225
         * The first line will return "5", the second one "foo" and the third to fitfh
3226
         * one return 50 while the last one will return 30.
3227
         *
3228
         * NULL values are removed before the comparison. If there are no values or all
3229
         * values are NULL, NULL is returned.
3230
         *
3231
         * This does also work for multi-dimensional arrays by passing the keys
3232
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
3233
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
3234
         * public properties of objects or objects implementing __isset() and __get() methods.
3235
         *
3236
         * Be careful comparing elements of different types because this can have
3237
         * unpredictable results due to the PHP comparison rules:
3238
         * {@link https://www.php.net/manual/en/language.operators.comparison.php}
3239
         *
3240
         * @param Closure|string|null $col Closure, key or path to the value of the nested array or object
3241
         * @return mixed Maximum value or NULL if there are no elements in the map
3242
         */
3243
        public function max( $col = null )
3244
        {
3245
                $list = $this->list();
32✔
3246
                $vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list );
32✔
3247

3248
                return !empty( $vals ) ? max( $vals ) : null;
32✔
3249
        }
3250

3251

3252
        /**
3253
         * Merges the map with the given elements without returning a new map.
3254
         *
3255
         * Elements with the same non-numeric keys will be overwritten, elements
3256
         * with the same numeric keys will be added.
3257
         *
3258
         * Examples:
3259
         *  Map::from( ['a', 'b'] )->merge( ['b', 'c'] );
3260
         *  Map::from( ['a' => 1, 'b' => 2] )->merge( ['b' => 4, 'c' => 6] );
3261
         *  Map::from( ['a' => 1, 'b' => 2] )->merge( ['b' => 4, 'c' => 6], true );
3262
         *
3263
         * Results:
3264
         *  ['a', 'b', 'b', 'c']
3265
         *  ['a' => 1, 'b' => 4, 'c' => 6]
3266
         *  ['a' => 1, 'b' => [2, 4], 'c' => 6]
3267
         *
3268
         * The method is similar to replace() but doesn't replace elements with
3269
         * the same numeric keys. If you want to be sure that all passed elements
3270
         * are added without replacing existing ones, use concat() instead.
3271
         *
3272
         * The keys are preserved using this method.
3273
         *
3274
         * @param iterable<int|string,mixed> $elements List of elements
3275
         * @param bool $recursive TRUE to merge nested arrays too, FALSE for first level elements only
3276
         * @return self<int|string,mixed> Updated map for fluid interface
3277
         */
3278
        public function merge( iterable $elements, bool $recursive = false ) : self
3279
        {
3280
                if( $recursive ) {
24✔
3281
                        $this->list = array_merge_recursive( $this->list(), $this->array( $elements ) );
8✔
3282
                } else {
3283
                        $this->list = array_merge( $this->list(), $this->array( $elements ) );
16✔
3284
                }
3285

3286
                return $this;
24✔
3287
        }
3288

3289

3290
        /**
3291
         * Returns the minimum value of all elements.
3292
         *
3293
         * Examples:
3294
         *  Map::from( [2, 3, 1, 5, 4] )->min()
3295
         *  Map::from( ['baz', 'foo', 'bar'] )->min()
3296
         *  Map::from( [['p' => 30], ['p' => 50], ['p' => 10]] )->min( 'p' )
3297
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->min( 'i/p' )
3298
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->min( fn( $val, $key ) => $val['i']['p'] ?? null )
3299
         *  Map::from( [10, 50, 30] )->min( fn( $val, $key ) => $key > 0 ? $val : null )
3300
         *
3301
         * Results:
3302
         * The first line will return "1", the second one "bar", the third one
3303
         * 10, and the forth to the last one 30.
3304
         *
3305
         * NULL values are removed before the comparison. If there are no values or all
3306
         * values are NULL, NULL is returned.
3307
         *
3308
         * This does also work for multi-dimensional arrays by passing the keys
3309
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
3310
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
3311
         * public properties of objects or objects implementing __isset() and __get() methods.
3312
         *
3313
         * Be careful comparing elements of different types because this can have
3314
         * unpredictable results due to the PHP comparison rules:
3315
         * {@link https://www.php.net/manual/en/language.operators.comparison.php}
3316
         *
3317
         * @param Closure|string|null $key Closure, key or path to the value of the nested array or object
3318
         * @return mixed Minimum value or NULL if there are no elements in the map
3319
         */
3320
        public function min( $col = null )
3321
        {
3322
                $list = $this->list();
32✔
3323
                $vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list );
32✔
3324

3325
                return !empty( $vals ) ? min( $vals ) : null;
32✔
3326
        }
3327

3328

3329
        /**
3330
         * Tests if none of the elements are part of the map.
3331
         *
3332
         * Examples:
3333
         *  Map::from( ['a', 'b'] )->none( 'x' );
3334
         *  Map::from( ['a', 'b'] )->none( ['x', 'y'] );
3335
         *  Map::from( ['1', '2'] )->none( 2, true );
3336
         *  Map::from( ['a', 'b'] )->none( 'a' );
3337
         *  Map::from( ['a', 'b'] )->none( ['a', 'b'] );
3338
         *  Map::from( ['a', 'b'] )->none( ['a', 'x'] );
3339
         *
3340
         * Results:
3341
         * The first three examples will return TRUE while the other ones will return FALSE
3342
         *
3343
         * @param mixed|array $element Element or elements to search for in the map
3344
         * @param bool $strict TRUE to check the type too, using FALSE '1' and 1 will be the same
3345
         * @return bool TRUE if none of the elements is part of the map, FALSE if at least one is
3346
         */
3347
        public function none( $element, bool $strict = false ) : bool
3348
        {
3349
                $list = $this->list();
8✔
3350

3351
                if( !is_array( $element ) ) {
8✔
3352
                        return !in_array( $element, $list, $strict );
8✔
3353
                };
3354

3355
                foreach( $element as $entry )
8✔
3356
                {
3357
                        if( in_array( $entry, $list, $strict ) === true ) {
8✔
3358
                                return false;
8✔
3359
                        }
3360
                }
3361

3362
                return true;
8✔
3363
        }
3364

3365

3366
        /**
3367
         * Returns every nth element from the map.
3368
         *
3369
         * Examples:
3370
         *  Map::from( ['a', 'b', 'c', 'd', 'e', 'f'] )->nth( 2 );
3371
         *  Map::from( ['a', 'b', 'c', 'd', 'e', 'f'] )->nth( 2, 1 );
3372
         *
3373
         * Results:
3374
         *  ['a', 'c', 'e']
3375
         *  ['b', 'd', 'f']
3376
         *
3377
         * @param int $step Step width
3378
         * @param int $offset Number of element to start from (0-based)
3379
         * @return self<int|string,mixed> New map
3380
         */
3381
        public function nth( int $step, int $offset = 0 ) : self
3382
        {
3383
                if( $step < 1 ) {
24✔
3384
                        throw new \InvalidArgumentException( 'Step width must be greater than zero' );
8✔
3385
                }
3386

3387
                if( $step === 1 ) {
16✔
3388
                        return clone $this;
8✔
3389
                }
3390

3391
                $result = [];
8✔
3392
                $list = $this->list();
8✔
3393

3394
                while( !empty( $pair = array_slice( $list, $offset, 1, true ) ) )
8✔
3395
                {
3396
                        $result += $pair;
8✔
3397
                        $offset += $step;
8✔
3398
                }
3399

3400
                return new static( $result );
8✔
3401
        }
3402

3403

3404
        /**
3405
         * Determines if an element exists at an offset.
3406
         *
3407
         * Examples:
3408
         *  $map = Map::from( ['a' => 1, 'b' => 3, 'c' => null] );
3409
         *  isset( $map['b'] );
3410
         *  isset( $map['c'] );
3411
         *  isset( $map['d'] );
3412
         *
3413
         * Results:
3414
         *  The first isset() will return TRUE while the second and third one will return FALSE
3415
         *
3416
         * @param int|string $key Key to check for
3417
         * @return bool TRUE if key exists, FALSE if not
3418
         */
3419
        public function offsetExists( $key ) : bool
3420
        {
3421
                return isset( $this->list()[$key] );
56✔
3422
        }
3423

3424

3425
        /**
3426
         * Returns an element at a given offset.
3427
         *
3428
         * Examples:
3429
         *  $map = Map::from( ['a' => 1, 'b' => 3] );
3430
         *  $map['b'];
3431
         *
3432
         * Results:
3433
         *  $map['b'] will return 3
3434
         *
3435
         * @param int|string $key Key to return the element for
3436
         * @return mixed Value associated to the given key
3437
         */
3438
        #[\ReturnTypeWillChange]
3439
        public function offsetGet( $key )
3440
        {
3441
                return $this->list()[$key] ?? null;
40✔
3442
        }
3443

3444

3445
        /**
3446
         * Sets the element at a given offset.
3447
         *
3448
         * Examples:
3449
         *  $map = Map::from( ['a' => 1] );
3450
         *  $map['b'] = 2;
3451
         *  $map[0] = 4;
3452
         *
3453
         * Results:
3454
         *  ['a' => 1, 'b' => 2, 0 => 4]
3455
         *
3456
         * @param int|string|null $key Key to set the element for or NULL to append value
3457
         * @param mixed $value New value set for the key
3458
         */
3459
        public function offsetSet( $key, $value ) : void
3460
        {
3461
                if( $key !== null ) {
24✔
3462
                        $this->list()[$key] = $value;
16✔
3463
                } else {
3464
                        $this->list()[] = $value;
16✔
3465
                }
3466
        }
6✔
3467

3468

3469
        /**
3470
         * Unsets the element at a given offset.
3471
         *
3472
         * Examples:
3473
         *  $map = Map::from( ['a' => 1] );
3474
         *  unset( $map['a'] );
3475
         *
3476
         * Results:
3477
         *  The map will be empty
3478
         *
3479
         * @param int|string $key Key for unsetting the item
3480
         */
3481
        public function offsetUnset( $key ) : void
3482
        {
3483
                unset( $this->list()[$key] );
16✔
3484
        }
4✔
3485

3486

3487
        /**
3488
         * Returns a new map with only those elements specified by the given keys.
3489
         *
3490
         * Examples:
3491
         *  Map::from( ['a' => 1, 0 => 'b'] )->only( 'a' );
3492
         *  Map::from( ['a' => 1, 0 => 'b', 1 => 'c'] )->only( [0, 1] );
3493
         *
3494
         * Results:
3495
         *  ['a' => 1]
3496
         *  [0 => 'b', 1 => 'c']
3497
         *
3498
         * The keys are preserved using this method.
3499
         *
3500
         * @param iterable<mixed>|array<mixed>|string|int $keys Keys of the elements that should be returned
3501
         * @return self<int|string,mixed> New map with only the elements specified by the keys
3502
         */
3503
        public function only( $keys ) : self
3504
        {
3505
                return $this->intersectKeys( array_flip( $this->array( $keys ) ) );
8✔
3506
        }
3507

3508

3509
        /**
3510
         * Returns a new map with elements ordered by the passed keys.
3511
         *
3512
         * If there are less keys passed than available in the map, the remaining
3513
         * elements are removed. Otherwise, if keys are passed that are not in the
3514
         * map, they will be also available in the returned map but their value is
3515
         * NULL.
3516
         *
3517
         * Examples:
3518
         *  Map::from( ['a' => 1, 1 => 'c', 0 => 'b'] )->order( [0, 1, 'a'] );
3519
         *  Map::from( ['a' => 1, 1 => 'c', 0 => 'b'] )->order( [0, 1, 2] );
3520
         *  Map::from( ['a' => 1, 1 => 'c', 0 => 'b'] )->order( [0, 1] );
3521
         *
3522
         * Results:
3523
         *  [0 => 'b', 1 => 'c', 'a' => 1]
3524
         *  [0 => 'b', 1 => 'c', 2 => null]
3525
         *  [0 => 'b', 1 => 'c']
3526
         *
3527
         * The keys are preserved using this method.
3528
         *
3529
         * @param iterable<mixed> $keys Keys of the elements in the required order
3530
         * @return self<int|string,mixed> New map with elements ordered by the passed keys
3531
         */
3532
        public function order( iterable $keys ) : self
3533
        {
3534
                $result = [];
8✔
3535
                $list = $this->list();
8✔
3536

3537
                foreach( $keys as $key ) {
8✔
3538
                        $result[$key] = $list[$key] ?? null;
8✔
3539
                }
3540

3541
                return new static( $result );
8✔
3542
        }
3543

3544

3545
        /**
3546
         * Fill up to the specified length with the given value
3547
         *
3548
         * In case the given number is smaller than the number of element that are
3549
         * already in the list, the map is unchanged. If the size is positive, the
3550
         * new elements are padded on the right, if it's negative then the elements
3551
         * are padded on the left.
3552
         *
3553
         * Examples:
3554
         *  Map::from( [1, 2, 3] )->pad( 5 );
3555
         *  Map::from( [1, 2, 3] )->pad( -5 );
3556
         *  Map::from( [1, 2, 3] )->pad( 5, '0' );
3557
         *  Map::from( [1, 2, 3] )->pad( 2 );
3558
         *  Map::from( [10 => 1, 20 => 2] )->pad( 3 );
3559
         *  Map::from( ['a' => 1, 'b' => 2] )->pad( 3, 3 );
3560
         *
3561
         * Results:
3562
         *  [1, 2, 3, null, null]
3563
         *  [null, null, 1, 2, 3]
3564
         *  [1, 2, 3, '0', '0']
3565
         *  [1, 2, 3]
3566
         *  [0 => 1, 1 => 2, 2 => null]
3567
         *  ['a' => 1, 'b' => 2, 0 => 3]
3568
         *
3569
         * Associative keys are preserved, numerical keys are replaced and numerical
3570
         * keys are used for the new elements.
3571
         *
3572
         * @param int $size Total number of elements that should be in the list
3573
         * @param mixed $value Value to fill up with if the map length is smaller than the given size
3574
         * @return self<int|string,mixed> New map
3575
         */
3576
        public function pad( int $size, $value = null ) : self
3577
        {
3578
                return new static( array_pad( $this->list(), $size, $value ) );
8✔
3579
        }
3580

3581

3582
        /**
3583
         * Breaks the list of elements into the given number of groups.
3584
         *
3585
         * Examples:
3586
         *  Map::from( [1, 2, 3, 4, 5] )->partition( 3 );
3587
         *  Map::from( [1, 2, 3, 4, 5] )->partition( function( $val, $idx ) {
3588
         *                return $idx % 3;
3589
         *        } );
3590
         *
3591
         * Results:
3592
         *  [[0 => 1, 1 => 2], [2 => 3, 3 => 4], [4 => 5]]
3593
         *  [0 => [0 => 1, 3 => 4], 1 => [1 => 2, 4 => 5], 2 => [2 => 3]]
3594
         *
3595
         * The keys of the original map are preserved in the returned map.
3596
         *
3597
         * @param \Closure|int $number Function with (value, index) as arguments returning the bucket key or number of groups
3598
         * @return self<int|string,mixed> New map
3599
         */
3600
        public function partition( $number ) : self
3601
        {
3602
                $list = $this->list();
32✔
3603

3604
                if( empty( $list ) ) {
32✔
3605
                        return new static();
8✔
3606
                }
3607

3608
                $result = [];
24✔
3609

3610
                if( $number instanceof \Closure )
24✔
3611
                {
3612
                        foreach( $list as $idx => $item ) {
8✔
3613
                                $result[$number( $item, $idx )][$idx] = $item;
8✔
3614
                        }
3615

3616
                        return new static( $result );
8✔
3617
                }
3618

3619
                if( is_int( $number ) )
16✔
3620
                {
3621
                        $start = 0;
8✔
3622
                        $size = (int) ceil( count( $list ) / $number );
8✔
3623

3624
                        for( $i = 0; $i < $number; $i++ )
8✔
3625
                        {
3626
                                $result[] = array_slice( $list, $start, $size, true );
8✔
3627
                                $start += $size;
8✔
3628
                        }
3629

3630
                        return new static( $result );
8✔
3631
                }
3632

3633
                throw new \InvalidArgumentException( 'Parameter is no closure or integer' );
8✔
3634
        }
3635

3636

3637
        /**
3638
         * Returns the percentage of all elements passing the test in the map.
3639
         *
3640
         * Examples:
3641
         *  Map::from( [30, 50, 10] )->percentage( fn( $val, $key ) => $val < 50 );
3642
         *  Map::from( [] )->percentage( fn( $val, $key ) => true );
3643
         *  Map::from( [30, 50, 10] )->percentage( fn( $val, $key ) => $val > 100 );
3644
         *  Map::from( [30, 50, 10] )->percentage( fn( $val, $key ) => $val > 30, 3 );
3645
         *  Map::from( [30, 50, 10] )->percentage( fn( $val, $key ) => $val > 30, 0 );
3646
         *  Map::from( [30, 50, 10] )->percentage( fn( $val, $key ) => $val < 50, -1 );
3647
         *
3648
         * Results:
3649
         * The first line will return "66.67", the second and third one "0.0", the forth
3650
         * one "33.333", the fifth one "33.0" and the last one "70.0" (66 rounded up).
3651
         *
3652
         * @param Closure $fcn Closure to filter the values in the nested array or object to compute the percentage
3653
         * @param int $precision Number of decimal digits use by the result value
3654
         * @return float Percentage of all elements passing the test in the map
3655
         */
3656
        public function percentage( \Closure $fcn, int $precision = 2 ) : float
3657
        {
3658
                $vals = array_filter( $this->list(), $fcn, ARRAY_FILTER_USE_BOTH );
8✔
3659

3660
                $cnt = count( $this->list() );
8✔
3661
                return $cnt > 0 ? round( count( $vals ) * 100 / $cnt, $precision ) : 0;
8✔
3662
        }
3663

3664

3665
        /**
3666
         * Passes the map to the given callback and return the result.
3667
         *
3668
         * Examples:
3669
         *  Map::from( ['a', 'b'] )->pipe( function( $map ) {
3670
         *      return join( '-', $map->toArray() );
3671
         *  } );
3672
         *
3673
         * Results:
3674
         *  "a-b" will be returned
3675
         *
3676
         * @param \Closure $callback Function with map as parameter which returns arbitrary result
3677
         * @return mixed Result returned by the callback
3678
         */
3679
        public function pipe( \Closure $callback )
3680
        {
3681
                return $callback( $this );
8✔
3682
        }
3683

3684

3685
        /**
3686
         * Returns the values of a single column/property from an array of arrays or objects in a new map.
3687
         *
3688
         * This method is an alias for col(). For performance reasons, col() should
3689
         * be preferred because it uses one method call less than pluck().
3690
         *
3691
         * @param string|null $valuecol Name or path of the value property
3692
         * @param string|null $indexcol Name or path of the index property
3693
         * @return self<int|string,mixed> New map with mapped entries
3694
         * @see col() - Underlying method with same parameters and return value but better performance
3695
         */
3696
        public function pluck( ?string $valuecol = null, ?string $indexcol = null ) : self
3697
        {
3698
                return $this->col( $valuecol, $indexcol );
8✔
3699
        }
3700

3701

3702
        /**
3703
         * Returns and removes the last element from the map.
3704
         *
3705
         * Examples:
3706
         *  Map::from( ['a', 'b'] )->pop();
3707
         *
3708
         * Results:
3709
         *  "b" will be returned and the map only contains ['a'] afterwards
3710
         *
3711
         * @return mixed Last element of the map or null if empty
3712
         */
3713
        public function pop()
3714
        {
3715
                return array_pop( $this->list() );
16✔
3716
        }
3717

3718

3719
        /**
3720
         * Returns the numerical index of the value.
3721
         *
3722
         * Examples:
3723
         *  Map::from( [4 => 'a', 8 => 'b'] )->pos( 'b' );
3724
         *  Map::from( [4 => 'a', 8 => 'b'] )->pos( function( $item, $key ) {
3725
         *      return $item === 'b';
3726
         *  } );
3727
         *
3728
         * Results:
3729
         * Both examples will return "1" because the value "b" is at the second position
3730
         * and the returned index is zero based so the first item has the index "0".
3731
         *
3732
         * @param \Closure|mixed $value Value to search for or function with (item, key) parameters return TRUE if value is found
3733
         * @return int|null Position of the found value (zero based) or NULL if not found
3734
         */
3735
        public function pos( $value ) : ?int
3736
        {
3737
                $pos = 0;
120✔
3738
                $list = $this->list();
120✔
3739

3740
                if( $value instanceof \Closure )
120✔
3741
                {
3742
                        foreach( $list as $key => $item )
24✔
3743
                        {
3744
                                if( $value( $item, $key ) ) {
24✔
3745
                                        return $pos;
24✔
3746
                                }
3747

3748
                                ++$pos;
24✔
3749
                        }
3750

3751
                        return null;
×
3752
                }
3753

3754
                if( ( $key = array_search( $value, $list, true ) ) !== false
96✔
3755
                        && ( $pos = array_search( $key, array_keys( $list ), true ) ) !== false
96✔
3756
                ) {
3757
                        return $pos;
80✔
3758
                }
3759

3760
                return null;
16✔
3761
        }
3762

3763

3764
        /**
3765
         * Adds a prefix in front of each map entry.
3766
         *
3767
         * By defaul, nested arrays are walked recusively so all entries at all levels are prefixed.
3768
         *
3769
         * Examples:
3770
         *  Map::from( ['a', 'b'] )->prefix( '1-' );
3771
         *  Map::from( ['a', ['b']] )->prefix( '1-' );
3772
         *  Map::from( ['a', ['b']] )->prefix( '1-', 1 );
3773
         *  Map::from( ['a', 'b'] )->prefix( function( $item, $key ) {
3774
         *      return ( ord( $item ) + ord( $key ) ) . '-';
3775
         *  } );
3776
         *
3777
         * Results:
3778
         *  The first example returns ['1-a', '1-b'] while the second one will return
3779
         *  ['1-a', ['1-b']]. In the third example, the depth is limited to the first
3780
         *  level only so it will return ['1-a', ['b']]. The forth example passing
3781
         *  the closure will return ['145-a', '147-b'].
3782
         *
3783
         * The keys of the original map are preserved in the returned map.
3784
         *
3785
         * @param \Closure|string $prefix Prefix string or anonymous function with ($item, $key) as parameters
3786
         * @param int|null $depth Maximum depth to dive into multi-dimensional arrays starting from "1"
3787
         * @return self<int|string,mixed> Updated map for fluid interface
3788
         */
3789
        public function prefix( $prefix, ?int $depth = null ) : self
3790
        {
3791
                $fcn = function( array $list, $prefix, int $depth ) use ( &$fcn ) {
6✔
3792

3793
                        foreach( $list as $key => $item )
8✔
3794
                        {
3795
                                if( is_array( $item ) ) {
8✔
3796
                                        $list[$key] = $depth > 1 ? $fcn( $item, $prefix, $depth - 1 ) : $item;
8✔
3797
                                } else {
3798
                                        $list[$key] = ( is_callable( $prefix ) ? $prefix( $item, $key ) : $prefix ) . $item;
8✔
3799
                                }
3800
                        }
3801

3802
                        return $list;
8✔
3803
                };
8✔
3804

3805
                $this->list = $fcn( $this->list(), $prefix, $depth ?? 0x7fffffff );
8✔
3806
                return $this;
8✔
3807
        }
3808

3809

3810
        /**
3811
         * Pushes an element onto the beginning of the map without returning a new map.
3812
         *
3813
         * This method is an alias for unshift().
3814
         *
3815
         * @param mixed $value Item to add at the beginning
3816
         * @param int|string|null $key Key for the item or NULL to reindex all numerical keys
3817
         * @return self<int|string,mixed> Updated map for fluid interface
3818
         * @see unshift() - Underlying method with same parameters and return value but better performance
3819
         */
3820
        public function prepend( $value, $key = null ) : self
3821
        {
3822
                return $this->unshift( $value, $key );
8✔
3823
        }
3824

3825

3826
        /**
3827
         * Returns and removes an element from the map by its key.
3828
         *
3829
         * Examples:
3830
         *  Map::from( ['a', 'b', 'c'] )->pull( 1 );
3831
         *  Map::from( ['a', 'b', 'c'] )->pull( 'x', 'none' );
3832
         *  Map::from( [] )->pull( 'Y', new \Exception( 'error' ) );
3833
         *  Map::from( [] )->pull( 'Z', function() { return rand(); } );
3834
         *
3835
         * Results:
3836
         * The first example will return "b" and the map contains ['a', 'c'] afterwards.
3837
         * The second one will return "none" and the map content stays untouched. If you
3838
         * pass an exception as default value, it will throw that exception if the map
3839
         * contains no elements. In the fourth example, a random value generated by the
3840
         * closure function will be returned.
3841
         *
3842
         * @param int|string $key Key to retrieve the value for
3843
         * @param mixed $default Default value if key isn't available
3844
         * @return mixed Value from map or default value
3845
         */
3846
        public function pull( $key, $default = null )
3847
        {
3848
                $value = $this->get( $key, $default );
32✔
3849
                unset( $this->list()[$key] );
24✔
3850

3851
                return $value;
24✔
3852
        }
3853

3854

3855
        /**
3856
         * Pushes an element onto the end of the map without returning a new map.
3857
         *
3858
         * Examples:
3859
         *  Map::from( ['a', 'b'] )->push( 'aa' );
3860
         *
3861
         * Results:
3862
         *  ['a', 'b', 'aa']
3863
         *
3864
         * @param mixed $value Value to add to the end
3865
         * @return self<int|string,mixed> Updated map for fluid interface
3866
         */
3867
        public function push( $value ) : self
3868
        {
3869
                $this->list()[] = $value;
24✔
3870
                return $this;
24✔
3871
        }
3872

3873

3874
        /**
3875
         * Sets the given key and value in the map without returning a new map.
3876
         *
3877
         * This method is an alias for set(). For performance reasons, set() should be
3878
         * preferred because it uses one method call less than put().
3879
         *
3880
         * @param int|string $key Key to set the new value for
3881
         * @param mixed $value New element that should be set
3882
         * @return self<int|string,mixed> Updated map for fluid interface
3883
         * @see set() - Underlying method with same parameters and return value but better performance
3884
         */
3885
        public function put( $key, $value ) : self
3886
        {
3887
                return $this->set( $key, $value );
8✔
3888
        }
3889

3890

3891
        /**
3892
         * Returns one or more random element from the map incl. their keys.
3893
         *
3894
         * Examples:
3895
         *  Map::from( [2, 4, 8, 16] )->random();
3896
         *  Map::from( [2, 4, 8, 16] )->random( 2 );
3897
         *  Map::from( [2, 4, 8, 16] )->random( 5 );
3898
         *
3899
         * Results:
3900
         * The first example will return a map including [0 => 8] or any other value,
3901
         * the second one will return a map with [0 => 16, 1 => 2] or any other values
3902
         * and the third example will return a map of the whole list in random order. The
3903
         * less elements are in the map, the less random the order will be, especially if
3904
         * the maximum number of values is high or close to the number of elements.
3905
         *
3906
         * The keys of the original map are preserved in the returned map.
3907
         *
3908
         * @param int $max Maximum number of elements that should be returned
3909
         * @return self<int|string,mixed> New map with key/element pairs from original map in random order
3910
         * @throws \InvalidArgumentException If requested number of elements is less than 1
3911
         */
3912
        public function random( int $max = 1 ) : self
3913
        {
3914
                if( $max < 1 ) {
40✔
3915
                        throw new \InvalidArgumentException( 'Requested number of elements must be greater or equal than 1' );
8✔
3916
                }
3917

3918
                $list = $this->list();
32✔
3919

3920
                if( empty( $list ) ) {
32✔
3921
                        return new static();
8✔
3922
                }
3923

3924
                if( ( $num = count( $list ) ) < $max ) {
24✔
3925
                        $max = $num;
8✔
3926
                }
3927

3928
                $keys = array_rand( $list, $max );
24✔
3929

3930
                return new static( array_intersect_key( $list, array_flip( (array) $keys ) ) );
24✔
3931
        }
3932

3933

3934
        /**
3935
         * Iteratively reduces the array to a single value using a callback function.
3936
         * Afterwards, the map will be empty.
3937
         *
3938
         * Examples:
3939
         *  Map::from( [2, 8] )->reduce( function( $result, $value ) {
3940
         *      return $result += $value;
3941
         *  }, 10 );
3942
         *
3943
         * Results:
3944
         *  "20" will be returned because the sum is computed by 10 (initial value) + 2 + 8
3945
         *
3946
         * @param callable $callback Function with (result, value) parameters and returns result
3947
         * @param mixed $initial Initial value when computing the result
3948
         * @return mixed Value computed by the callback function
3949
         */
3950
        public function reduce( callable $callback, $initial = null )
3951
        {
3952
                return array_reduce( $this->list(), $callback, $initial );
8✔
3953
        }
3954

3955

3956
        /**
3957
         * Removes all matched elements and returns a new map.
3958
         *
3959
         * Examples:
3960
         *  Map::from( [2 => 'a', 6 => 'b', 13 => 'm', 30 => 'z'] )->reject( function( $value, $key ) {
3961
         *      return $value < 'm';
3962
         *  } );
3963
         *  Map::from( [2 => 'a', 13 => 'm', 30 => 'z'] )->reject( 'm' );
3964
         *  Map::from( [2 => 'a', 6 => null, 13 => 'm'] )->reject();
3965
         *
3966
         * Results:
3967
         *  [13 => 'm', 30 => 'z']
3968
         *  [2 => 'a', 30 => 'z']
3969
         *  [6 => null]
3970
         *
3971
         * This method is the inverse of the filter() and should return TRUE if the
3972
         * item should be removed from the returned map.
3973
         *
3974
         * If no callback is passed, all values which are NOT empty, null or false will be
3975
         * removed. The keys of the original map are preserved in the returned map.
3976
         *
3977
         * @param Closure|mixed $callback Function with (item, key) parameter which returns TRUE/FALSE
3978
         * @return self<int|string,mixed> New map
3979
         */
3980
        public function reject( $callback = true ) : self
3981
        {
3982
                $result = [];
24✔
3983

3984
                if( $callback instanceof \Closure )
24✔
3985
                {
3986
                        foreach( $this->list() as $key => $value )
8✔
3987
                        {
3988
                                if( !$callback( $value, $key ) ) {
8✔
3989
                                        $result[$key] = $value;
8✔
3990
                                }
3991
                        }
3992
                }
3993
                else
3994
                {
3995
                        foreach( $this->list() as $key => $value )
16✔
3996
                        {
3997
                                if( $value != $callback ) {
16✔
3998
                                        $result[$key] = $value;
16✔
3999
                                }
4000
                        }
4001
                }
4002

4003
                return new static( $result );
24✔
4004
        }
4005

4006

4007
        /**
4008
         * Changes the keys according to the passed function.
4009
         *
4010
         * Examples:
4011
         *  Map::from( ['a' => 2, 'b' => 4] )->rekey( function( $value, $key ) {
4012
         *      return 'key-' . $key;
4013
         *  } );
4014
         *
4015
         * Results:
4016
         *  ['key-a' => 2, 'key-b' => 4]
4017
         *
4018
         * @param callable $callback Function with (value, key) parameters and returns new key
4019
         * @return self<int|string,mixed> New map with new keys and original values
4020
         * @see map() - Maps new values to the existing keys using the passed function and returns a new map for the result
4021
         * @see transform() - Creates new key/value pairs using the passed function and returns a new map for the result
4022
         */
4023
        public function rekey( callable $callback ) : self
4024
        {
4025
                $list = $this->list();
8✔
4026
                $newKeys = array_map( $callback, $list, array_keys( $list ) );
8✔
4027

4028
                return new static( array_combine( $newKeys, array_values( $list ) ) );
8✔
4029
        }
4030

4031

4032
        /**
4033
         * Removes one or more elements from the map by its keys without returning a new map.
4034
         *
4035
         * Examples:
4036
         *  Map::from( ['a' => 1, 2 => 'b'] )->remove( 'a' );
4037
         *  Map::from( ['a' => 1, 2 => 'b'] )->remove( [2, 'a'] );
4038
         *
4039
         * Results:
4040
         * The first example will result in [2 => 'b'] while the second one resulting
4041
         * in an empty list
4042
         *
4043
         * @param iterable<string|int>|array<string|int>|string|int $keys List of keys to remove
4044
         * @return self<int|string,mixed> Updated map for fluid interface
4045
         */
4046
        public function remove( $keys ) : self
4047
        {
4048
                foreach( $this->array( $keys ) as $key ) {
40✔
4049
                        unset( $this->list()[$key] );
40✔
4050
                }
4051

4052
                return $this;
40✔
4053
        }
4054

4055

4056
        /**
4057
         * Replaces elements in the map with the given elements without returning a new map.
4058
         *
4059
         * Examples:
4060
         *  Map::from( ['a' => 1, 2 => 'b'] )->replace( ['a' => 2] );
4061
         *  Map::from( ['a' => 1, 'b' => ['c' => 3, 'd' => 4]] )->replace( ['b' => ['c' => 9]] );
4062
         *
4063
         * Results:
4064
         *  ['a' => 2, 2 => 'b']
4065
         *  ['a' => 1, 'b' => ['c' => 9, 'd' => 4]]
4066
         *
4067
         * The method is similar to merge() but it also replaces elements with numeric
4068
         * keys. These would be added by merge() with a new numeric key.
4069
         *
4070
         * The keys are preserved using this method.
4071
         *
4072
         * @param iterable<int|string,mixed> $elements List of elements
4073
         * @param bool $recursive TRUE to replace recursively (default), FALSE to replace elements only
4074
         * @return self<int|string,mixed> Updated map for fluid interface
4075
         */
4076
        public function replace( iterable $elements, bool $recursive = true ) : self
4077
        {
4078
                if( $recursive ) {
40✔
4079
                        $this->list = array_replace_recursive( $this->list(), $this->array( $elements ) );
32✔
4080
                } else {
4081
                        $this->list = array_replace( $this->list(), $this->array( $elements ) );
8✔
4082
                }
4083

4084
                return $this;
40✔
4085
        }
4086

4087

4088
        /**
4089
         * Reverses the element order with keys without returning a new map.
4090
         *
4091
         * Examples:
4092
         *  Map::from( ['a', 'b'] )->reverse();
4093
         *  Map::from( ['name' => 'test', 'last' => 'user'] )->reverse();
4094
         *
4095
         * Results:
4096
         *  ['b', 'a']
4097
         *  ['last' => 'user', 'name' => 'test']
4098
         *
4099
         * The keys are preserved using this method.
4100
         *
4101
         * @return self<int|string,mixed> Updated map for fluid interface
4102
         * @see reversed() - Reverses the element order in a copy of the map
4103
         */
4104
        public function reverse() : self
4105
        {
4106
                $this->list = array_reverse( $this->list(), true );
32✔
4107
                return $this;
32✔
4108
        }
4109

4110

4111
        /**
4112
         * Reverses the element order in a copy of the map.
4113
         *
4114
         * Examples:
4115
         *  Map::from( ['a', 'b'] )->reversed();
4116
         *  Map::from( ['name' => 'test', 'last' => 'user'] )->reversed();
4117
         *
4118
         * Results:
4119
         *  ['b', 'a']
4120
         *  ['last' => 'user', 'name' => 'test']
4121
         *
4122
         * The keys are preserved using this method and a new map is created before reversing the elements.
4123
         * Thus, reverse() should be preferred for performance reasons if possible.
4124
         *
4125
         * @return self<int|string,mixed> New map with a reversed copy of the elements
4126
         * @see reverse() - Reverses the element order with keys without returning a new map
4127
         */
4128
        public function reversed() : self
4129
        {
4130
                return ( clone $this )->reverse();
16✔
4131
        }
4132

4133

4134
        /**
4135
         * Sorts all elements in reverse order using new keys.
4136
         *
4137
         * Examples:
4138
         *  Map::from( ['a' => 1, 'b' => 0] )->rsort();
4139
         *  Map::from( [0 => 'b', 1 => 'a'] )->rsort();
4140
         *
4141
         * Results:
4142
         *  [0 => 1, 1 => 0]
4143
         *  [0 => 'b', 1 => 'a']
4144
         *
4145
         * The parameter modifies how the values are compared. Possible parameter values are:
4146
         * - SORT_REGULAR : compare elements normally (don't change types)
4147
         * - SORT_NUMERIC : compare elements numerically
4148
         * - SORT_STRING : compare elements as strings
4149
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
4150
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
4151
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
4152
         *
4153
         * The keys aren't preserved and elements get a new index. No new map is created
4154
         *
4155
         * @param int $options Sort options for rsort()
4156
         * @return self<int|string,mixed> Updated map for fluid interface
4157
         */
4158
        public function rsort( int $options = SORT_REGULAR ) : self
4159
        {
4160
                rsort( $this->list(), $options );
24✔
4161
                return $this;
24✔
4162
        }
4163

4164

4165
        /**
4166
         * Sorts a copy of all elements in reverse order using new keys.
4167
         *
4168
         * Examples:
4169
         *  Map::from( ['a' => 1, 'b' => 0] )->rsorted();
4170
         *  Map::from( [0 => 'b', 1 => 'a'] )->rsorted();
4171
         *
4172
         * Results:
4173
         *  [0 => 1, 1 => 0]
4174
         *  [0 => 'b', 1 => 'a']
4175
         *
4176
         * The parameter modifies how the values are compared. Possible parameter values are:
4177
         * - SORT_REGULAR : compare elements normally (don't change types)
4178
         * - SORT_NUMERIC : compare elements numerically
4179
         * - SORT_STRING : compare elements as strings
4180
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
4181
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
4182
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
4183
         *
4184
         * The keys aren't preserved, elements get a new index and a new map is created.
4185
         *
4186
         * @param int $options Sort options for rsort()
4187
         * @return self<int|string,mixed> Updated map for fluid interface
4188
         */
4189
        public function rsorted( int $options = SORT_REGULAR ) : self
4190
        {
4191
                return ( clone $this )->rsort( $options );
8✔
4192
        }
4193

4194

4195
        /**
4196
         * Removes the passed characters from the right of all strings.
4197
         *
4198
         * Examples:
4199
         *  Map::from( [" abc\n", "\tcde\r\n"] )->rtrim();
4200
         *  Map::from( ["a b c", "cbxa"] )->rtrim( 'abc' );
4201
         *
4202
         * Results:
4203
         * The first example will return [" abc", "\tcde"] while the second one will return ["a b ", "cbx"].
4204
         *
4205
         * @param string $chars List of characters to trim
4206
         * @return self<int|string,mixed> Updated map for fluid interface
4207
         */
4208
        public function rtrim( string $chars = " \n\r\t\v\x00" ) : self
4209
        {
4210
                foreach( $this->list() as &$entry )
8✔
4211
                {
4212
                        if( is_string( $entry ) ) {
8✔
4213
                                $entry = rtrim( $entry, $chars );
8✔
4214
                        }
4215
                }
4216

4217
                return $this;
8✔
4218
        }
4219

4220

4221
        /**
4222
         * Searches the map for a given value and return the corresponding key if successful.
4223
         *
4224
         * Examples:
4225
         *  Map::from( ['a', 'b', 'c'] )->search( 'b' );
4226
         *  Map::from( [1, 2, 3] )->search( '2', true );
4227
         *
4228
         * Results:
4229
         * The first example will return 1 (array index) while the second one will
4230
         * return NULL because the types doesn't match (int vs. string)
4231
         *
4232
         * @param mixed $value Item to search for
4233
         * @param bool $strict TRUE if type of the element should be checked too
4234
         * @return int|string|null Key associated to the value or null if not found
4235
         */
4236
        public function search( $value, $strict = true )
4237
        {
4238
                if( ( $result = array_search( $value, $this->list(), $strict ) ) !== false ) {
8✔
4239
                        return $result;
8✔
4240
                }
4241

4242
                return null;
8✔
4243
        }
4244

4245

4246
        /**
4247
         * Sets the seperator for paths to values in multi-dimensional arrays or objects.
4248
         *
4249
         * This method only changes the separator for the current map instance. To
4250
         * change the separator for all maps created afterwards, use the static
4251
         * delimiter() method instead.
4252
         *
4253
         * Examples:
4254
         *  Map::from( ['foo' => ['bar' => 'baz']] )->sep( '/' )->get( 'foo/bar' );
4255
         *
4256
         * Results:
4257
         *  'baz'
4258
         *
4259
         * @param string $char Separator character, e.g. "." for "key.to.value" instead of "key/to/value"
4260
         * @return self<int|string,mixed> Same map for fluid interface
4261
         */
4262
        public function sep( string $char ) : self
4263
        {
4264
                $this->sep = $char;
8✔
4265
                return $this;
8✔
4266
        }
4267

4268

4269
        /**
4270
         * Sets an element in the map by key without returning a new map.
4271
         *
4272
         * Examples:
4273
         *  Map::from( ['a'] )->set( 1, 'b' );
4274
         *  Map::from( ['a'] )->set( 0, 'b' );
4275
         *
4276
         * Results:
4277
         *  ['a', 'b']
4278
         *  ['b']
4279
         *
4280
         * @param int|string $key Key to set the new value for
4281
         * @param mixed $value New element that should be set
4282
         * @return self<int|string,mixed> Updated map for fluid interface
4283
         */
4284
        public function set( $key, $value ) : self
4285
        {
4286
                $this->list()[(string) $key] = $value;
40✔
4287
                return $this;
40✔
4288
        }
4289

4290

4291
        /**
4292
         * Returns and removes the first element from the map.
4293
         *
4294
         * Examples:
4295
         *  Map::from( ['a', 'b'] )->shift();
4296
         *  Map::from( [] )->shift();
4297
         *
4298
         * Results:
4299
         * The first example returns "a" and shortens the map to ['b'] only while the
4300
         * second example will return NULL
4301
         *
4302
         * Performance note:
4303
         * The bigger the list, the higher the performance impact because shift()
4304
         * reindexes all existing elements. Usually, it's better to reverse() the list
4305
         * and pop() entries from the list afterwards if a significant number of elements
4306
         * should be removed from the list:
4307
         *
4308
         *  $map->reverse()->pop();
4309
         * instead of
4310
         *  $map->shift( 'a' );
4311
         *
4312
         * @return mixed|null Value from map or null if not found
4313
         */
4314
        public function shift()
4315
        {
4316
                return array_shift( $this->list() );
8✔
4317
        }
4318

4319

4320
        /**
4321
         * Shuffles the elements in the map without returning a new map.
4322
         *
4323
         * Examples:
4324
         *  Map::from( [2 => 'a', 4 => 'b'] )->shuffle();
4325
         *  Map::from( [2 => 'a', 4 => 'b'] )->shuffle( true );
4326
         *
4327
         * Results:
4328
         * The map in the first example will contain "a" and "b" in random order and
4329
         * with new keys assigned. The second call will also return all values in
4330
         * random order but preserves the keys of the original list.
4331
         *
4332
         * @param bool $assoc True to preserve keys, false to assign new keys
4333
         * @return self<int|string,mixed> Updated map for fluid interface
4334
         * @see shuffled() - Shuffles the elements in a copy of the map
4335
         */
4336
        public function shuffle( bool $assoc = false ) : self
4337
        {
4338
                if( $assoc )
24✔
4339
                {
4340
                        $list = $this->list();
8✔
4341
                        $keys = array_keys( $list );
8✔
4342
                        shuffle( $keys );
8✔
4343
                        $items = [];
8✔
4344

4345
                        foreach( $keys as $key ) {
8✔
4346
                                $items[$key] = $list[$key];
8✔
4347
                        }
4348

4349
                        $this->list = $items;
8✔
4350
                }
4351
                else
4352
                {
4353
                        shuffle( $this->list() );
16✔
4354
                }
4355

4356
                return $this;
24✔
4357
        }
4358

4359

4360
        /**
4361
         * Shuffles the elements in a copy of the map.
4362
         *
4363
         * Examples:
4364
         *  Map::from( [2 => 'a', 4 => 'b'] )->shuffled();
4365
         *  Map::from( [2 => 'a', 4 => 'b'] )->shuffled( true );
4366
         *
4367
         * Results:
4368
         * The map in the first example will contain "a" and "b" in random order and
4369
         * with new keys assigned. The second call will also return all values in
4370
         * random order but preserves the keys of the original list.
4371
         *
4372
         * @param bool $assoc True to preserve keys, false to assign new keys
4373
         * @return self<int|string,mixed> New map with a shuffled copy of the elements
4374
         * @see shuffle() - Shuffles the elements in the map without returning a new map
4375
         */
4376
        public function shuffled( bool $assoc = false ) : self
4377
        {
4378
                return ( clone $this )->shuffle( $assoc );
8✔
4379
        }
4380

4381

4382
        /**
4383
         * Returns a new map with the given number of items skipped.
4384
         *
4385
         * Examples:
4386
         *  Map::from( [1, 2, 3, 4] )->skip( 2 );
4387
         *  Map::from( [1, 2, 3, 4] )->skip( function( $item, $key ) {
4388
         *      return $item < 4;
4389
         *  } );
4390
         *
4391
         * Results:
4392
         *  [2 => 3, 3 => 4]
4393
         *  [3 => 4]
4394
         *
4395
         * The keys of the items returned in the new map are the same as in the original one.
4396
         *
4397
         * @param \Closure|int $offset Number of items to skip or function($item, $key) returning true for skipped items
4398
         * @return self<int|string,mixed> New map
4399
         */
4400
        public function skip( $offset ) : self
4401
        {
4402
                if( is_numeric( $offset ) ) {
24✔
4403
                        return new static( array_slice( $this->list(), (int) $offset, null, true ) );
8✔
4404
                }
4405

4406
                if( $offset instanceof \Closure ) {
16✔
4407
                        return new static( array_slice( $this->list(), $this->until( $this->list(), $offset ), null, true ) );
8✔
4408
                }
4409

4410
                throw new \InvalidArgumentException( 'Only an integer or a closure is allowed as first argument for skip()' );
8✔
4411
        }
4412

4413

4414
        /**
4415
         * Returns a map with the slice from the original map.
4416
         *
4417
         * Examples:
4418
         *  Map::from( ['a', 'b', 'c'] )->slice( 1 );
4419
         *  Map::from( ['a', 'b', 'c'] )->slice( 1, 1 );
4420
         *  Map::from( ['a', 'b', 'c', 'd'] )->slice( -2, -1 );
4421
         *
4422
         * Results:
4423
         * The first example will return ['b', 'c'] and the second one ['b'] only.
4424
         * The third example returns ['c'] because the slice starts at the second
4425
         * last value and ends before the last value.
4426
         *
4427
         * The rules for offsets are:
4428
         * - If offset is non-negative, the sequence will start at that offset
4429
         * - If offset is negative, the sequence will start that far from the end
4430
         *
4431
         * Similar for the length:
4432
         * - If length is given and is positive, then the sequence will have up to that many elements in it
4433
         * - If the array is shorter than the length, then only the available array elements will be present
4434
         * - If length is given and is negative then the sequence will stop that many elements from the end
4435
         * - If it is omitted, then the sequence will have everything from offset up until the end
4436
         *
4437
         * The keys of the items returned in the new map are the same as in the original one.
4438
         *
4439
         * @param int $offset Number of elements to start from
4440
         * @param int|null $length Number of elements to return or NULL for no limit
4441
         * @return self<int|string,mixed> New map
4442
         */
4443
        public function slice( int $offset, ?int $length = null ) : self
4444
        {
4445
                return new static( array_slice( $this->list(), $offset, $length, true ) );
48✔
4446
        }
4447

4448

4449
        /**
4450
         * Tests if at least one element passes the test or is part of the map.
4451
         *
4452
         * Examples:
4453
         *  Map::from( ['a', 'b'] )->some( 'a' );
4454
         *  Map::from( ['a', 'b'] )->some( ['a', 'c'] );
4455
         *  Map::from( ['a', 'b'] )->some( function( $item, $key ) {
4456
         *    return $item === 'a';
4457
         *  } );
4458
         *  Map::from( ['a', 'b'] )->some( ['c', 'd'] );
4459
         *  Map::from( ['1', '2'] )->some( [2], true );
4460
         *
4461
         * Results:
4462
         * The first three examples will return TRUE while the fourth and fifth will return FALSE
4463
         *
4464
         * @param \Closure|iterable|mixed $values Closure with (item, key) parameter, element or list of elements to test against
4465
         * @param bool $strict TRUE to check the type too, using FALSE '1' and 1 will be the same
4466
         * @return bool TRUE if at least one element is available in map, FALSE if the map contains none of them
4467
         */
4468
        public function some( $values, bool $strict = false ) : bool
4469
        {
4470
                $list = $this->list();
48✔
4471

4472
                if( is_iterable( $values ) )
48✔
4473
                {
4474
                        foreach( $values as $entry )
24✔
4475
                        {
4476
                                if( in_array( $entry, $list, $strict ) === true ) {
24✔
4477
                                        return true;
24✔
4478
                                }
4479
                        }
4480

4481
                        return false;
16✔
4482
                }
4483

4484
                if( $values instanceof \Closure )
32✔
4485
                {
4486
                        foreach( $list as $key => $item )
16✔
4487
                        {
4488
                                if( $values( $item, $key ) ) {
16✔
4489
                                        return true;
16✔
4490
                                }
4491
                        }
4492
                }
4493

4494
                return in_array( $values, $list, $strict );
32✔
4495
        }
4496

4497

4498
        /**
4499
         * Sorts all elements in-place using new keys.
4500
         *
4501
         * Examples:
4502
         *  Map::from( ['a' => 1, 'b' => 0] )->sort();
4503
         *  Map::from( [0 => 'b', 1 => 'a'] )->sort();
4504
         *
4505
         * Results:
4506
         *  [0 => 0, 1 => 1]
4507
         *  [0 => 'a', 1 => 'b']
4508
         *
4509
         * The parameter modifies how the values are compared. Possible parameter values are:
4510
         * - SORT_REGULAR : compare elements normally (don't change types)
4511
         * - SORT_NUMERIC : compare elements numerically
4512
         * - SORT_STRING : compare elements as strings
4513
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
4514
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
4515
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
4516
         *
4517
         * The keys aren't preserved and elements get a new index. No new map is created.
4518
         *
4519
         * @param int $options Sort options for PHP sort()
4520
         * @return self<int|string,mixed> Updated map for fluid interface
4521
         * @see sorted() - Sorts elements in a copy of the map
4522
         */
4523
        public function sort( int $options = SORT_REGULAR ) : self
4524
        {
4525
                sort( $this->list(), $options );
40✔
4526
                return $this;
40✔
4527
        }
4528

4529

4530
        /**
4531
         * Sorts the elements in a copy of the map using new keys.
4532
         *
4533
         * Examples:
4534
         *  Map::from( ['a' => 1, 'b' => 0] )->sorted();
4535
         *  Map::from( [0 => 'b', 1 => 'a'] )->sorted();
4536
         *
4537
         * Results:
4538
         *  [0 => 0, 1 => 1]
4539
         *  [0 => 'a', 1 => 'b']
4540
         *
4541
         * The parameter modifies how the values are compared. Possible parameter values are:
4542
         * - SORT_REGULAR : compare elements normally (don't change types)
4543
         * - SORT_NUMERIC : compare elements numerically
4544
         * - SORT_STRING : compare elements as strings
4545
         * - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
4546
         * - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
4547
         * - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
4548
         *
4549
         * The keys aren't preserved and elements get a new index and a new map is created before sorting the elements.
4550
         * Thus, sort() should be preferred for performance reasons if possible. A new map is created by calling this method.
4551
         *
4552
         * @param int $options Sort options for PHP sort()
4553
         * @return self<int|string,mixed> New map with a sorted copy of the elements
4554
         * @see sort() - Sorts elements in-place in the original map
4555
         */
4556
        public function sorted( int $options = SORT_REGULAR ) : self
4557
        {
4558
                return ( clone $this )->sort( $options );
16✔
4559
        }
4560

4561

4562
        /**
4563
         * Removes a portion of the map and replace it with the given replacement, then return the updated map.
4564
         *
4565
         * Examples:
4566
         *  Map::from( ['a', 'b', 'c'] )->splice( 1 );
4567
         *  Map::from( ['a', 'b', 'c'] )->splice( 1, 1, ['x', 'y'] );
4568
         *
4569
         * Results:
4570
         * The first example removes all entries after "a", so only ['a'] will be left
4571
         * in the map and ['b', 'c'] is returned. The second example replaces/returns "b"
4572
         * (start at 1, length 1) with ['x', 'y'] so the new map will contain
4573
         * ['a', 'x', 'y', 'c'] afterwards.
4574
         *
4575
         * The rules for offsets are:
4576
         * - If offset is non-negative, the sequence will start at that offset
4577
         * - If offset is negative, the sequence will start that far from the end
4578
         *
4579
         * Similar for the length:
4580
         * - If length is given and is positive, then the sequence will have up to that many elements in it
4581
         * - If the array is shorter than the length, then only the available array elements will be present
4582
         * - If length is given and is negative then the sequence will stop that many elements from the end
4583
         * - If it is omitted, then the sequence will have everything from offset up until the end
4584
         *
4585
         * Numerical array indexes are NOT preserved.
4586
         *
4587
         * @param int $offset Number of elements to start from
4588
         * @param int|null $length Number of elements to remove, NULL for all
4589
         * @param mixed $replacement List of elements to insert
4590
         * @return self<int|string,mixed> New map
4591
         */
4592
        public function splice( int $offset, ?int $length = null, $replacement = [] ) : self
4593
        {
4594
                // PHP 7.x doesn't allow to pass NULL as replacement
4595
                if( $length === null ) {
40✔
4596
                        $length = count( $this->list() );
16✔
4597
                }
4598

4599
                return new static( array_splice( $this->list(), $offset, $length, (array) $replacement ) );
40✔
4600
        }
4601

4602

4603
        /**
4604
         * Returns the strings after the passed value.
4605
         *
4606
         * Examples:
4607
         *  Map::from( ['äöüß'] )->strAfter( 'ö' );
4608
         *  Map::from( ['abc'] )->strAfter( '' );
4609
         *  Map::from( ['abc'] )->strAfter( 'b' );
4610
         *  Map::from( ['abc'] )->strAfter( 'c' );
4611
         *  Map::from( ['abc'] )->strAfter( 'x' );
4612
         *  Map::from( [''] )->strAfter( '' );
4613
         *  Map::from( [1, 1.0, true, ['x'], new \stdClass] )->strAfter( '' );
4614
         *  Map::from( [0, 0.0, false, []] )->strAfter( '' );
4615
         *
4616
         * Results:
4617
         *  ['üß']
4618
         *  ['abc']
4619
         *  ['c']
4620
         *  ['']
4621
         *  []
4622
         *  []
4623
         *  ['1', '1', '1']
4624
         *  ['0', '0']
4625
         *
4626
         * All scalar values (bool, int, float, string) will be converted to strings.
4627
         * Non-scalar values as well as empty strings will be skipped and are not part of the result.
4628
         *
4629
         * @param string $value Character or string to search for
4630
         * @param bool $case TRUE if search should be case insensitive, FALSE if case-sensitive
4631
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4632
         * @return self<int|string,mixed> New map
4633
         */
4634
        public function strAfter( string $value, bool $case = false, string $encoding = 'UTF-8' ) : self
4635
        {
4636
                $list = [];
8✔
4637
                $len = mb_strlen( $value );
8✔
4638
                $fcn = $case ? 'mb_stripos' : 'mb_strpos';
8✔
4639

4640
                foreach( $this->list() as $key => $entry )
8✔
4641
                {
4642
                        if( is_scalar( $entry ) )
8✔
4643
                        {
4644
                                $pos = null;
8✔
4645
                                $str = (string) $entry;
8✔
4646

4647
                                if( $str !== '' && $value !== '' && ( $pos = $fcn( $str, $value, 0, $encoding ) ) !== false ) {
8✔
4648
                                        $list[$key] = mb_substr( $str, $pos + $len, null, $encoding );
8✔
4649
                                } elseif( $str !== '' && $pos !== false ) {
8✔
4650
                                        $list[$key] = $str;
8✔
4651
                                }
4652
                        }
4653
                }
4654

4655
                return new static( $list );
8✔
4656
        }
4657

4658

4659
        /**
4660
         * Returns the strings before the passed value.
4661
         *
4662
         * Examples:
4663
         *  Map::from( ['äöüß'] )->strBefore( 'ü' );
4664
         *  Map::from( ['abc'] )->strBefore( '' );
4665
         *  Map::from( ['abc'] )->strBefore( 'b' );
4666
         *  Map::from( ['abc'] )->strBefore( 'a' );
4667
         *  Map::from( ['abc'] )->strBefore( 'x' );
4668
         *  Map::from( [''] )->strBefore( '' );
4669
         *  Map::from( [1, 1.0, true, ['x'], new \stdClass] )->strAfter( '' );
4670
         *  Map::from( [0, 0.0, false, []] )->strAfter( '' );
4671
         *
4672
         * Results:
4673
         *  ['äö']
4674
         *  ['abc']
4675
         *  ['a']
4676
         *  ['']
4677
         *  []
4678
         *  []
4679
         *  ['1', '1', '1']
4680
         *  ['0', '0']
4681
         *
4682
         * All scalar values (bool, int, float, string) will be converted to strings.
4683
         * Non-scalar values as well as empty strings will be skipped and are not part of the result.
4684
         *
4685
         * @param string $value Character or string to search for
4686
         * @param bool $case TRUE if search should be case insensitive, FALSE if case-sensitive
4687
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4688
         * @return self<int|string,mixed> New map
4689
         */
4690
        public function strBefore( string $value, bool $case = false, string $encoding = 'UTF-8' ) : self
4691
        {
4692
                $list = [];
8✔
4693
                $fcn = $case ? 'mb_strripos' : 'mb_strrpos';
8✔
4694

4695
                foreach( $this->list() as $key => $entry )
8✔
4696
                {
4697
                        if( is_scalar( $entry ) )
8✔
4698
                        {
4699
                                $pos = null;
8✔
4700
                                $str = (string) $entry;
8✔
4701

4702
                                if( $str !== '' && $value !== '' && ( $pos = $fcn( $str, $value, 0, $encoding ) ) !== false ) {
8✔
4703
                                        $list[$key] = mb_substr( $str, 0, $pos, $encoding );
8✔
4704
                                } elseif( $str !== '' && $pos !== false ) {
8✔
4705
                                        $list[$key] = $str;
8✔
4706
                                }
4707
                        }
4708
                }
4709

4710
                return new static( $list );
8✔
4711
        }
4712

4713

4714
        /**
4715
         * Compares the value against all map elements.
4716
         *
4717
         * Examples:
4718
         *  Map::from( ['foo', 'bar'] )->compare( 'foo' );
4719
         *  Map::from( ['foo', 'bar'] )->compare( 'Foo', false );
4720
         *  Map::from( [123, 12.3] )->compare( '12.3' );
4721
         *  Map::from( [false, true] )->compare( '1' );
4722
         *  Map::from( ['foo', 'bar'] )->compare( 'Foo' );
4723
         *  Map::from( ['foo', 'bar'] )->compare( 'baz' );
4724
         *  Map::from( [new \stdClass(), 'bar'] )->compare( 'foo' );
4725
         *
4726
         * Results:
4727
         * The first four examples return TRUE, the last three examples will return FALSE.
4728
         *
4729
         * All scalar values (bool, float, int and string) are casted to string values before
4730
         * comparing to the given value. Non-scalar values in the map are ignored.
4731
         *
4732
         * @param string $value Value to compare map elements to
4733
         * @param bool $case TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
4734
         * @return bool TRUE If at least one element matches, FALSE if value is not in map
4735
         */
4736
        public function strCompare( string $value, bool $case = true ) : bool
4737
        {
4738
                $fcn = $case ? 'strcmp' : 'strcasecmp';
16✔
4739

4740
                foreach( $this->list() as $item )
16✔
4741
                {
4742
                        if( is_scalar( $item ) && !$fcn( (string) $item, $value ) ) {
16✔
4743
                                return true;
16✔
4744
                        }
4745
                }
4746

4747
                return false;
16✔
4748
        }
4749

4750

4751
        /**
4752
         * Tests if at least one of the passed strings is part of at least one entry.
4753
         *
4754
         * Examples:
4755
         *  Map::from( ['abc'] )->strContains( '' );
4756
         *  Map::from( ['abc'] )->strContains( 'a' );
4757
         *  Map::from( ['abc'] )->strContains( 'bc' );
4758
         *  Map::from( [12345] )->strContains( '23' );
4759
         *  Map::from( [123.4] )->strContains( 23.4 );
4760
         *  Map::from( [12345] )->strContains( false );
4761
         *  Map::from( [12345] )->strContains( true );
4762
         *  Map::from( [false] )->strContains( false );
4763
         *  Map::from( [''] )->strContains( false );
4764
         *  Map::from( ['abc'] )->strContains( ['b', 'd'] );
4765
         *  Map::from( ['abc'] )->strContains( 'c', 'ASCII' );
4766
         *
4767
         *  Map::from( ['abc'] )->strContains( 'd' );
4768
         *  Map::from( ['abc'] )->strContains( 'cb' );
4769
         *  Map::from( [23456] )->strContains( true );
4770
         *  Map::from( [false] )->strContains( 0 );
4771
         *  Map::from( ['abc'] )->strContains( ['d', 'e'] );
4772
         *  Map::from( ['abc'] )->strContains( 'cb', 'ASCII' );
4773
         *
4774
         * Results:
4775
         * The first eleven examples will return TRUE while the last six will return FALSE.
4776
         *
4777
         * @param array|string $value The string or list of strings to search for in each entry
4778
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4779
         * @return bool TRUE if one of the entries contains one of the strings, FALSE if not
4780
         * @todo 4.0 Add $case parameter at second position
4781
         */
4782
        public function strContains( $value, string $encoding = 'UTF-8' ) : bool
4783
        {
4784
                foreach( $this->list() as $entry )
8✔
4785
                {
4786
                        $entry = (string) $entry;
8✔
4787

4788
                        foreach( (array) $value as $str )
8✔
4789
                        {
4790
                                $str = (string) $str;
8✔
4791

4792
                                if( ( $str === '' || mb_strpos( $entry, (string) $str, 0, $encoding ) !== false ) ) {
8✔
4793
                                        return true;
8✔
4794
                                }
4795
                        }
4796
                }
4797

4798
                return false;
8✔
4799
        }
4800

4801

4802
        /**
4803
         * Tests if all of the entries contains one of the passed strings.
4804
         *
4805
         * Examples:
4806
         *  Map::from( ['abc', 'def'] )->strContainsAll( '' );
4807
         *  Map::from( ['abc', 'cba'] )->strContainsAll( 'a' );
4808
         *  Map::from( ['abc', 'bca'] )->strContainsAll( 'bc' );
4809
         *  Map::from( [12345, '230'] )->strContainsAll( '23' );
4810
         *  Map::from( [123.4, 23.42] )->strContainsAll( 23.4 );
4811
         *  Map::from( [12345, '234'] )->strContainsAll( [true, false] );
4812
         *  Map::from( ['', false] )->strContainsAll( false );
4813
         *  Map::from( ['abc', 'def'] )->strContainsAll( ['b', 'd'] );
4814
         *  Map::from( ['abc', 'ecf'] )->strContainsAll( 'c', 'ASCII' );
4815
         *
4816
         *  Map::from( ['abc', 'def'] )->strContainsAll( 'd' );
4817
         *  Map::from( ['abc', 'cab'] )->strContainsAll( 'cb' );
4818
         *  Map::from( [23456, '123'] )->strContainsAll( true );
4819
         *  Map::from( [false, '000'] )->strContainsAll( 0 );
4820
         *  Map::from( ['abc', 'acf'] )->strContainsAll( ['d', 'e'] );
4821
         *  Map::from( ['abc', 'bca'] )->strContainsAll( 'cb', 'ASCII' );
4822
         *
4823
         * Results:
4824
         * The first nine examples will return TRUE while the last six will return FALSE.
4825
         *
4826
         * @param array|string $value The string or list of strings to search for in each entry
4827
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4828
         * @return bool TRUE if all of the entries contains at least one of the strings, FALSE if not
4829
         * @todo 4.0 Add $case parameter at second position
4830
         */
4831
        public function strContainsAll( $value, string $encoding = 'UTF-8' ) : bool
4832
        {
4833
                $list = [];
8✔
4834

4835
                foreach( $this->list() as $entry )
8✔
4836
                {
4837
                        $entry = (string) $entry;
8✔
4838
                        $list[$entry] = 0;
8✔
4839

4840
                        foreach( (array) $value as $str )
8✔
4841
                        {
4842
                                $str = (string) $str;
8✔
4843

4844
                                if( (int) ( $str === '' || mb_strpos( $entry, (string) $str, 0, $encoding ) !== false ) ) {
8✔
4845
                                        $list[$entry] = 1; break;
8✔
4846
                                }
4847
                        }
4848
                }
4849

4850
                return array_sum( $list ) === count( $list );
8✔
4851
        }
4852

4853

4854
        /**
4855
         * Tests if at least one of the entries ends with one of the passed strings.
4856
         *
4857
         * Examples:
4858
         *  Map::from( ['abc'] )->strEnds( '' );
4859
         *  Map::from( ['abc'] )->strEnds( 'c' );
4860
         *  Map::from( ['abc'] )->strEnds( 'bc' );
4861
         *  Map::from( ['abc'] )->strEnds( ['b', 'c'] );
4862
         *  Map::from( ['abc'] )->strEnds( 'c', 'ASCII' );
4863
         *  Map::from( ['abc'] )->strEnds( 'a' );
4864
         *  Map::from( ['abc'] )->strEnds( 'cb' );
4865
         *  Map::from( ['abc'] )->strEnds( ['d', 'b'] );
4866
         *  Map::from( ['abc'] )->strEnds( 'cb', 'ASCII' );
4867
         *
4868
         * Results:
4869
         * The first five examples will return TRUE while the last four will return FALSE.
4870
         *
4871
         * @param array|string $value The string or strings to search for in each entry
4872
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4873
         * @return bool TRUE if one of the entries ends with one of the strings, FALSE if not
4874
         * @todo 4.0 Add $case parameter at second position
4875
         */
4876
        public function strEnds( $value, string $encoding = 'UTF-8' ) : bool
4877
        {
4878
                foreach( $this->list() as $entry )
8✔
4879
                {
4880
                        $entry = (string) $entry;
8✔
4881

4882
                        foreach( (array) $value as $str )
8✔
4883
                        {
4884
                                $len = mb_strlen( (string) $str );
8✔
4885

4886
                                if( ( $str === '' || mb_strpos( $entry, (string) $str, -$len, $encoding ) !== false ) ) {
8✔
4887
                                        return true;
8✔
4888
                                }
4889
                        }
4890
                }
4891

4892
                return false;
8✔
4893
        }
4894

4895

4896
        /**
4897
         * Tests if all of the entries ends with at least one of the passed strings.
4898
         *
4899
         * Examples:
4900
         *  Map::from( ['abc', 'def'] )->strEndsAll( '' );
4901
         *  Map::from( ['abc', 'bac'] )->strEndsAll( 'c' );
4902
         *  Map::from( ['abc', 'cbc'] )->strEndsAll( 'bc' );
4903
         *  Map::from( ['abc', 'def'] )->strEndsAll( ['c', 'f'] );
4904
         *  Map::from( ['abc', 'efc'] )->strEndsAll( 'c', 'ASCII' );
4905
         *  Map::from( ['abc', 'fed'] )->strEndsAll( 'd' );
4906
         *  Map::from( ['abc', 'bca'] )->strEndsAll( 'ca' );
4907
         *  Map::from( ['abc', 'acf'] )->strEndsAll( ['a', 'c'] );
4908
         *  Map::from( ['abc', 'bca'] )->strEndsAll( 'ca', 'ASCII' );
4909
         *
4910
         * Results:
4911
         * The first five examples will return TRUE while the last four will return FALSE.
4912
         *
4913
         * @param array|string $value The string or strings to search for in each entry
4914
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4915
         * @return bool TRUE if all of the entries ends with at least one of the strings, FALSE if not
4916
         * @todo 4.0 Add $case parameter at second position
4917
         */
4918
        public function strEndsAll( $value, string $encoding = 'UTF-8' ) : bool
4919
        {
4920
                $list = [];
8✔
4921

4922
                foreach( $this->list() as $entry )
8✔
4923
                {
4924
                        $entry = (string) $entry;
8✔
4925
                        $list[$entry] = 0;
8✔
4926

4927
                        foreach( (array) $value as $str )
8✔
4928
                        {
4929
                                $len = mb_strlen( (string) $str );
8✔
4930

4931
                                if( (int) ( $str === '' || mb_strpos( $entry, (string) $str, -$len, $encoding ) !== false ) ) {
8✔
4932
                                        $list[$entry] = 1; break;
8✔
4933
                                }
4934
                        }
4935
                }
4936

4937
                return array_sum( $list ) === count( $list );
8✔
4938
        }
4939

4940

4941
        /**
4942
         * Returns an element by key and casts it to string if possible.
4943
         *
4944
         * Examples:
4945
         *  Map::from( ['a' => true] )->string( 'a' );
4946
         *  Map::from( ['a' => 1] )->string( 'a' );
4947
         *  Map::from( ['a' => 1.1] )->string( 'a' );
4948
         *  Map::from( ['a' => 'abc'] )->string( 'a' );
4949
         *  Map::from( ['a' => ['b' => ['c' => 'yes']]] )->string( 'a/b/c' );
4950
         *  Map::from( [] )->string( 'a', function() { return 'no'; } );
4951
         *
4952
         *  Map::from( [] )->string( 'b' );
4953
         *  Map::from( ['b' => ''] )->string( 'b' );
4954
         *  Map::from( ['b' => null] )->string( 'b' );
4955
         *  Map::from( ['b' => [true]] )->string( 'b' );
4956
         *  Map::from( ['b' => resource] )->string( 'b' );
4957
         *  Map::from( ['b' => new \stdClass] )->string( 'b' );
4958
         *
4959
         *  Map::from( [] )->string( 'c', new \Exception( 'error' ) );
4960
         *
4961
         * Results:
4962
         * The first six examples will return the value as string while the 9th to 12th
4963
         * example returns an empty string. The last example will throw an exception.
4964
         *
4965
         * This does also work for multi-dimensional arrays by passing the keys
4966
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
4967
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
4968
         * public properties of objects or objects implementing __isset() and __get() methods.
4969
         *
4970
         * @param int|string $key Key or path to the requested item
4971
         * @param mixed $default Default value if key isn't found (will be casted to bool)
4972
         * @return string Value from map or default value
4973
         */
4974
        public function string( $key, $default = '' ) : string
4975
        {
4976
                return (string) ( is_scalar( $val = $this->get( $key, $default ) ) ? $val : $default );
24✔
4977
        }
4978

4979

4980
        /**
4981
         * Converts all alphabetic characters in strings to lower case.
4982
         *
4983
         * Examples:
4984
         *  Map::from( ['My String'] )->strLower();
4985
         *  Map::from( ['Τάχιστη'] )->strLower();
4986
         *  Map::from( ['Äpfel', 'Birnen'] )->strLower( 'ISO-8859-1' );
4987
         *  Map::from( [123] )->strLower();
4988
         *  Map::from( [new stdClass] )->strLower();
4989
         *
4990
         * Results:
4991
         * The first example will return ["my string"], the second one ["τάχιστη"] and
4992
         * the third one ["äpfel", "birnen"]. The last two strings will be unchanged.
4993
         *
4994
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
4995
         * @return self<int|string,mixed> Updated map for fluid interface
4996
         */
4997
        public function strLower( string $encoding = 'UTF-8' ) : self
4998
        {
4999
                foreach( $this->list() as &$entry )
8✔
5000
                {
5001
                        if( is_string( $entry ) ) {
8✔
5002
                                $entry = mb_strtolower( $entry, $encoding );
8✔
5003
                        }
5004
                }
5005

5006
                return $this;
8✔
5007
        }
5008

5009

5010
        /**
5011
         * Replaces all occurrences of the search string with the replacement string.
5012
         *
5013
         * Examples:
5014
         * Map::from( ['google.com', 'aimeos.com'] )->strReplace( '.com', '.de' );
5015
         * Map::from( ['google.com', 'aimeos.org'] )->strReplace( ['.com', '.org'], '.de' );
5016
         * Map::from( ['google.com', 'aimeos.org'] )->strReplace( ['.com', '.org'], ['.de'] );
5017
         * Map::from( ['google.com', 'aimeos.org'] )->strReplace( ['.com', '.org'], ['.fr', '.de'] );
5018
         * Map::from( ['google.com', 'aimeos.com'] )->strReplace( ['.com', '.co'], ['.co', '.de', '.fr'] );
5019
         * Map::from( ['google.com', 'aimeos.com', 123] )->strReplace( '.com', '.de' );
5020
         * Map::from( ['GOOGLE.COM', 'AIMEOS.COM'] )->strReplace( '.com', '.de', true );
5021
         *
5022
         * Restults:
5023
         * ['google.de', 'aimeos.de']
5024
         * ['google.de', 'aimeos.de']
5025
         * ['google.de', 'aimeos']
5026
         * ['google.fr', 'aimeos.de']
5027
         * ['google.de', 'aimeos.de']
5028
         * ['google.de', 'aimeos.de', 123]
5029
         * ['GOOGLE.de', 'AIMEOS.de']
5030
         *
5031
         * If you use an array of strings for search or search/replacement, the order of
5032
         * the strings matters! Each search string found is replaced by the corresponding
5033
         * replacement string at the same position.
5034
         *
5035
         * In case of array parameters and if the number of replacement strings is less
5036
         * than the number of search strings, the search strings with no corresponding
5037
         * replacement string are replaced with empty strings. Replacement strings with
5038
         * no corresponding search string are ignored.
5039
         *
5040
         * An array parameter for the replacements is only allowed if the search parameter
5041
         * is an array of strings too!
5042
         *
5043
         * Because the method replaces from left to right, it might replace a previously
5044
         * inserted value when doing multiple replacements. Entries which are non-string
5045
         * values are left untouched.
5046
         *
5047
         * @param array|string $search String or list of strings to search for
5048
         * @param array|string $replace String or list of strings of replacement strings
5049
         * @param bool $case TRUE if replacements should be case insensitive, FALSE if case-sensitive
5050
         * @return self<int|string,mixed> Updated map for fluid interface
5051
         */
5052
        public function strReplace( $search, $replace, bool $case = false ) : self
5053
        {
5054
                $fcn = $case ? 'str_ireplace' : 'str_replace';
8✔
5055

5056
                foreach( $this->list() as &$entry )
8✔
5057
                {
5058
                        if( is_string( $entry ) ) {
8✔
5059
                                $entry = $fcn( $search, $replace, $entry );
8✔
5060
                        }
5061
                }
5062

5063
                return $this;
8✔
5064
        }
5065

5066

5067
        /**
5068
         * Tests if at least one of the entries starts with at least one of the passed strings.
5069
         *
5070
         * Examples:
5071
         *  Map::from( ['abc'] )->strStarts( '' );
5072
         *  Map::from( ['abc'] )->strStarts( 'a' );
5073
         *  Map::from( ['abc'] )->strStarts( 'ab' );
5074
         *  Map::from( ['abc'] )->strStarts( ['a', 'b'] );
5075
         *  Map::from( ['abc'] )->strStarts( 'ab', 'ASCII' );
5076
         *  Map::from( ['abc'] )->strStarts( 'b' );
5077
         *  Map::from( ['abc'] )->strStarts( 'bc' );
5078
         *  Map::from( ['abc'] )->strStarts( ['b', 'c'] );
5079
         *  Map::from( ['abc'] )->strStarts( 'bc', 'ASCII' );
5080
         *
5081
         * Results:
5082
         * The first five examples will return TRUE while the last four will return FALSE.
5083
         *
5084
         * @param array|string $value The string or strings to search for in each entry
5085
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
5086
         * @return bool TRUE if all of the entries ends with at least one of the strings, FALSE if not
5087
         * @todo 4.0 Add $case parameter at second position
5088
         */
5089
        public function strStarts( $value, string $encoding = 'UTF-8' ) : bool
5090
        {
5091
                foreach( $this->list() as $entry )
8✔
5092
                {
5093
                        $entry = (string) $entry;
8✔
5094

5095
                        foreach( (array) $value as $str )
8✔
5096
                        {
5097
                                if( ( $str === '' || mb_strpos( $entry, (string) $str, 0, $encoding ) === 0 ) ) {
8✔
5098
                                        return true;
8✔
5099
                                }
5100
                        }
5101
                }
5102

5103
                return false;
8✔
5104
        }
5105

5106

5107
        /**
5108
         * Tests if all of the entries starts with one of the passed strings.
5109
         *
5110
         * Examples:
5111
         *  Map::from( ['abc', 'def'] )->strStartsAll( '' );
5112
         *  Map::from( ['abc', 'acb'] )->strStartsAll( 'a' );
5113
         *  Map::from( ['abc', 'aba'] )->strStartsAll( 'ab' );
5114
         *  Map::from( ['abc', 'def'] )->strStartsAll( ['a', 'd'] );
5115
         *  Map::from( ['abc', 'acf'] )->strStartsAll( 'a', 'ASCII' );
5116
         *  Map::from( ['abc', 'def'] )->strStartsAll( 'd' );
5117
         *  Map::from( ['abc', 'bca'] )->strStartsAll( 'ab' );
5118
         *  Map::from( ['abc', 'bac'] )->strStartsAll( ['a', 'c'] );
5119
         *  Map::from( ['abc', 'cab'] )->strStartsAll( 'ab', 'ASCII' );
5120
         *
5121
         * Results:
5122
         * The first five examples will return TRUE while the last four will return FALSE.
5123
         *
5124
         * @param array|string $value The string or strings to search for in each entry
5125
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
5126
         * @return bool TRUE if one of the entries starts with one of the strings, FALSE if not
5127
         * @todo 4.0 Add $case parameter at second position
5128
         */
5129
        public function strStartsAll( $value, string $encoding = 'UTF-8' ) : bool
5130
        {
5131
                $list = [];
8✔
5132

5133
                foreach( $this->list() as $entry )
8✔
5134
                {
5135
                        $entry = (string) $entry;
8✔
5136
                        $list[$entry] = 0;
8✔
5137

5138
                        foreach( (array) $value as $str )
8✔
5139
                        {
5140
                                if( (int) ( $str === '' || mb_strpos( $entry, (string) $str, 0, $encoding ) === 0 ) ) {
8✔
5141
                                        $list[$entry] = 1; break;
8✔
5142
                                }
5143
                        }
5144
                }
5145

5146
                return array_sum( $list ) === count( $list );
8✔
5147
        }
5148

5149

5150
        /**
5151
         * Converts all alphabetic characters in strings to upper case.
5152
         *
5153
         * Examples:
5154
         *  Map::from( ['My String'] )->strUpper();
5155
         *  Map::from( ['τάχιστη'] )->strUpper();
5156
         *  Map::from( ['äpfel', 'birnen'] )->strUpper( 'ISO-8859-1' );
5157
         *  Map::from( [123] )->strUpper();
5158
         *  Map::from( [new stdClass] )->strUpper();
5159
         *
5160
         * Results:
5161
         * The first example will return ["MY STRING"], the second one ["ΤΆΧΙΣΤΗ"] and
5162
         * the third one ["ÄPFEL", "BIRNEN"]. The last two strings will be unchanged.
5163
         *
5164
         * @param string $encoding Character encoding of the strings, e.g. "UTF-8" (default), "ASCII", "ISO-8859-1", etc.
5165
         * @return self<int|string,mixed> Updated map for fluid interface
5166
         */
5167
        public function strUpper( string $encoding = 'UTF-8' ) :self
5168
        {
5169
                foreach( $this->list() as &$entry )
8✔
5170
                {
5171
                        if( is_string( $entry ) ) {
8✔
5172
                                $entry = mb_strtoupper( $entry, $encoding );
8✔
5173
                        }
5174
                }
5175

5176
                return $this;
8✔
5177
        }
5178

5179

5180
        /**
5181
         * Adds a suffix at the end of each map entry.
5182
         *
5183
         * By defaul, nested arrays are walked recusively so all entries at all levels are suffixed.
5184
         *
5185
         * Examples:
5186
         *  Map::from( ['a', 'b'] )->suffix( '-1' );
5187
         *  Map::from( ['a', ['b']] )->suffix( '-1' );
5188
         *  Map::from( ['a', ['b']] )->suffix( '-1', 1 );
5189
         *  Map::from( ['a', 'b'] )->suffix( function( $item, $key ) {
5190
         *      return '-' . ( ord( $item ) + ord( $key ) );
5191
         *  } );
5192
         *
5193
         * Results:
5194
         *  The first example returns ['a-1', 'b-1'] while the second one will return
5195
         *  ['a-1', ['b-1']]. In the third example, the depth is limited to the first
5196
         *  level only so it will return ['a-1', ['b']]. The forth example passing
5197
         *  the closure will return ['a-145', 'b-147'].
5198
         *
5199
         * The keys are preserved using this method.
5200
         *
5201
         * @param \Closure|string $suffix Suffix string or anonymous function with ($item, $key) as parameters
5202
         * @param int|null $depth Maximum depth to dive into multi-dimensional arrays starting from "1"
5203
         * @return self<int|string,mixed> Updated map for fluid interface
5204
         */
5205
        public function suffix( $suffix, ?int $depth = null ) : self
5206
        {
5207
                $fcn = function( $list, $suffix, $depth ) use ( &$fcn ) {
6✔
5208

5209
                        foreach( $list as $key => $item )
8✔
5210
                        {
5211
                                if( is_array( $item ) ) {
8✔
5212
                                        $list[$key] = $depth > 1 ? $fcn( $item, $suffix, $depth - 1 ) : $item;
8✔
5213
                                } else {
5214
                                        $list[$key] = $item . ( is_callable( $suffix ) ? $suffix( $item, $key ) : $suffix );
8✔
5215
                                }
5216
                        }
5217

5218
                        return $list;
8✔
5219
                };
8✔
5220

5221
                $this->list = $fcn( $this->list(), $suffix, $depth ?? 0x7fffffff );
8✔
5222
                return $this;
8✔
5223
        }
5224

5225

5226
        /**
5227
         * Returns the sum of all integer and float values in the map.
5228
         *
5229
         * Examples:
5230
         *  Map::from( [1, 3, 5] )->sum();
5231
         *  Map::from( [1, 'sum', 5] )->sum();
5232
         *  Map::from( [['p' => 30], ['p' => 50], ['p' => 10]] )->sum( 'p' );
5233
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->sum( 'i/p' );
5234
         *  Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->sum( fn( $val, $key ) => $val['i']['p'] ?? null )
5235
         *  Map::from( [30, 50, 10] )->sum( fn( $val, $key ) => $val < 50 ? $val : null )
5236
         *
5237
         * Results:
5238
         * The first line will return "9", the second one "6", the third one "90"
5239
         * the forth/fifth "80" and the last one "40".
5240
         *
5241
         * Non-numeric values will be removed before calculation.
5242
         *
5243
         * NULL values are treated as 0, non-numeric values will generate an error.
5244
         *
5245
         * This does also work for multi-dimensional arrays by passing the keys
5246
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
5247
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
5248
         * public properties of objects or objects implementing __isset() and __get() methods.
5249
         *
5250
         * @param Closure|string|null $col Closure, key or path to the values in the nested array or object to sum up
5251
         * @return float Sum of all elements or 0 if there are no elements in the map
5252
         */
5253
        public function sum( $col = null ) : float
5254
        {
5255
                $list = $this->list();
24✔
5256
                $vals = array_filter( $col ? array_map( $this->mapper( $col ), $list, array_keys( $list ) ) : $list, 'is_numeric' );
24✔
5257

5258
                return array_sum( $vals );
24✔
5259
        }
5260

5261

5262
        /**
5263
         * Returns a new map with the given number of items.
5264
         *
5265
         * The keys of the items returned in the new map are the same as in the original one.
5266
         *
5267
         * Examples:
5268
         *  Map::from( [1, 2, 3, 4] )->take( 2 );
5269
         *  Map::from( [1, 2, 3, 4] )->take( 2, 1 );
5270
         *  Map::from( [1, 2, 3, 4] )->take( 2, -2 );
5271
         *  Map::from( [1, 2, 3, 4] )->take( 2, function( $item, $key ) {
5272
         *      return $item < 2;
5273
         *  } );
5274
         *
5275
         * Results:
5276
         *  [0 => 1, 1 => 2]
5277
         *  [1 => 2, 2 => 3]
5278
         *  [2 => 3, 3 => 4]
5279
         *  [1 => 2, 2 => 3]
5280
         *
5281
         * The keys of the items returned in the new map are the same as in the original one.
5282
         *
5283
         * @param int $size Number of items to return
5284
         * @param \Closure|int $offset Number of items to skip or function($item, $key) returning true for skipped items
5285
         * @return self<int|string,mixed> New map
5286
         */
5287
        public function take( int $size, $offset = 0 ) : self
5288
        {
5289
                $list = $this->list();
40✔
5290

5291
                if( is_numeric( $offset ) ) {
40✔
5292
                        return new static( array_slice( $list, (int) $offset, $size, true ) );
24✔
5293
                }
5294

5295
                if( $offset instanceof \Closure ) {
16✔
5296
                        return new static( array_slice( $list, $this->until( $list, $offset ), $size, true ) );
8✔
5297
                }
5298

5299
                throw new \InvalidArgumentException( 'Only an integer or a closure is allowed as second argument for take()' );
8✔
5300
        }
5301

5302

5303
        /**
5304
         * Passes a clone of the map to the given callback.
5305
         *
5306
         * Use it to "tap" into a chain of methods to check the state between two
5307
         * method calls. The original map is not altered by anything done in the
5308
         * callback.
5309
         *
5310
         * Examples:
5311
         *  Map::from( [3, 2, 1] )->rsort()->tap( function( $map ) {
5312
         *    print_r( $map->remove( 0 )->toArray() );
5313
         *  } )->first();
5314
         *
5315
         * Results:
5316
         * It will sort the list in reverse order (`[1, 2, 3]`) while keeping the keys,
5317
         * then prints the items without the first (`[2, 3]`) in the function passed
5318
         * to `tap()` and returns the first item ("1") at the end.
5319
         *
5320
         * @param callable $callback Function receiving ($map) parameter
5321
         * @return self<int|string,mixed> Same map for fluid interface
5322
         */
5323
        public function tap( callable $callback ) : self
5324
        {
5325
                $callback( clone $this );
8✔
5326
                return $this;
8✔
5327
        }
5328

5329

5330
        /**
5331
         * Returns the elements as a plain array.
5332
         *
5333
         * @return array<int|string,mixed> Plain array
5334
         */
5335
        public function to() : array
5336
        {
5337
                return $this->list = $this->array( $this->list );
8✔
5338
        }
5339

5340

5341
        /**
5342
         * Returns the elements as a plain array.
5343
         *
5344
         * @return array<int|string,mixed> Plain array
5345
         */
5346
        public function toArray() : array
5347
        {
5348
                return $this->list = $this->array( $this->list );
1,920✔
5349
        }
5350

5351

5352
        /**
5353
         * Returns the elements encoded as JSON string.
5354
         *
5355
         * There are several options available to modify the JSON output:
5356
         * {@link https://www.php.net/manual/en/function.json-encode.php}
5357
         * The parameter can be a single JSON_* constant or a bitmask of several
5358
         * constants combine by bitwise OR (|), e.g.:
5359
         *
5360
         *  JSON_FORCE_OBJECT|JSON_HEX_QUOT
5361
         *
5362
         * @param int $options Combination of JSON_* constants
5363
         * @return string|null Array encoded as JSON string or NULL on failure
5364
         */
5365
        public function toJson( int $options = 0 ) : ?string
5366
        {
5367
                $result = json_encode( $this->list(), $options );
16✔
5368
                return $result !== false ? $result : null;
16✔
5369
        }
5370

5371

5372
        /**
5373
         * Reverses the element order in a copy of the map (alias).
5374
         *
5375
         * This method is an alias for reversed(). For performance reasons, reversed() should be
5376
         * preferred because it uses one method call less than toReversed().
5377
         *
5378
         * @return self<int|string,mixed> New map with a reversed copy of the elements
5379
         * @see reversed() - Underlying method with same parameters and return value but better performance
5380
         */
5381
        public function toReversed() : self
5382
        {
5383
                return $this->reversed();
8✔
5384
        }
5385

5386

5387
        /**
5388
         * Sorts the elements in a copy of the map using new keys (alias).
5389
         *
5390
         * This method is an alias for sorted(). For performance reasons, sorted() should be
5391
         * preferred because it uses one method call less than toSorted().
5392
         *
5393
         * @param int $options Sort options for PHP sort()
5394
         * @return self<int|string,mixed> New map with a sorted copy of the elements
5395
         * @see sorted() - Underlying method with same parameters and return value but better performance
5396
         */
5397
        public function toSorted( int $options = SORT_REGULAR ) : self
5398
        {
5399
                return $this->sorted( $options );
8✔
5400
        }
5401

5402

5403
        /**
5404
         * Creates a HTTP query string from the map elements.
5405
         *
5406
         * Examples:
5407
         *  Map::from( ['a' => 1, 'b' => 2] )->toUrl();
5408
         *  Map::from( ['a' => ['b' => 'abc', 'c' => 'def'], 'd' => 123] )->toUrl();
5409
         *
5410
         * Results:
5411
         *  a=1&b=2
5412
         *  a%5Bb%5D=abc&a%5Bc%5D=def&d=123
5413
         *
5414
         * @return string Parameter string for GET requests
5415
         */
5416
        public function toUrl() : string
5417
        {
5418
                return http_build_query( $this->list(), '', '&', PHP_QUERY_RFC3986 );
16✔
5419
        }
5420

5421

5422
        /**
5423
         * Creates new key/value pairs using the passed function and returns a new map for the result.
5424
         *
5425
         * Examples:
5426
         *  Map::from( ['a' => 2, 'b' => 4] )->transform( function( $value, $key ) {
5427
         *      return [$key . '-2' => $value * 2];
5428
         *  } );
5429
         *  Map::from( ['a' => 2, 'b' => 4] )->transform( function( $value, $key ) {
5430
         *      return [$key => $value * 2, $key . $key => $value * 4];
5431
         *  } );
5432
         *  Map::from( ['a' => 2, 'b' => 4] )->transform( function( $value, $key ) {
5433
         *      return $key < 'b' ? [$key => $value * 2] : null;
5434
         *  } );
5435
         *  Map::from( ['la' => 2, 'le' => 4, 'li' => 6] )->transform( function( $value, $key ) {
5436
         *      return [$key[0] => $value * 2];
5437
         *  } );
5438
         *
5439
         * Results:
5440
         *  ['a-2' => 4, 'b-2' => 8]
5441
         *  ['a' => 4, 'aa' => 8, 'b' => 8, 'bb' => 16]
5442
         *  ['a' => 4]
5443
         *  ['l' => 12]
5444
         *
5445
         * If a key is returned twice, the last value will overwrite previous values.
5446
         *
5447
         * @param \Closure $callback Function with (value, key) parameters and returns an array of new key/value pair(s)
5448
         * @return self<int|string,mixed> New map with the new key/value pairs
5449
         * @see map() - Maps new values to the existing keys using the passed function and returns a new map for the result
5450
         * @see rekey() - Changes the keys according to the passed function
5451
         */
5452
        public function transform( \Closure $callback ) : self
5453
        {
5454
                $result = [];
32✔
5455

5456
                foreach( $this->list() as $key => $value )
32✔
5457
                {
5458
                        foreach( (array) $callback( $value, $key ) as $newkey => $newval ) {
32✔
5459
                                $result[$newkey] = $newval;
32✔
5460
                        }
5461
                }
5462

5463
                return new static( $result );
32✔
5464
        }
5465

5466

5467
        /**
5468
         * Exchanges rows and columns for a two dimensional map.
5469
         *
5470
         * Examples:
5471
         *  Map::from( [
5472
         *    ['name' => 'A', 2020 => 200, 2021 => 100, 2022 => 50],
5473
         *    ['name' => 'B', 2020 => 300, 2021 => 200, 2022 => 100],
5474
         *    ['name' => 'C', 2020 => 400, 2021 => 300, 2022 => 200],
5475
         *  ] )->transpose();
5476
         *
5477
         *  Map::from( [
5478
         *    ['name' => 'A', 2020 => 200, 2021 => 100, 2022 => 50],
5479
         *    ['name' => 'B', 2020 => 300, 2021 => 200],
5480
         *    ['name' => 'C', 2020 => 400]
5481
         *  ] );
5482
         *
5483
         * Results:
5484
         *  [
5485
         *    'name' => ['A', 'B', 'C'],
5486
         *    2020 => [200, 300, 400],
5487
         *    2021 => [100, 200, 300],
5488
         *    2022 => [50, 100, 200]
5489
         *  ]
5490
         *
5491
         *  [
5492
         *    'name' => ['A', 'B', 'C'],
5493
         *    2020 => [200, 300, 400],
5494
         *    2021 => [100, 200],
5495
         *    2022 => [50]
5496
         *  ]
5497
         *
5498
         * @return self<int|string,mixed> New map
5499
         */
5500
        public function transpose() : self
5501
        {
5502
                $result = [];
16✔
5503

5504
                foreach( (array) $this->first( [] ) as $key => $col ) {
16✔
5505
                        $result[$key] = array_column( $this->list(), $key );
16✔
5506
                }
5507

5508
                return new static( $result );
16✔
5509
        }
5510

5511

5512
        /**
5513
         * Traverses trees of nested items passing each item to the callback.
5514
         *
5515
         * This does work for nested arrays and objects with public properties or
5516
         * objects implementing __isset() and __get() methods. To build trees
5517
         * of nested items, use the tree() method.
5518
         *
5519
         * Examples:
5520
         *   Map::from( [[
5521
         *     'id' => 1, 'pid' => null, 'name' => 'n1', 'children' => [
5522
         *       ['id' => 2, 'pid' => 1, 'name' => 'n2', 'children' => []],
5523
         *       ['id' => 3, 'pid' => 1, 'name' => 'n3', 'children' => []]
5524
         *     ]
5525
         *   ]] )->traverse();
5526
         *
5527
         *   Map::from( [[
5528
         *     'id' => 1, 'pid' => null, 'name' => 'n1', 'children' => [
5529
         *       ['id' => 2, 'pid' => 1, 'name' => 'n2', 'children' => []],
5530
         *       ['id' => 3, 'pid' => 1, 'name' => 'n3', 'children' => []]
5531
         *     ]
5532
         *   ]] )->traverse( function( $entry, $key, $level, $parent ) {
5533
         *     return str_repeat( '-', $level ) . '- ' . $entry['name'];
5534
         *   } );
5535
         *
5536
         *   Map::from( [[
5537
         *     'id' => 1, 'pid' => null, 'name' => 'n1', 'children' => [
5538
         *       ['id' => 2, 'pid' => 1, 'name' => 'n2', 'children' => []],
5539
         *       ['id' => 3, 'pid' => 1, 'name' => 'n3', 'children' => []]
5540
         *     ]
5541
         *   ]] )->traverse( function( &$entry, $key, $level, $parent ) {
5542
         *     $entry['path'] = isset( $parent['path'] ) ? $parent['path'] . '/' . $entry['name'] : $entry['name'];
5543
         *     return $entry;
5544
         *   } );
5545
         *
5546
         *   Map::from( [[
5547
         *     'id' => 1, 'pid' => null, 'name' => 'n1', 'nodes' => [
5548
         *       ['id' => 2, 'pid' => 1, 'name' => 'n2', 'nodes' => []]
5549
         *     ]
5550
         *   ]] )->traverse( null, 'nodes' );
5551
         *
5552
         * Results:
5553
         *   [
5554
         *     ['id' => 1, 'pid' => null, 'name' => 'n1', 'children' => [...]],
5555
         *     ['id' => 2, 'pid' => 1, 'name' => 'n2', 'children' => []],
5556
         *     ['id' => 3, 'pid' => 1, 'name' => 'n3', 'children' => []],
5557
         *   ]
5558
         *
5559
         *   ['- n1', '-- n2', '-- n3']
5560
         *
5561
         *   [
5562
         *     ['id' => 1, 'pid' => null, 'name' => 'n1', 'children' => [...], 'path' => 'n1'],
5563
         *     ['id' => 2, 'pid' => 1, 'name' => 'n2', 'children' => [], 'path' => 'n1/n2'],
5564
         *     ['id' => 3, 'pid' => 1, 'name' => 'n3', 'children' => [], 'path' => 'n1/n3'],
5565
         *   ]
5566
         *
5567
         *   [
5568
         *     ['id' => 1, 'pid' => null, 'name' => 'n1', 'nodes' => [...]],
5569
         *     ['id' => 2, 'pid' => 1, 'name' => 'n2', 'nodes' => []],
5570
         *   ]
5571
         *
5572
         * @param \Closure|null $callback Callback with (entry, key, level, $parent) arguments, returns the entry added to result
5573
         * @param string $nestKey Key to the children of each item
5574
         * @return self<int|string,mixed> New map with all items as flat list
5575
         */
5576
        public function traverse( ?\Closure $callback = null, string $nestKey = 'children' ) : self
5577
        {
5578
                $result = [];
40✔
5579
                $this->visit( $this->list(), $result, 0, $callback, $nestKey );
40✔
5580

5581
                return map( $result );
40✔
5582
        }
5583

5584

5585
        /**
5586
         * Creates a tree structure from the list items.
5587
         *
5588
         * Use this method to rebuild trees e.g. from database records. To traverse
5589
         * trees, use the traverse() method.
5590
         *
5591
         * Examples:
5592
         *  Map::from( [
5593
         *    ['id' => 1, 'pid' => null, 'lvl' => 0, 'name' => 'n1'],
5594
         *    ['id' => 2, 'pid' => 1, 'lvl' => 1, 'name' => 'n2'],
5595
         *    ['id' => 3, 'pid' => 2, 'lvl' => 2, 'name' => 'n3'],
5596
         *    ['id' => 4, 'pid' => 1, 'lvl' => 1, 'name' => 'n4'],
5597
         *    ['id' => 5, 'pid' => 3, 'lvl' => 2, 'name' => 'n5'],
5598
         *    ['id' => 6, 'pid' => 1, 'lvl' => 1, 'name' => 'n6'],
5599
         *  ] )->tree( 'id', 'pid' );
5600
         *
5601
         * Results:
5602
         *   [1 => [
5603
         *     'id' => 1, 'pid' => null, 'lvl' => 0, 'name' => 'n1', 'children' => [
5604
         *       2 => ['id' => 2, 'pid' => 1, 'lvl' => 1, 'name' => 'n2', 'children' => [
5605
         *         3 => ['id' => 3, 'pid' => 2, 'lvl' => 2, 'name' => 'n3', 'children' => []]
5606
         *       ]],
5607
         *       4 => ['id' => 4, 'pid' => 1, 'lvl' => 1, 'name' => 'n4', 'children' => [
5608
         *         5 => ['id' => 5, 'pid' => 3, 'lvl' => 2, 'name' => 'n5', 'children' => []]
5609
         *       ]],
5610
         *       6 => ['id' => 6, 'pid' => 1, 'lvl' => 1, 'name' => 'n6', 'children' => []]
5611
         *     ]
5612
         *   ]]
5613
         *
5614
         * To build the tree correctly, the items must be in order or at least the
5615
         * nodes of the lower levels must come first. For a tree like this:
5616
         * n1
5617
         * |- n2
5618
         * |  |- n3
5619
         * |- n4
5620
         * |  |- n5
5621
         * |- n6
5622
         *
5623
         * Accepted item order:
5624
         * - in order: n1, n2, n3, n4, n5, n6
5625
         * - lower levels first: n1, n2, n4, n6, n3, n5
5626
         *
5627
         * If your items are unordered, apply usort() first to the map entries, e.g.
5628
         *   Map::from( [['id' => 3, 'lvl' => 2], ...] )->usort( function( $item1, $item2 ) {
5629
         *     return $item1['lvl'] <=> $item2['lvl'];
5630
         *   } );
5631
         *
5632
         * @param string $idKey Name of the key with the unique ID of the node
5633
         * @param string $parentKey Name of the key with the ID of the parent node
5634
         * @param string $nestKey Name of the key with will contain the children of the node
5635
         * @return self<int|string,mixed> New map with one or more root tree nodes
5636
         */
5637
        public function tree( string $idKey, string $parentKey, string $nestKey = 'children' ) : self
5638
        {
5639
                $this->list();
8✔
5640
                $trees = $refs = [];
8✔
5641

5642
                foreach( $this->list as &$node )
8✔
5643
                {
5644
                        $node[$nestKey] = [];
8✔
5645
                        $refs[$node[$idKey]] = &$node;
8✔
5646

5647
                        if( $node[$parentKey] ) {
8✔
5648
                                $refs[$node[$parentKey]][$nestKey][$node[$idKey]] = &$node;
8✔
5649
                        } else {
5650
                                $trees[$node[$idKey]] = &$node;
8✔
5651
                        }
5652
                }
5653

5654
                return map( $trees );
8✔
5655
        }
5656

5657

5658
        /**
5659
         * Removes the passed characters from the left/right of all strings.
5660
         *
5661
         * Examples:
5662
         *  Map::from( [" abc\n", "\tcde\r\n"] )->trim();
5663
         *  Map::from( ["a b c", "cbax"] )->trim( 'abc' );
5664
         *
5665
         * Results:
5666
         * The first example will return ["abc", "cde"] while the second one will return [" b ", "x"].
5667
         *
5668
         * @param string $chars List of characters to trim
5669
         * @return self<int|string,mixed> Updated map for fluid interface
5670
         */
5671
        public function trim( string $chars = " \n\r\t\v\x00" ) : self
5672
        {
5673
                foreach( $this->list() as &$entry )
8✔
5674
                {
5675
                        if( is_string( $entry ) ) {
8✔
5676
                                $entry = trim( $entry, $chars );
8✔
5677
                        }
5678
                }
5679

5680
                return $this;
8✔
5681
        }
5682

5683

5684
        /**
5685
         * Sorts all elements using a callback and maintains the key association.
5686
         *
5687
         * The given callback will be used to compare the values. The callback must accept
5688
         * two parameters (item A and B) and must return -1 if item A is smaller than
5689
         * item B, 0 if both are equal and 1 if item A is greater than item B. Both, a
5690
         * method name and an anonymous function can be passed.
5691
         *
5692
         * Examples:
5693
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->uasort( 'strcasecmp' );
5694
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->uasort( function( $itemA, $itemB ) {
5695
         *      return strtolower( $itemA ) <=> strtolower( $itemB );
5696
         *  } );
5697
         *
5698
         * Results:
5699
         *  ['b' => 'a', 'a' => 'B']
5700
         *  ['b' => 'a', 'a' => 'B']
5701
         *
5702
         * The keys are preserved using this method and no new map is created.
5703
         *
5704
         * @param callable $callback Function with (itemA, itemB) parameters and returns -1 (<), 0 (=) and 1 (>)
5705
         * @return self<int|string,mixed> Updated map for fluid interface
5706
         */
5707
        public function uasort( callable $callback ) : self
5708
        {
5709
                uasort( $this->list(), $callback );
16✔
5710
                return $this;
16✔
5711
        }
5712

5713

5714
        /**
5715
         * Sorts all elements using a callback and maintains the key association.
5716
         *
5717
         * The given callback will be used to compare the values. The callback must accept
5718
         * two parameters (item A and B) and must return -1 if item A is smaller than
5719
         * item B, 0 if both are equal and 1 if item A is greater than item B. Both, a
5720
         * method name and an anonymous function can be passed.
5721
         *
5722
         * Examples:
5723
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->uasorted( 'strcasecmp' );
5724
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->uasorted( function( $itemA, $itemB ) {
5725
         *      return strtolower( $itemA ) <=> strtolower( $itemB );
5726
         *  } );
5727
         *
5728
         * Results:
5729
         *  ['b' => 'a', 'a' => 'B']
5730
         *  ['b' => 'a', 'a' => 'B']
5731
         *
5732
         * The keys are preserved using this method and a new map is created.
5733
         *
5734
         * @param callable $callback Function with (itemA, itemB) parameters and returns -1 (<), 0 (=) and 1 (>)
5735
         * @return self<int|string,mixed> Updated map for fluid interface
5736
         */
5737
        public function uasorted( callable $callback ) : self
5738
        {
5739
                return ( clone $this )->uasort( $callback );
8✔
5740
        }
5741

5742

5743
        /**
5744
         * Sorts the map elements by their keys using a callback.
5745
         *
5746
         * The given callback will be used to compare the keys. The callback must accept
5747
         * two parameters (key A and B) and must return -1 if key A is smaller than
5748
         * key B, 0 if both are equal and 1 if key A is greater than key B. Both, a
5749
         * method name and an anonymous function can be passed.
5750
         *
5751
         * Examples:
5752
         *  Map::from( ['B' => 'a', 'a' => 'b'] )->uksort( 'strcasecmp' );
5753
         *  Map::from( ['B' => 'a', 'a' => 'b'] )->uksort( function( $keyA, $keyB ) {
5754
         *      return strtolower( $keyA ) <=> strtolower( $keyB );
5755
         *  } );
5756
         *
5757
         * Results:
5758
         *  ['a' => 'b', 'B' => 'a']
5759
         *  ['a' => 'b', 'B' => 'a']
5760
         *
5761
         * The keys are preserved using this method and no new map is created.
5762
         *
5763
         * @param callable $callback Function with (keyA, keyB) parameters and returns -1 (<), 0 (=) and 1 (>)
5764
         * @return self<int|string,mixed> Updated map for fluid interface
5765
         */
5766
        public function uksort( callable $callback ) : self
5767
        {
5768
                uksort( $this->list(), $callback );
16✔
5769
                return $this;
16✔
5770
        }
5771

5772

5773
        /**
5774
         * Sorts a copy of the map elements by their keys using a callback.
5775
         *
5776
         * The given callback will be used to compare the keys. The callback must accept
5777
         * two parameters (key A and B) and must return -1 if key A is smaller than
5778
         * key B, 0 if both are equal and 1 if key A is greater than key B. Both, a
5779
         * method name and an anonymous function can be passed.
5780
         *
5781
         * Examples:
5782
         *  Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( 'strcasecmp' );
5783
         *  Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( function( $keyA, $keyB ) {
5784
         *      return strtolower( $keyA ) <=> strtolower( $keyB );
5785
         *  } );
5786
         *
5787
         * Results:
5788
         *  ['a' => 'b', 'B' => 'a']
5789
         *  ['a' => 'b', 'B' => 'a']
5790
         *
5791
         * The keys are preserved using this method and a new map is created.
5792
         *
5793
         * @param callable $callback Function with (keyA, keyB) parameters and returns -1 (<), 0 (=) and 1 (>)
5794
         * @return self<int|string,mixed> Updated map for fluid interface
5795
         */
5796
        public function uksorted( callable $callback ) : self
5797
        {
5798
                return ( clone $this )->uksort( $callback );
8✔
5799
        }
5800

5801

5802
        /**
5803
         * Builds a union of the elements and the given elements without overwriting existing ones.
5804
         * Existing keys in the map will not be overwritten
5805
         *
5806
         * Examples:
5807
         *  Map::from( [0 => 'a', 1 => 'b'] )->union( [0 => 'c'] );
5808
         *  Map::from( ['a' => 1, 'b' => 2] )->union( ['c' => 1] );
5809
         *
5810
         * Results:
5811
         * The first example will result in [0 => 'a', 1 => 'b'] because the key 0
5812
         * isn't overwritten. In the second example, the result will be a combined
5813
         * list: ['a' => 1, 'b' => 2, 'c' => 1].
5814
         *
5815
         * If list entries should be overwritten,  please use merge() instead!
5816
         * The keys are preserved using this method and no new map is created.
5817
         *
5818
         * @param iterable<int|string,mixed> $elements List of elements
5819
         * @return self<int|string,mixed> Updated map for fluid interface
5820
         */
5821
        public function union( iterable $elements ) : self
5822
        {
5823
                $this->list = $this->list() + $this->array( $elements );
16✔
5824
                return $this;
16✔
5825
        }
5826

5827

5828
        /**
5829
         * Returns only unique elements from the map incl. their keys.
5830
         *
5831
         * Examples:
5832
         *  Map::from( [0 => 'a', 1 => 'b', 2 => 'b', 3 => 'c'] )->unique();
5833
         *  Map::from( [['p' => '1'], ['p' => 1], ['p' => 2]] )->unique( 'p' )
5834
         *  Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->unique( 'i/p' )
5835
         *  Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->unique( fn( $item, $key ) => $item['i']['p'] )
5836
         *
5837
         * Results:
5838
         * [0 => 'a', 1 => 'b', 3 => 'c']
5839
         * [['p' => 1], ['p' => 2]]
5840
         * [['i' => ['p' => '1']]]
5841
         * [['i' => ['p' => '1']]]
5842
         *
5843
         * Two elements are considered equal if comparing their string representions returns TRUE:
5844
         * (string) $elem1 === (string) $elem2
5845
         *
5846
         * The keys of the elements are only preserved in the new map if no key is passed.
5847
         *
5848
         * @param \Closure|string|null $col Key, path of the nested array or anonymous function with ($item, $key) parameters returning the value for comparison
5849
         * @return self<int|string,mixed> New map
5850
         */
5851
        public function unique( $col = null ) : self
5852
        {
5853
                if( $col === null ) {
40✔
5854
                        return new static( array_unique( $this->list() ) );
16✔
5855
                }
5856

5857
                $list = $this->list();
24✔
5858
                $map = array_map( $this->mapper( $col ), array_values( $list ), array_keys( $list ) );
24✔
5859

5860
                return new static( array_intersect_key( $list, array_unique( $map ) ) );
24✔
5861
        }
5862

5863

5864
        /**
5865
         * Pushes an element onto the beginning of the map without returning a new map.
5866
         *
5867
         * Examples:
5868
         *  Map::from( ['a', 'b'] )->unshift( 'd' );
5869
         *  Map::from( ['a', 'b'] )->unshift( 'd', 'first' );
5870
         *
5871
         * Results:
5872
         *  ['d', 'a', 'b']
5873
         *  ['first' => 'd', 0 => 'a', 1 => 'b']
5874
         *
5875
         * The keys of the elements are only preserved in the new map if no key is passed.
5876
         *
5877
         * Performance note:
5878
         * The bigger the list, the higher the performance impact because unshift()
5879
         * needs to create a new list and copies all existing elements to the new
5880
         * array. Usually, it's better to push() new entries at the end and reverse()
5881
         * the list afterwards:
5882
         *
5883
         *  $map->push( 'a' )->push( 'b' )->reverse();
5884
         * instead of
5885
         *  $map->unshift( 'a' )->unshift( 'b' );
5886
         *
5887
         * @param mixed $value Item to add at the beginning
5888
         * @param int|string|null $key Key for the item or NULL to reindex all numerical keys
5889
         * @return self<int|string,mixed> Updated map for fluid interface
5890
         */
5891
        public function unshift( $value, $key = null ) : self
5892
        {
5893
                if( $key === null ) {
24✔
5894
                        array_unshift( $this->list(), $value );
16✔
5895
                } else {
5896
                        $this->list = [$key => $value] + $this->list();
8✔
5897
                }
5898

5899
                return $this;
24✔
5900
        }
5901

5902

5903
        /**
5904
         * Sorts all elements using a callback using new keys.
5905
         *
5906
         * The given callback will be used to compare the values. The callback must accept
5907
         * two parameters (item A and B) and must return -1 if item A is smaller than
5908
         * item B, 0 if both are equal and 1 if item A is greater than item B. Both, a
5909
         * method name and an anonymous function can be passed.
5910
         *
5911
         * Examples:
5912
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->usort( 'strcasecmp' );
5913
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->usort( function( $itemA, $itemB ) {
5914
         *      return strtolower( $itemA ) <=> strtolower( $itemB );
5915
         *  } );
5916
         *
5917
         * Results:
5918
         *  [0 => 'a', 1 => 'B']
5919
         *  [0 => 'a', 1 => 'B']
5920
         *
5921
         * The keys aren't preserved and elements get a new index. No new map is created.
5922
         *
5923
         * @param callable $callback Function with (itemA, itemB) parameters and returns -1 (<), 0 (=) and 1 (>)
5924
         * @return self<int|string,mixed> Updated map for fluid interface
5925
         */
5926
        public function usort( callable $callback ) : self
5927
        {
5928
                usort( $this->list(), $callback );
16✔
5929
                return $this;
16✔
5930
        }
5931

5932

5933
        /**
5934
         * Sorts a copy of all elements using a callback using new keys.
5935
         *
5936
         * The given callback will be used to compare the values. The callback must accept
5937
         * two parameters (item A and B) and must return -1 if item A is smaller than
5938
         * item B, 0 if both are equal and 1 if item A is greater than item B. Both, a
5939
         * method name and an anonymous function can be passed.
5940
         *
5941
         * Examples:
5942
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->usorted( 'strcasecmp' );
5943
         *  Map::from( ['a' => 'B', 'b' => 'a'] )->usorted( function( $itemA, $itemB ) {
5944
         *      return strtolower( $itemA ) <=> strtolower( $itemB );
5945
         *  } );
5946
         *
5947
         * Results:
5948
         *  [0 => 'a', 1 => 'B']
5949
         *  [0 => 'a', 1 => 'B']
5950
         *
5951
         * The keys aren't preserved, elements get a new index and a new map is created.
5952
         *
5953
         * @param callable $callback Function with (itemA, itemB) parameters and returns -1 (<), 0 (=) and 1 (>)
5954
         * @return self<int|string,mixed> Updated map for fluid interface
5955
         */
5956
        public function usorted( callable $callback ) : self
5957
        {
5958
                return ( clone $this )->usort( $callback );
8✔
5959
        }
5960

5961

5962
        /**
5963
         * Resets the keys and return the values in a new map.
5964
         *
5965
         * Examples:
5966
         *  Map::from( ['x' => 'b', 2 => 'a', 'c'] )->values();
5967
         *
5968
         * Results:
5969
         * A new map with [0 => 'b', 1 => 'a', 2 => 'c'] as content
5970
         *
5971
         * @return self<int|string,mixed> New map of the values
5972
         */
5973
        public function values() : self
5974
        {
5975
                return new static( array_values( $this->list() ) );
80✔
5976
        }
5977

5978

5979
        /**
5980
         * Applies the given callback to all elements.
5981
         *
5982
         * To change the values of the Map, specify the value parameter as reference
5983
         * (&$value). You can only change the values but not the keys nor the array
5984
         * structure.
5985
         *
5986
         * Examples:
5987
         *  Map::from( ['a', 'B', ['c', 'd'], 'e'] )->walk( function( &$value ) {
5988
         *    $value = strtoupper( $value );
5989
         *  } );
5990
         *  Map::from( [66 => 'B', 97 => 'a'] )->walk( function( $value, $key ) {
5991
         *    echo 'ASCII ' . $key . ' is ' . $value . "\n";
5992
         *  } );
5993
         *  Map::from( [1, 2, 3] )->walk( function( &$value, $key, $data ) {
5994
         *    $value = $data[$value] ?? $value;
5995
         *  }, [1 => 'one', 2 => 'two'] );
5996
         *
5997
         * Results:
5998
         * The first example will change the Map elements to:
5999
         *   ['A', 'B', ['C', 'D'], 'E']
6000
         * The output of the second one will be:
6001
         *  ASCII 66 is B
6002
         *  ASCII 97 is a
6003
         * The last example changes the Map elements to:
6004
         *  ['one', 'two', 3]
6005
         *
6006
         * By default, Map elements which are arrays will be traversed recursively.
6007
         * To iterate over the Map elements only, pass FALSE as third parameter.
6008
         *
6009
         * @param callable $callback Function with (item, key, data) parameters
6010
         * @param mixed $data Arbitrary data that will be passed to the callback as third parameter
6011
         * @param bool $recursive TRUE to traverse sub-arrays recursively (default), FALSE to iterate Map elements only
6012
         * @return self<int|string,mixed> Updated map for fluid interface
6013
         */
6014
        public function walk( callable $callback, $data = null, bool $recursive = true ) : self
6015
        {
6016
                if( $recursive ) {
24✔
6017
                        array_walk_recursive( $this->list(), $callback, $data );
16✔
6018
                } else {
6019
                        array_walk( $this->list(), $callback, $data );
8✔
6020
                }
6021

6022
                return $this;
24✔
6023
        }
6024

6025

6026
        /**
6027
         * Filters the list of elements by a given condition.
6028
         *
6029
         * Examples:
6030
         *  Map::from( [
6031
         *    ['id' => 1, 'type' => 'name'],
6032
         *    ['id' => 2, 'type' => 'short'],
6033
         *  ] )->where( 'type', '==', 'name' );
6034
         *
6035
         *  Map::from( [
6036
         *    ['id' => 3, 'price' => 10],
6037
         *    ['id' => 4, 'price' => 50],
6038
         *  ] )->where( 'price', '>', 20 );
6039
         *
6040
         *  Map::from( [
6041
         *    ['id' => 3, 'price' => 10],
6042
         *    ['id' => 4, 'price' => 50],
6043
         *  ] )->where( 'price', 'in', [10, 25] );
6044
         *
6045
         *  Map::from( [
6046
         *    ['id' => 3, 'price' => 10],
6047
         *    ['id' => 4, 'price' => 50],
6048
         *  ] )->where( 'price', '-', [10, 100] );
6049
         *
6050
         *  Map::from( [
6051
         *    ['item' => ['id' => 3, 'price' => 10]],
6052
         *    ['item' => ['id' => 4, 'price' => 50]],
6053
         *  ] )->where( 'item/price', '>', 30 );
6054
         *
6055
         * Results:
6056
         *  [0 => ['id' => 1, 'type' => 'name']]
6057
         *  [1 => ['id' => 4, 'price' => 50]]
6058
         *  [0 => ['id' => 3, 'price' => 10]]
6059
         *  [0 => ['id' => 3, 'price' => 10], ['id' => 4, 'price' => 50]]
6060
         *  [1 => ['item' => ['id' => 4, 'price' => 50]]]
6061
         *
6062
         * Available operators are:
6063
         * * '==' : Equal
6064
         * * '===' : Equal and same type
6065
         * * '!=' : Not equal
6066
         * * '!==' : Not equal and same type
6067
         * * '<=' : Smaller than an equal
6068
         * * '>=' : Greater than an equal
6069
         * * '<' : Smaller
6070
         * * '>' : Greater
6071
         * 'in' : Array of value which are in the list of values
6072
         * '-' : Values between array of start and end value, e.g. [10, 100] (inclusive)
6073
         *
6074
         * This does also work for multi-dimensional arrays by passing the keys
6075
         * of the arrays separated by the delimiter ("/" by default), e.g. "key1/key2/key3"
6076
         * to get "val" from ['key1' => ['key2' => ['key3' => 'val']]]. The same applies to
6077
         * public properties of objects or objects implementing __isset() and __get() methods.
6078
         *
6079
         * The keys of the original map are preserved in the returned map.
6080
         *
6081
         * @param string $key Key or path of the value in the array or object used for comparison
6082
         * @param string $op Operator used for comparison
6083
         * @param mixed $value Value used for comparison
6084
         * @return self<int|string,mixed> New map for fluid interface
6085
         */
6086
        public function where( string $key, string $op, $value ) : self
6087
        {
6088
                return $this->filter( function( $item ) use ( $key, $op, $value ) {
36✔
6089

6090
                        if( ( $val = $this->val( $item, explode( $this->sep, $key ) ) ) !== null )
48✔
6091
                        {
6092
                                switch( $op )
5✔
6093
                                {
6094
                                        case '-':
40✔
6095
                                                $list = (array) $value;
8✔
6096
                                                return $val >= current( $list ) && $val <= end( $list );
8✔
6097
                                        case 'in': return in_array( $val, (array) $value );
32✔
6098
                                        case '<': return $val < $value;
24✔
6099
                                        case '>': return $val > $value;
24✔
6100
                                        case '<=': return $val <= $value;
16✔
6101
                                        case '>=': return $val >= $value;
16✔
6102
                                        case '===': return $val === $value;
16✔
6103
                                        case '!==': return $val !== $value;
16✔
6104
                                        case '!=': return $val != $value;
16✔
6105
                                        default: return $val == $value;
16✔
6106
                                }
6107
                        }
6108

6109
                        return false;
8✔
6110
                } );
48✔
6111
        }
6112

6113

6114
        /**
6115
         * Returns a copy of the map with the element at the given index replaced with the given value.
6116
         *
6117
         * Examples:
6118
         *  $m = Map::from( ['a' => 1] );
6119
         *  $m->with( 2, 'b' );
6120
         *  $m->with( 'a', 2 );
6121
         *
6122
         * Results:
6123
         *  ['a' => 1, 2 => 'b']
6124
         *  ['a' => 2]
6125
         *
6126
         * The original map ($m) stays untouched!
6127
         * This method is a shortcut for calling the copy() and set() methods.
6128
         *
6129
         * @param int|string $key Array key to set or replace
6130
         * @param mixed $value New value for the given key
6131
         * @return self<int|string,mixed> New map
6132
         */
6133
        public function with( $key, $value ) : self
6134
        {
6135
                return ( clone $this )->set( $key, $value );
8✔
6136
        }
6137

6138

6139
        /**
6140
         * Merges the values of all arrays at the corresponding index.
6141
         *
6142
         * Examples:
6143
         *  $en = ['one', 'two', 'three'];
6144
         *  $es = ['uno', 'dos', 'tres'];
6145
         *  $m = Map::from( [1, 2, 3] )->zip( $en, $es );
6146
         *
6147
         * Results:
6148
         *  [
6149
         *    [1, 'one', 'uno'],
6150
         *    [2, 'two', 'dos'],
6151
         *    [3, 'three', 'tres'],
6152
         *  ]
6153
         *
6154
         * @param array<int|string,mixed>|\Traversable<int|string,mixed>|\Iterator<int|string,mixed> $arrays List of arrays to merge with at the same position
6155
         * @return self<int|string,mixed> New map of arrays
6156
         */
6157
        public function zip( ...$arrays ) : self
6158
        {
6159
                $args = array_map( function( $items ) {
6✔
6160
                        return $this->array( $items );
8✔
6161
                }, $arrays );
8✔
6162

6163
                return new static( array_map( null, $this->list(), ...$args ) );
8✔
6164
        }
6165

6166

6167
        /**
6168
         * Returns a plain array of the given elements.
6169
         *
6170
         * @param mixed $elements List of elements or single value
6171
         * @return array<int|string,mixed> Plain array
6172
         */
6173
        protected function array( $elements ) : array
6174
        {
6175
                if( is_array( $elements ) ) {
2,040✔
6176
                        return $elements;
1,960✔
6177
                }
6178

6179
                if( $elements instanceof \Closure ) {
312✔
6180
                        return (array) $elements();
×
6181
                }
6182

6183
                if( $elements instanceof \Aimeos\Map ) {
312✔
6184
                        return $elements->toArray();
184✔
6185
                }
6186

6187
                if( is_iterable( $elements ) ) {
136✔
6188
                        return iterator_to_array( $elements, true );
24✔
6189
                }
6190

6191
                return $elements !== null ? [$elements] : [];
112✔
6192
        }
6193

6194

6195
        /**
6196
         * Flattens a multi-dimensional array or map into a single level array.
6197
         *
6198
         * @param iterable<int|string,mixed> $entries Single of multi-level array, map or everything foreach can be used with
6199
         * @param array<mixed> &$result Will contain all elements from the multi-dimensional arrays afterwards
6200
         * @param int $depth Number of levels to flatten in multi-dimensional arrays
6201
         */
6202
        protected function flatten( iterable $entries, array &$result, int $depth ) : void
6203
        {
6204
                foreach( $entries as $entry )
40✔
6205
                {
6206
                        if( is_iterable( $entry ) && $depth > 0 ) {
40✔
6207
                                $this->flatten( $entry, $result, $depth - 1 );
32✔
6208
                        } else {
6209
                                $result[] = $entry;
40✔
6210
                        }
6211
                }
6212
        }
10✔
6213

6214

6215
        /**
6216
         * Flattens a multi-dimensional array or map into a single level array.
6217
         *
6218
         * @param iterable<int|string,mixed> $entries Single of multi-level array, map or everything foreach can be used with
6219
         * @param array<int|string,mixed> $result Will contain all elements from the multi-dimensional arrays afterwards
6220
         * @param int $depth Number of levels to flatten in multi-dimensional arrays
6221
         */
6222
        protected function kflatten( iterable $entries, array &$result, int $depth ) : void
6223
        {
6224
                foreach( $entries as $key => $entry )
40✔
6225
                {
6226
                        if( is_iterable( $entry ) && $depth > 0 ) {
40✔
6227
                                $this->kflatten( $entry, $result, $depth - 1 );
40✔
6228
                        } else {
6229
                                $result[$key] = $entry;
40✔
6230
                        }
6231
                }
6232
        }
10✔
6233

6234

6235
        /**
6236
         * Returns a reference to the array of elements
6237
         *
6238
         * @return array Reference to the array of elements
6239
         */
6240
        protected function &list() : array
6241
        {
6242
                if( !is_array( $this->list ) ) {
2,896✔
6243
                        $this->list = $this->array( $this->list );
×
6244
                }
6245

6246
                return $this->list;
2,896✔
6247
        }
6248

6249

6250
        /**
6251
         * Returns a closure that retrieves the value for the passed key
6252
         *
6253
         * @param \Closure|string|null $key Closure or key (e.g. "key1/key2/key3") to retrieve the value for
6254
         * @return \Closure Closure that retrieves the value for the passed key
6255
         */
6256
        protected function mapper( $key = null ) : \Closure
6257
        {
6258
                if( $key instanceof \Closure ) {
112✔
6259
                        return $key;
48✔
6260
                }
6261

6262
                $parts = $key ? explode( $this->sep, (string) $key ) : [];
64✔
6263

6264
                return function( $item ) use ( $parts ) {
48✔
6265
                        return $this->val( $item, $parts );
64✔
6266
                };
64✔
6267
        }
6268

6269

6270
        /**
6271
         * Returns the position of the first element that doesn't match the condition
6272
         *
6273
         * @param iterable<int|string,mixed> $list List of elements to check
6274
         * @param \Closure $callback Closure with ($item, $key) arguments to check the condition
6275
         * @return int Position of the first element that doesn't match the condition
6276
         */
6277
        protected function until( iterable $list, \Closure $callback ) : int
6278
        {
6279
                $idx = 0;
16✔
6280

6281
                foreach( $list as $key => $item )
16✔
6282
                {
6283
                        if( !$callback( $item, $key ) ) {
16✔
6284
                                break;
16✔
6285
                        }
6286

6287
                        ++$idx;
16✔
6288
                }
6289

6290
                return $idx;
16✔
6291
        }
6292

6293

6294
        /**
6295
         * Returns a configuration value from an array.
6296
         *
6297
         * @param array<mixed>|object $entry The array or object to look at
6298
         * @param array<string> $parts Path parts to look for inside the array or object
6299
         * @return mixed Found value or null if no value is available
6300
         */
6301
        protected function val( $entry, array $parts )
6302
        {
6303
                foreach( $parts as $part )
376✔
6304
                {
6305
                        if( ( is_array( $entry ) || $entry instanceof \ArrayAccess ) && isset( $entry[$part] ) ) {
360✔
6306
                                $entry = $entry[$part];
208✔
6307
                        } elseif( is_object( $entry ) && isset( $entry->{$part} ) ) {
216✔
6308
                                $entry = $entry->{$part};
16✔
6309
                        } else {
6310
                                return null;
220✔
6311
                        }
6312
                }
6313

6314
                return $entry;
232✔
6315
        }
6316

6317

6318
        /**
6319
         * Visits each entry, calls the callback and returns the items in the result argument
6320
         *
6321
         * @param iterable<int|string,mixed> $entries List of entries with children (optional)
6322
         * @param array<mixed> $result Numerically indexed list of all visited entries
6323
         * @param int $level Current depth of the nodes in the tree
6324
         * @param \Closure|null $callback Callback with ($entry, $key, $level) arguments, returns the entry added to result
6325
         * @param string $nestKey Key to the children of each entry
6326
         * @param array<mixed>|object|null $parent Parent entry
6327
         */
6328
        protected function visit( iterable $entries, array &$result, int $level, ?\Closure $callback, string $nestKey, $parent = null ) : void
6329
        {
6330
                foreach( $entries as $key => $entry )
40✔
6331
                {
6332
                        $result[] = $callback ? $callback( $entry, $key, $level, $parent ) : $entry;
40✔
6333

6334
                        if( ( is_array( $entry ) || $entry instanceof \ArrayAccess ) && isset( $entry[$nestKey] ) ) {
40✔
6335
                                $this->visit( $entry[$nestKey], $result, $level + 1, $callback, $nestKey, $entry );
32✔
6336
                        } elseif( is_object( $entry ) && isset( $entry->{$nestKey} ) ) {
8✔
6337
                                $this->visit( $entry->{$nestKey}, $result, $level + 1, $callback, $nestKey, $entry );
12✔
6338
                        }
6339
                }
6340
        }
10✔
6341
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc