Skip to content

Commit 3209379

Browse files
authored
feat(support): add removeValues to array utils (#1204)
1 parent 215671f commit 3209379

File tree

5 files changed

+143
-30
lines changed

5 files changed

+143
-30
lines changed

packages/support/src/Arr/ManipulatesArray.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,33 @@ public function shuffle(): static
115115
}
116116

117117
/**
118-
* Removes the specified items from the array.
118+
* Removes the specified keys and their values from the array.
119119
*
120120
* @param array-key|array<array-key> $keys The keys of the items to remove.
121121
*/
122-
public function remove(string|int|array $keys): static
122+
public function removeKeys(string|int|array $keys): static
123123
{
124-
return $this->createOrModify(namespace\remove($this->value, $keys));
124+
return $this->createOrModify(remove_keys($this->value, $keys));
125125
}
126126

127127
/**
128-
* Alias of {@see \Tempest\Support\Arr\remove}.
128+
* Removes the specified keys and their values from the array. Alias of `removeKeys`.
129+
*
130+
* @param array-key|array<array-key> $keys The keys of the items to remove.
129131
*/
130132
public function forget(string|int|array $keys): static
131133
{
132-
return $this->createOrModify(namespace\remove($this->value, $keys));
134+
return $this->removeKeys($keys);
135+
}
136+
137+
/**
138+
* Removes the specified values from the array.
139+
*
140+
* @param TValue|array<TValue> $values The values to remove.
141+
*/
142+
public function removeValues(string|int|array $values): static
143+
{
144+
return $this->createOrModify(remove_values($this->value, $values));
133145
}
134146

135147
/**

packages/support/src/Arr/functions.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function reduce(iterable $array, callable $callback, mixed $initial = null): mix
9696
}
9797

9898
/**
99-
* Gets a value from the array and remove it.
99+
* Gets a value by its key from the array and remove it. Mutates the array.
100100
*
101101
* @template TKey of array-key
102102
* @template TValue
@@ -107,7 +107,7 @@ function reduce(iterable $array, callable $callback, mixed $initial = null): mix
107107
function pull(array &$array, string|int $key, mixed $default = null): mixed
108108
{
109109
$value = get_by_key($array, $key, $default);
110-
$array = namespace\remove($array, $key);
110+
$array = namespace\forget_keys($array, $key);
111111

112112
return $value;
113113
}
@@ -127,15 +127,37 @@ function shuffle(iterable $array): array
127127
}
128128

129129
/**
130-
* Alias of {@see \Tempest\Support\Arr\remove}.
130+
* Removes the specified keys from the array. The array is not mutated.
131+
*
132+
* @template TKey of array-key
133+
* @template TValue
134+
*
135+
* @param array<TKey,TValue> $array
136+
* @param array-key|array<array-key> $keys The keys of the items to remove.
137+
* @return array<TKey,TValue>
138+
*/
139+
function remove_keys(iterable $array, string|int|array $keys): array
140+
{
141+
return namespace\forget_keys(to_array($array), $keys);
142+
}
143+
144+
/**
145+
* Removes the specified values from the array. The array is mutated.
146+
*
147+
* @template TKey of array-key
148+
* @template TValue
149+
*
150+
* @param array<TKey,TValue> $array
151+
* @param TValue|array<TValue> $values The values to remove.
152+
* @return array<TKey,TValue>
131153
*/
132-
function forget(iterable $array, string|int|array $keys): array
154+
function remove_values(array $array, string|int|array $values): array
133155
{
134-
return namespace\remove(to_array($array), $keys);
156+
return namespace\forget_values(to_array($array), $values);
135157
}
136158

