Fixed flatten() failed, bacause INF is float. (#1454)

* 强制int参数,默认INF并不会有用,且会报错

* Added test cases.

* Update CHANGELOG.md

Co-authored-by: Donjan <Donjan@cqbaobao.cn>
Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
Donjan 2020-03-23 17:18:53 +08:00 committed by GitHub
parent 893415c9c9
commit de69980836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 3 deletions

View File

@ -3,6 +3,7 @@
## Fixed
- [#1449](https://github.com/hyperf/hyperf/pull/1449) Fixed memory overflow for high cardinality request path.
- [#1454](https://github.com/hyperf/hyperf/pull/1454) Fixed `flatten()` failed, bacause `INF` is `float`.
# v1.1.21 - 2020-03-19

View File

@ -437,8 +437,9 @@ class Collection extends BaseCollection implements CompressInterface
/**
* Get a flattened array of the items in the collection.
* @param float|int $depth
*/
public function flatten(int $depth = INF): BaseCollection
public function flatten($depth = INF): BaseCollection
{
return $this->toBase()->flatten($depth);
}

View File

@ -170,7 +170,7 @@ class Arr
/**
* Flatten a multi-dimensional array into a single level.
* @param mixed $depth
* @param float|int $depth
*/
public static function flatten(array $array, $depth = INF): array
{

View File

@ -557,8 +557,9 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate
/**
* Get a flattened array of the items in the collection.
* @param float|int $depth
*/
public function flatten(int $depth = INF): self
public function flatten($depth = INF): self
{
return new static(Arr::flatten($this->items, $depth));
}

View File

@ -44,4 +44,18 @@ class CollectionTest extends TestCase
$res = $col->random(1);
$this->assertTrue($res instanceof Collection);
}
public function testFlatten()
{
$collection = new Collection([
'item' => [
'name' => 'Hyperf',
],
'it' => [
'id' => $uuid = uniqid(),
],
]);
$this->assertSame(['Hyperf', $uuid], $collection->flatten()->toArray());
}
}