Skip to content

Commit 7a2712e

Browse files
author
TCB13
committed
Improved ArrayPull: pull strings from arrays or objects matching conditions from arrays of objects.
1 parent 465fad5 commit 7a2712e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/ArrayPull.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ public function __construct(string $property, $value)
1515

1616
public function asArray(): array
1717
{
18+
// Remove individual value from array
19+
// new ArrayPull("list", "user.123")
20+
if (!is_array($this->value)) {
21+
return [
22+
"\$pull" => [
23+
$this->name => $this->value
24+
]
25+
];
26+
}
27+
28+
// Remove array values from property
29+
// new ArrayPull("list", ["user.123", "user.456"])
30+
if (is_int(array_keys($this->value)[0])) {
31+
return [
32+
"\$pull" => [
33+
$this->name => [
34+
"\$in" => $this->value
35+
]
36+
]
37+
];
38+
}
39+
40+
// Remove objects from array with conditions
41+
// new ArrayPull("list", ["id" => ["user.123", "user.456"]])
42+
// new ArrayPull("list", ["id" => "user.123"])
43+
// new ArrayPull("list", ["id" => "user.123", "name" => "joe"])
1844
foreach ($this->value as $key => &$value) {
1945
if (is_array($value)) {
2046
$value = [

0 commit comments

Comments
 (0)