mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added Collection::replace()
and Collection::replaceRecursive()
(#6501)
This commit is contained in:
parent
8f11133572
commit
b83d1d9b43
@ -10,6 +10,7 @@
|
||||
- [#6483](https://github.com/hyperf/hyperf/pull/6483) [#6487] (https://github.com/hyperf/hyperf/pull/6487) Added new ways to register crontab.
|
||||
- [#6488](https://github.com/hyperf/hyperf/pull/6488) Added the default implement for `Psr\Log\LoggerInterface`.
|
||||
- [#6495](https://github.com/hyperf/hyperf/pull/6495) Added cron support for closure-command.
|
||||
- [#6501](https://github.com/hyperf/hyperf/pull/6501) Added `Collection::replace()` and `Collection::replaceRecursive()`.
|
||||
|
||||
## Optimized
|
||||
|
||||
|
@ -1238,6 +1238,28 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the collection items with the given items.
|
||||
*
|
||||
* @param Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @return static
|
||||
*/
|
||||
public function replace($items)
|
||||
{
|
||||
return new static(array_replace($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively replace the collection items with the given items.
|
||||
*
|
||||
* @param Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @return static
|
||||
*/
|
||||
public function replaceRecursive($items)
|
||||
{
|
||||
return new static(array_replace_recursive($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse items order.
|
||||
*
|
||||
@ -1813,24 +1835,14 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate
|
||||
*/
|
||||
protected function getArrayableItems($items): array
|
||||
{
|
||||
if (is_array($items)) {
|
||||
return $items;
|
||||
}
|
||||
if ($items instanceof self) {
|
||||
return $items->all();
|
||||
}
|
||||
if ($items instanceof Arrayable) {
|
||||
return $items->toArray();
|
||||
}
|
||||
if ($items instanceof Jsonable) {
|
||||
return json_decode($items->__toString(), true);
|
||||
}
|
||||
if ($items instanceof JsonSerializable) {
|
||||
return $items->jsonSerialize();
|
||||
}
|
||||
if ($items instanceof Traversable) {
|
||||
return iterator_to_array($items);
|
||||
}
|
||||
return (array) $items;
|
||||
return match (true) {
|
||||
is_array($items) => $items,
|
||||
$items instanceof self => $items->all(),
|
||||
$items instanceof Arrayable => $items->toArray(),
|
||||
$items instanceof Jsonable => json_decode($items->__toString(), true),
|
||||
$items instanceof JsonSerializable => $items->jsonSerialize(),
|
||||
$items instanceof Traversable => iterator_to_array($items),
|
||||
default => (array) $items,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -194,4 +194,67 @@ class CollectionTest extends TestCase
|
||||
$this->assertEquals(['first' => 'Swoole', 'email' => 'hyperf@gmail.com'], $data->except('last')->all());
|
||||
$this->assertEquals(['first' => 'Swoole', 'email' => 'hyperf@gmail.com'], $data->except(collect(['last']))->all());
|
||||
}
|
||||
|
||||
public function testReplaceNull()
|
||||
{
|
||||
$c = new Collection(['a', 'b', 'c']);
|
||||
$this->assertEquals(['a', 'b', 'c'], $c->replace(null)->all());
|
||||
}
|
||||
|
||||
public function testReplaceArray()
|
||||
{
|
||||
$c = new Collection(['a', 'b', 'c']);
|
||||
$this->assertEquals(['a', 'd', 'e'], $c->replace([1 => 'd', 2 => 'e'])->all());
|
||||
|
||||
$c = new Collection(['a', 'b', 'c']);
|
||||
$this->assertEquals(['a', 'd', 'e', 'f', 'g'], $c->replace([1 => 'd', 2 => 'e', 3 => 'f', 4 => 'g'])->all());
|
||||
|
||||
$c = new Collection(['name' => 'amir', 'family' => 'otwell']);
|
||||
$this->assertEquals(['name' => 'taylor', 'family' => 'otwell', 'age' => 26], $c->replace(['name' => 'taylor', 'age' => 26])->all());
|
||||
}
|
||||
|
||||
public function testReplaceCollection()
|
||||
{
|
||||
$c = new Collection(['a', 'b', 'c']);
|
||||
$this->assertEquals(
|
||||
['a', 'd', 'e'],
|
||||
$c->replace(new Collection([1 => 'd', 2 => 'e']))->all()
|
||||
);
|
||||
|
||||
$c = new Collection(['a', 'b', 'c']);
|
||||
$this->assertEquals(
|
||||
['a', 'd', 'e', 'f', 'g'],
|
||||
$c->replace(new Collection([1 => 'd', 2 => 'e', 3 => 'f', 4 => 'g']))->all()
|
||||
);
|
||||
|
||||
$c = new Collection(['name' => 'amir', 'family' => 'otwell']);
|
||||
$this->assertEquals(
|
||||
['name' => 'taylor', 'family' => 'otwell', 'age' => 26],
|
||||
$c->replace(new Collection(['name' => 'taylor', 'age' => 26]))->all()
|
||||
);
|
||||
}
|
||||
|
||||
public function testReplaceRecursiveNull()
|
||||
{
|
||||
$c = new Collection(['a', 'b', ['c', 'd']]);
|
||||
$this->assertEquals(['a', 'b', ['c', 'd']], $c->replaceRecursive(null)->all());
|
||||
}
|
||||
|
||||
public function testReplaceRecursiveArray()
|
||||
{
|
||||
$c = new Collection(['a', 'b', ['c', 'd']]);
|
||||
$this->assertEquals(['z', 'b', ['c', 'e']], $c->replaceRecursive(['z', 2 => [1 => 'e']])->all());
|
||||
|
||||
$c = new Collection(['a', 'b', ['c', 'd']]);
|
||||
$this->assertEquals(['z', 'b', ['c', 'e'], 'f'], $c->replaceRecursive(['z', 2 => [1 => 'e'], 'f'])->all());
|
||||
}
|
||||
|
||||
public function testReplaceRecursiveCollection()
|
||||
{
|
||||
$c = new Collection(['a', 'b', ['c', 'd']]);
|
||||
$this->assertEquals(
|
||||
['z', 'b', ['c', 'e']],
|
||||
$c->replaceRecursive(new Collection(['z', 2 => [1 => 'e']]))->all()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user