@@ -1380,30 +1380,13 @@ public function dump( ?callable $callback = null ) : self
1380
1380
*/
1381
1381
public function duplicates ( $ col = null ) : self
1382
1382
{
1383
- $ list = $ this ->list ();
1383
+ $ list = $ map = $ this ->list ();
1384
1384
1385
- if ( $ col = == null ) {
1386
- return new static ( array_diff_key ( $ list , array_unique ( $ list ) ) );
1385
+ if ( $ col ! == null ) {
1386
+ $ map = array_map ( $ this -> mapper ( $ col ), array_values ( $ list ), array_keys ( $ list ) );
1387
1387
}
1388
1388
1389
- if ( !( $ col instanceof \Closure ) )
1390
- {
1391
- $ parts = explode ( $ this ->sep , (string ) $ col );
1392
-
1393
- $ col = function ( $ item ) use ( $ parts ) {
1394
- return $ this ->val ( $ item , $ parts );
1395
- };
1396
- }
1397
-
1398
- $ items = [];
1399
-
1400
- foreach ( $ list as $ key => $ item )
1401
- {
1402
- $ value = $ col ( $ item , $ key );
1403
- $ items [$ key ] = $ value ;
1404
- }
1405
-
1406
- return new static ( array_diff_key ( $ list , array_unique ( $ items ) ) );
1389
+ return new static ( array_diff_key ( $ list , array_unique ( $ map ) ) );
1407
1390
}
1408
1391
1409
1392
@@ -5759,28 +5742,10 @@ public function unique( $col = null ) : self
5759
5742
return new static ( array_unique ( $ this ->list () ) );
5760
5743
}
5761
5744
5762
- if ( !( $ col instanceof \Closure ) )
5763
- {
5764
- $ parts = explode ( $ this ->sep , (string ) $ col );
5765
-
5766
- $ col = function ( $ item ) use ( $ parts ) {
5767
- return $ this ->val ( $ item , $ parts );
5768
- };
5769
- }
5770
-
5771
- $ list = $ unique = [];
5772
-
5773
- foreach ( $ this ->list () as $ key => $ item )
5774
- {
5775
- $ value = (string ) $ col ( $ item , $ key );
5776
-
5777
- if ( !isset ( $ unique [$ value ] ) ) {
5778
- $ unique [$ value ] = true ;
5779
- $ list [$ key ] = $ item ;
5780
- }
5781
- }
5745
+ $ list = $ this ->list ();
5746
+ $ map = array_map ( $ this ->mapper ( $ col ), array_values ( $ list ), array_keys ( $ list ) );
5782
5747
5783
- return new static ( $ list );
5748
+ return new static ( array_intersect_key ( $ list, array_unique ( $ map ) ) );
5784
5749
}
5785
5750
5786
5751
@@ -6170,6 +6135,27 @@ protected function &list() : array
6170
6135
}
6171
6136
6172
6137
6138
+ /**
6139
+ * Returns a closure that retrieves the value for the passed key
6140
+ *
6141
+ * @param \Closure|string|null $key Closure or key (e.g. "key1/key2/key3") to retrieve the value for
6142
+ * @return \Closure Closure that retrieves the value for the passed key
6143
+ */
6144
+ protected function mapper ( $ key = null , bool $ cast = false ) : \Closure
6145
+ {
6146
+ if ( $ key instanceof \Closure ) {
6147
+ return $ key ;
6148
+ }
6149
+
6150
+ $ parts = $ key ? explode ( $ this ->sep , (string ) $ key ) : [];
6151
+
6152
+ return function ( $ item ) use ( $ cast , $ parts ) {
6153
+ $ val = $ this ->val ( $ item , $ parts );
6154
+ return $ cast ? (string ) $ val : $ val ;
6155
+ };
6156
+ }
6157
+
6158
+
6173
6159
/**
6174
6160
* Returns a configuration value from an array.
6175
6161
*
0 commit comments