Fixed bug that data_get cannot support int key. (#7081)

This commit is contained in:
aogg 2024-09-23 13:14:27 +08:00 committed by GitHub
parent 06575f32de
commit beb0a18b22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 5 deletions

View File

@ -1,5 +1,9 @@
# v3.1.42 - TBD
## Fixed
- [#7081](https://github.com/hyperf/hyperf/pull/7081) Fixed bug that `data_get` cannot support `int` key.
# v3.1.41 - 2024-09-19
## Added

View File

@ -44,18 +44,15 @@ function data_fill(&$target, $key, $value)
/**
* Get an item from an array or object using "dot" notation.
*
* @param mixed $target
* @param null|array|int|string $key
* @param mixed $default
* @return mixed
*/
function data_get($target, $key, $default = null)
function data_get(mixed $target, mixed $key, mixed $default = null): mixed
{
if (is_null($key)) {
return $target;
}
$key = is_array($key) ? $key : explode('.', $key);
$key = is_array($key) ? $key : explode('.', (string) $key);
foreach ($key as $i => $segment) {
unset($key[$i]);

View File

@ -383,6 +383,14 @@ class CollectionTest extends TestCase
$this->assertEquals(['a', 'b', 'c'], $c->replace(null)->all());
}
public function testDateGetWithInteger()
{
$data = ['id' => 1, 2 => 2];
$this->assertSame(1, \Hyperf\Collection\data_get($data, 'id'));
$this->assertSame(2, \Hyperf\Collection\data_get($data, 2));
}
public function testReplaceArray(): void
{
$c = new Collection(['a', 'b', 'c']);