Skip to content

Commit c04e4e8

Browse files
committed
Revert "Implement value retrieval by matcher"
This reverts commit a4e40b3.
1 parent b3dde8e commit c04e4e8

File tree

3 files changed

+25
-53
lines changed

3 files changed

+25
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3.1.1 under development
44

5-
- New #142: Add `ArrayHelper::find()`, `ArrayHelper::findKey()`, `ArrayHelper::findAny()` and `ArrayHelper::findAll()` method for value retrieval or checks by a predicate function (@yus-ham)
5+
- New #142: Add `ArrayHelper::find()`, `ArrayHelper::findKey()`, `ArrayHelper::findAny()` and `ArrayHelper::findAll()` methods for value retrieval or checks by a predicate function (@yus-ham)
66
- Enh #156: Improve psalm types in `ArrayHelper::getObjectVars()`, `ArrayableInterface`, `ArrayableTrait` and
77
`ArrayAccessTrait` (@vjik)
88

src/ArrayHelper.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

tests/ArrayHelper/GetValueTest.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ private function commonDataProviderFromArray(): array
5353
['name', 'test'],
5454
['noname', null],
5555
['noname', 'test', 'test'],
56+
[
57+
static fn ($array, $defaultValue) => $array['date'] . $defaultValue,
58+
'31-12-2113test',
59+
'test',
60+
],
5661
[['version', 2], 'two'],
5762
[['version', 2.0], 'two'],
5863
[42.7, 500],
@@ -84,26 +89,6 @@ public function testGetValueFromArray($key, $expected, $default = null): void
8489
$this->assertEquals($expected, ArrayHelper::getValue($this->array, $key, $default));
8590
}
8691

87-
public function testGetValueByMatcher(): void
88-
{
89-
$users = [
90-
['name' => 'Cebe', 'status' => 'active'],
91-
['name' => 'John', 'status' => 'not active'],
92-
];
93-
94-
$activeUser = ArrayHelper::getValue($users, fn ($user) => $user['status'] === 'active');
95-
$this->assertEquals('Cebe', $activeUser['name']);
96-
97-
98-
$posts = [
99-
(object)['title' => 'hello world'],
100-
(object)['title' => 'hello test', 'tag' => 'test'],
101-
];
102-
103-
$taggedPost = ArrayHelper::getValue($posts, fn ($post) => isset($post->tag));
104-
$this->assertEquals('hello test', $taggedPost->title);
105-
}
106-
10792
public function dataProviderGetValueByPathFromArray(): array
10893
{
10994
return array_merge($this->commonDataProviderFromArray(), [

0 commit comments

Comments
 (0)