@@ -108,13 +108,13 @@ public static function toArray(mixed $object, array $properties = [], bool $recu
108108 $ result = [];
109109 /**
110110 * @var int|string $key
111- * @var Closure| string $name
111+ * @var string $name
112112 */
113113 foreach ($ properties [$ className ] as $ key => $ name ) {
114114 if (is_int ($ key )) {
115- $ result [( string ) $ name ] = $ object ->$ name ;
115+ $ result [$ name ] = $ object ->$ name ;
116116 } else {
117- $ result [$ key ] = $ name instanceof Closure ? $ name ( $ object ) : self ::getValue ($ object , $ name );
117+ $ result [$ key ] = self ::getValue ($ object , $ name );
118118 }
119119 }
120120
@@ -183,9 +183,9 @@ public static function parametrizedMerge(array $arrays, ?int $depth): array
183183 * // Working with object:
184184 * $username = \Yiisoft\Arrays\ArrayHelper::getValue($user, 'username');
185185 *
186- * // Retrieve the value by matcher function:
187- * $firstActiveMember = \Yiisoft\Arrays\ArrayHelper::getValue($users , function ($user, $key ) {
188- * return $user->type === 'member' && $user->isActive ;
186+ * // Working with anonymous function:
187+ * $fullName = \Yiisoft\Arrays\ArrayHelper::getValue($user , function ($user, $defaultValue ) {
188+ * return $user->firstName . ' ' . $user->lastName ;
189189 * });
190190 *
191191 * // Using an array of keys to retrieve the value:
@@ -194,22 +194,23 @@ public static function parametrizedMerge(array $arrays, ?int $depth): array
194194 *
195195 * @param array|object $array Array or object to extract value from.
196196 * @param array|Closure|float|int|string $key Key name of the array element,
197- * an array of keys, object property name, object method like `getName()` or a callable. The callable function signature should be:
198- * `function($value, $key)`.
197+ * an array of keys, object property name, object method like `getName()`, or an anonymous function
198+ * returning the value. The anonymous function signature should be:
199+ * `function($array, $defaultValue)`.
199200 * @param mixed $default The default value to be returned if the specified array key does not exist. Not used when
200201 * getting value from an object.
201202 *
202203 * @psalm-param ArrayKey|Closure $key
203204 *
204- * @return mixed The value of the element if found or the return value of callable is truthy , default value otherwise.
205+ * @return mixed The value of the element if found, default value otherwise.
205206 */
206207 public static function getValue (
207208 array |object $ array ,
208209 array |Closure |float |int |string $ key ,
209210 mixed $ default = null
210211 ): mixed {
211212 if ($ key instanceof Closure) {
212- return self :: getValueByMatcher ($ array, $ key , $ default );
213+ return $ key ($ array , $ default );
213214 }
214215
215216 if (is_array ($ key )) {
@@ -268,20 +269,6 @@ private static function getRootValue(mixed $array, float|int|string $key, mixed
268269 return $ default ;
269270 }
270271
271- private static function getValueByMatcher (
272- array $ array ,
273- Closure $ match ,
274- mixed $ default = null
275- ): mixed {
276- foreach ($ array as $ key => $ value ) {
277- if ($ match ($ value , $ key )) {
278- return $ value ;
279- }
280- }
281-
282- return $ default ;
283- }
284-
285272 /**
286273 * Retrieves the value of an array element or object property with the given key or property name.
287274 * If the key does not exist in the array or object, the default value will be returned instead.
@@ -790,7 +777,7 @@ public static function index(
790777 $ lastArray [] = $ element ;
791778 }
792779 } else {
793- $ value = $ key instanceof Closure ? $ key ( $ element ) : self ::getValue ($ element , $ key );
780+ $ value = self ::getValue ($ element , $ key );
794781 if ($ value !== null ) {
795782 $ lastArray [self ::normalizeArrayKey ($ value )] = $ element ;
796783 }
@@ -852,11 +839,11 @@ public static function getColumn(iterable $array, Closure|string $name, bool $ke
852839 $ result = [];
853840 if ($ keepKeys ) {
854841 foreach ($ array as $ k => $ element ) {
855- $ result [$ k ] = $ name instanceof Closure ? $ name ( $ element ) : self ::getValue ($ element , $ name );
842+ $ result [$ k ] = self ::getValue ($ element , $ name );
856843 }
857844 } else {
858845 foreach ($ array as $ element ) {
859- $ result [] = $ name instanceof Closure ? $ name ( $ element ) : self ::getValue ($ element , $ name );
846+ $ result [] = self ::getValue ($ element , $ name );
860847 }
861848 }
862849
@@ -917,8 +904,8 @@ public static function map(
917904 if ($ from instanceof Closure || $ to instanceof Closure || !is_array ($ array )) {
918905 $ result = [];
919906 foreach ($ array as $ element ) {
920- $ key = (string )( $ from instanceof Closure ? $ from ( $ element ) : self ::getValue ($ element , $ from) );
921- $ result [$ key ] = $ to instanceof Closure ? $ to ( $ element ) : self ::getValue ($ element , $ to );
907+ $ key = (string )self ::getValue ($ element , $ from );
908+ $ result [$ key ] = self ::getValue ($ element , $ to );
922909 }
923910
924911 return $ result ;
@@ -929,9 +916,9 @@ public static function map(
929916
930917 $ result = [];
931918 foreach ($ array as $ element ) {
932- $ groupKey = (string )( $ group instanceof Closure ? $ group ( $ element ) : self ::getValue ($ element , $ group) );
933- $ key = (string )( $ from instanceof Closure ? $ from ( $ element ) : self ::getValue ($ element , $ from) );
934- $ result [$ groupKey ][$ key ] = $ to instanceof Closure ? $ to ( $ element ) : self ::getValue ($ element , $ to );
919+ $ groupKey = (string )self ::getValue ($ element , $ group );
920+ $ key = (string )self ::getValue ($ element , $ from );
921+ $ result [$ groupKey ][$ key ] = self ::getValue ($ element , $ to );
935922 }
936923
937924 return $ result ;
0 commit comments