@@ -1144,30 +1144,37 @@ public function count() : int
1144
1144
* Examples:
1145
1145
* Map::from( [1, 'foo', 2, 'foo', 1] )->countBy();
1146
1146
* Map::from( [1.11, 3.33, 3.33, 9.99] )->countBy();
1147
+ * Map::from( [['i' => ['p' => 1.11]], ['i' => ['p' => 3.33]], ['i' => ['p' => 3.33]]] )->countBy( 'i/p' );
1147
1148
* Map::from( ['[email protected] ', '[email protected] ', '[email protected] '] )->countBy( function( $email ) {
1148
1149
* return substr( strrchr( $email, '@' ), 1 );
1149
1150
* } );
1150
1151
*
1151
1152
* Results:
1152
1153
* [1 => 2, 'foo' => 2, 2 => 1]
1153
1154
* ['1.11' => 1, '3.33' => 2, '9.99' => 1]
1155
+ * ['1.11' => 1, '3.33' => 2]
1154
1156
* ['gmail.com' => 2, 'yahoo.com' => 1]
1155
1157
*
1156
1158
* Counting values does only work for integers and strings because these are
1157
1159
* the only types allowed as array keys. All elements are casted to strings
1158
1160
* if no callback is passed. Custom callbacks need to make sure that only
1159
1161
* string or integer values are returned!
1160
1162
*
1161
- * @param callable| null $callback Function with (value, key) parameters which returns the value to use for counting
1163
+ * @param \Closure|string| null $col Key as "key1/key2/key3" or function with (value, key) parameters returning the values for counting
1162
1164
* @return self<int|string,mixed> New map with values as keys and their count as value
1163
1165
*/
1164
- public function countBy ( ? callable $ callback = null ) : self
1166
+ public function countBy ( $ col = null ) : self
1165
1167
{
1166
- $ callback = $ callback ?: function ( $ value ) {
1167
- return (string ) $ value ;
1168
- };
1168
+ if ( !( $ col instanceof \Closure ) )
1169
+ {
1170
+ $ parts = $ col ? explode ( $ this ->sep , (string ) $ col ) : [];
1171
+
1172
+ $ col = function ( $ item ) use ( $ parts ) {
1173
+ return (string ) $ this ->val ( $ item , $ parts );
1174
+ };
1175
+ }
1169
1176
1170
- return new static ( array_count_values ( array_map ( $ callback , $ this ->list () ) ) );
1177
+ return new static ( array_count_values ( array_map ( $ col , $ this ->list () ) ) );
1171
1178
}
1172
1179
1173
1180
@@ -6140,17 +6147,16 @@ protected function &list() : array
6140
6147
* @param \Closure|string|null $key Closure or key (e.g. "key1/key2/key3") to retrieve the value for
6141
6148
* @return \Closure Closure that retrieves the value for the passed key
6142
6149
*/
6143
- protected function mapper ( $ key = null , bool $ cast = false ) : \Closure
6150
+ protected function mapper ( $ key = null ) : \Closure
6144
6151
{
6145
6152
if ( $ key instanceof \Closure ) {
6146
6153
return $ key ;
6147
6154
}
6148
6155
6149
6156
$ parts = $ key ? explode ( $ this ->sep , (string ) $ key ) : [];
6150
6157
6151
- return function ( $ item ) use ( $ cast , $ parts ) {
6152
- $ val = $ this ->val ( $ item , $ parts );
6153
- return $ cast ? (string ) $ val : $ val ;
6158
+ return function ( $ item ) use ( $ parts ) {
6159
+ return $ this ->val ( $ item , $ parts );
6154
6160
};
6155
6161
}
6156
6162
0 commit comments