137159
/**
138-
* Removes the specified items from the array.
160+
* Removes the specified keys from the array. The array is mutated.
139161
*
140162
* @template TKey of array-key
141163
* @template TValue
@@ -144,7 +166,7 @@ function forget(iterable $array, string|int|array $keys): array
144166
* @param array-key|array<array-key> $keys The keys of the items to remove.
145167
* @return array<TKey,TValue>
146168
*/
147-
function remove(array $array, string|int|array $keys): array
169+
function forget_keys(array $array, string|int|array $keys): array
148170
{
149171
$keys = is_array($keys) ? $keys : [$keys];
150172

@@ -155,6 +177,29 @@ function remove(array $array, string|int|array $keys): array
155177
return $array;
156178
}
157179

180+
/**
181+
* Removes the specified values from the array. The array is mutated.
182+
*
183+
* @template TKey of array-key
184+
* @template TValue
185+
*
186+
* @param array<TKey,TValue> $array
187+
* @param TValue|array<TValue> $values The values to remove.
188+
* @return array<TKey,TValue>
189+
*/
190+
function forget_values(array $array, string|int|array $values): array
191+
{
192+
$values = is_array($values) ? $values : [$values];
193+
194+
foreach ($values as $value) {
195+
if (! is_null($key = array_find_key($array, fn (mixed $match) => $value === $match))) {
196+
unset($array[$key]);
197+
}
198+
}
199+
200+
return $array;
201+
}
202+
158203
/**
159204
* Asserts whether the array is a list.
160205
* An array is a list if its keys consist of consecutive numbers.

packages/support/tests/Arr/ImmutableArrayTest.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public function test_remove_with_basic_keys(): void
7272
$collection = new ImmutableArray([1, 2, 3]);
7373

7474
$this->assertEquals(
75-
$collection->remove(1)->toArray(),
75+
$collection->removeKeys(1)->toArray(),
7676
[0 => 1, 2 => 3],
7777
);
7878

7979
$this->assertEquals(
80-
$collection->remove([0, 2])->toArray(),
80+
$collection->removeKeys([0, 2])->toArray(),
8181
[1 => 2],
8282
);
8383
}
@@ -91,12 +91,53 @@ public function test_remove_with_associative_keys(): void
9191
]);
9292

9393
$this->assertEquals(
94-
$collection->remove('first_name')->toArray(),
94+
$collection->removeKeys('first_name')->toArray(),
9595
['last_name' => 'Doe', 'age' => 42],
9696
);
9797

9898
$this->assertEquals(
99-
$collection->remove(['last_name', 'age'])->toArray(),
99+
$collection->removeKeys(['last_name', 'age'])->toArray(),
100+
['first_name' => 'John'],
101+
);
102+
}
103+
104+
public function test_remove_values_with_basic_keys(): void
105+
{
106+
$collection = new ImmutableArray([1, 2, 3]);
107+
108+
$this->assertEquals(
109+
$collection->removeValues(1)->toArray(),
110+
[1 => 2, 2 => 3],
111+
);
112+
113+
$this->assertEquals(
114+
$collection->toArray(),
115+
[0 => 1, 1 => 2, 2 => 3],
116+
);
117+
118+
$this->assertEquals(
119+
$collection->removeValues([0, 2])->toArray(),
120+
[0 => 1, 2 => 3],
121+
);
122+
}
123+
124+
public function test_remove_values_with_associative_keys(): void
125+
{
126+
$collection = new ImmutableArray([
127+
'first_name' => 'John',
128+
'last_name' => 'Doe',
129+
'age' => 42,
130+
]);
131+
132+
$this->assertEquals(
133+
$collection->removeValues('John')->toArray(),
134+
['last_name' => 'Doe', 'age' => 42],
135+
);
136+
137+
$this->assertEquals($collection->count(), 3);
138+
139+
$this->assertEquals(
140+
$collection->removeValues(['Doe', 42])->toArray(),
100141
['first_name' => 'John'],
101142
);
102143
}

packages/support/tests/Arr/ManipulatesArrayTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,12 @@ public function test_remove_with_basic_keys(): void
11891189
$collection = arr([1, 2, 3]);
11901190

11911191
$this->assertEquals(
1192-
$collection->remove(1)->toArray(),
1192+
$collection->removeKeys(1)->toArray(),
11931193
[0 => 1, 2 => 3],
11941194
);
11951195

11961196
$this->assertEquals(
1197-
$collection->remove([0, 2])->toArray(),
1197+
$collection->removeKeys([0, 2])->toArray(),
11981198
[1 => 2],
11991199
);
12001200
}
@@ -1208,12 +1208,12 @@ public function test_remove_with_associative_keys(): void
12081208
]);
12091209

12101210
$this->assertEquals(
1211-
$collection->remove('first_name')->toArray(),
1211+
$collection->removeKeys('first_name')->toArray(),
12121212
['last_name' => 'Doe', 'age' => 42],
12131213
);
12141214

12151215
$this->assertEquals(
1216-
$collection->remove(['last_name', 'age'])->toArray(),
1216+
$collection->removeKeys(['last_name', 'age'])->toArray(),
12171217
['first_name' => 'John'],
12181218
);
12191219
}
@@ -1224,7 +1224,7 @@ public function test_remove_with_no_valid_key(): void
12241224

12251225
$this->assertEquals(
12261226
$collection
1227-
->remove(42)
1227+
->removeKeys(42)
12281228
->toArray(),
12291229
[1, 2, 3],
12301230
);
@@ -1237,7 +1237,7 @@ public function test_remove_with_no_valid_key(): void
12371237

12381238
$this->assertEquals(
12391239
$collection
1240-
->remove('foo')
1240+
->removeKeys('foo')
12411241
->toArray(),
12421242
[
12431243
'first_name' => 'John',
@@ -1248,7 +1248,7 @@ public function test_remove_with_no_valid_key(): void
12481248

12491249
$this->assertEquals(
12501250
$collection
1251-
->remove(['bar', 'first_name'])
1251+
->removeKeys(['bar', 'first_name'])
12521252
->toArray(),
12531253
[
12541254
'last_name' => 'Doe',
@@ -1264,9 +1264,9 @@ public function test_forget_is_alias_of_remove(): void
12641264
'last_name' => 'Doe',
12651265
'age' => 42,
12661266
])
1267-
->remove(42)
1268-
->remove('foo')
1269-
->remove(['bar', 'first_name']);
1267+
->removeKeys(42)
1268+
->removeKeys('foo')
1269+
->removeKeys(['bar', 'first_name']);
12701270

12711271
$second_collection = arr([
12721272
'first_name' => 'John',

packages/support/tests/Arr/MutableArrayTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public function test_remove_with_basic_keys(): void
7272
$collection = new MutableArray([1, 2, 3]);
7373

7474
$this->assertEquals(
75-
$collection->remove(1)->toArray(),
75+
$collection->removeKeys(1)->toArray(),
7676
[0 => 1, 2 => 3],
7777
);
7878

7979
$this->assertEquals(
80-
$collection->remove([0, 2])->toArray(),
80+
$collection->removeKeys([0, 2])->toArray(),
8181
[],
8282
);
8383
}
@@ -91,16 +91,31 @@ public function test_remove_with_associative_keys(): void
9191
]);
9292

9393
$this->assertEquals(
94-
$collection->remove('first_name')->toArray(),
94+
$collection->removeKeys('first_name')->toArray(),
9595
['last_name' => 'Doe', 'age' => 42],
9696
);
9797

9898
$this->assertEquals(
99-
$collection->remove(['last_name', 'age'])->toArray(),
99+
$collection->removeKeys(['last_name', 'age'])->toArray(),
100100
[],
101101
);
102102
}
103103

104+
public function test_remove_values_with_basic_keys(): void
105+
{
106+
$collection = new MutableArray([1, 2, 3]);
107+
108+
$this->assertEquals(
109+
$collection->removeValues(1)->toArray(),
110+
[1 => 2, 2 => 3],
111+
);
112+
113+
$this->assertEquals(
114+
$collection->toArray(),
115+
[1 => 2, 2 => 3],
116+
);
117+
}
118+
104119
public function test_pull(): void
105120
{
106121
$collection = new MutableArray([

0 commit comments

Comments
 (0)