mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Adds data_forget()
helper (#5915)
* Adds `data_forget()` helper * Update CHANGELOG-3.1.md
This commit is contained in:
parent
5dcad05ef8
commit
6d4f647ac9
@ -27,6 +27,7 @@
|
||||
- [#5894](https://github.com/hyperf/hyperf/pull/5894) [#5897](https://github.com/hyperf/hyperf/pull/5897) Added `model-factory` support for `hyperf/testing`.
|
||||
- [#5898](https://github.com/hyperf/hyperf/pull/5898) Added `toRawSql()` to Query Builders.
|
||||
- [#5906](https://github.com/hyperf/hyperf/pull/5906) Added `getRawQueryLog()` to Database Connection.
|
||||
- [#5915](https://github.com/hyperf/hyperf/pull/5915) Added `data_forget` helper.
|
||||
|
||||
## Optimized
|
||||
|
||||
|
@ -152,6 +152,42 @@ function data_set(&$target, $key, $value, $overwrite = true)
|
||||
return $target;
|
||||
}
|
||||
|
||||
if (! function_exists('data_forget')) {
|
||||
/**
|
||||
* Remove / unset an item from an array or object using "dot" notation.
|
||||
*
|
||||
* @param mixed $target
|
||||
* @param null|array|int|string $key
|
||||
* @return mixed
|
||||
*/
|
||||
function data_forget(&$target, $key)
|
||||
{
|
||||
$segments = is_array($key) ? $key : explode('.', $key);
|
||||
|
||||
if (($segment = array_shift($segments)) === '*' && Arr::accessible($target)) {
|
||||
if ($segments) {
|
||||
foreach ($target as &$inner) {
|
||||
data_forget($inner, $segments);
|
||||
}
|
||||
}
|
||||
} elseif (Arr::accessible($target)) {
|
||||
if ($segments && Arr::exists($target, $segment)) {
|
||||
data_forget($target[$segment], $segments);
|
||||
} else {
|
||||
Arr::forget($target, $segment);
|
||||
}
|
||||
} elseif (is_object($target)) {
|
||||
if ($segments && isset($target->{$segment})) {
|
||||
data_forget($target->{$segment}, $segments);
|
||||
} elseif (isset($target->{$segment})) {
|
||||
unset($target->{$segment});
|
||||
}
|
||||
}
|
||||
|
||||
return $target;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first element of an array. Useful for method chaining.
|
||||
*
|
||||
|
@ -19,6 +19,7 @@ use PHPUnit\Framework\TestCase;
|
||||
use Traversable;
|
||||
|
||||
use function Hyperf\Collection\data_fill;
|
||||
use function Hyperf\Collection\data_forget;
|
||||
use function Hyperf\Collection\data_get;
|
||||
use function Hyperf\Collection\data_set;
|
||||
use function Hyperf\Collection\head;
|
||||
@ -299,6 +300,95 @@ class FunctionsTest extends TestCase
|
||||
], $data);
|
||||
}
|
||||
|
||||
public function testDataForget()
|
||||
{
|
||||
$data = ['foo' => 'bar', 'hello' => 'world'];
|
||||
|
||||
$this->assertEquals(
|
||||
['hello' => 'world'],
|
||||
data_forget($data, 'foo')
|
||||
);
|
||||
|
||||
$data = ['foo' => 'bar', 'hello' => 'world'];
|
||||
|
||||
$this->assertEquals(
|
||||
['foo' => 'bar', 'hello' => 'world'],
|
||||
data_forget($data, 'nothing')
|
||||
);
|
||||
|
||||
$data = ['one' => ['two' => ['three' => 'hello', 'four' => ['five']]]];
|
||||
|
||||
$this->assertEquals(
|
||||
['one' => ['two' => ['four' => ['five']]]],
|
||||
data_forget($data, 'one.two.three')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDataForgetWithStar()
|
||||
{
|
||||
$data = [
|
||||
'article' => [
|
||||
'title' => 'Foo',
|
||||
'comments' => [
|
||||
['comment' => 'foo', 'name' => 'First'],
|
||||
['comment' => 'bar', 'name' => 'Second'],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'article' => [
|
||||
'title' => 'Foo',
|
||||
'comments' => [
|
||||
['comment' => 'foo'],
|
||||
['comment' => 'bar'],
|
||||
],
|
||||
],
|
||||
],
|
||||
data_forget($data, 'article.comments.*.name')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDataForgetWithDoubleStar()
|
||||
{
|
||||
$data = [
|
||||
'posts' => [
|
||||
(object) [
|
||||
'comments' => [
|
||||
(object) ['name' => 'First', 'comment' => 'foo'],
|
||||
(object) ['name' => 'Second', 'comment' => 'bar'],
|
||||
],
|
||||
],
|
||||
(object) [
|
||||
'comments' => [
|
||||
(object) ['name' => 'Third', 'comment' => 'hello'],
|
||||
(object) ['name' => 'Fourth', 'comment' => 'world'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
data_forget($data, 'posts.*.comments.*.name');
|
||||
|
||||
$this->assertEquals([
|
||||
'posts' => [
|
||||
(object) [
|
||||
'comments' => [
|
||||
(object) ['comment' => 'foo'],
|
||||
(object) ['comment' => 'bar'],
|
||||
],
|
||||
],
|
||||
(object) [
|
||||
'comments' => [
|
||||
(object) ['comment' => 'hello'],
|
||||
(object) ['comment' => 'world'],
|
||||
],
|
||||
],
|
||||
],
|
||||
], $data);
|
||||
}
|
||||
|
||||
public function testHead()
|
||||
{
|
||||
$array = ['a', 'b', 'c'];
|
||||
|
Loading…
Reference in New Issue
Block a user