mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Fixed bug that data_get
cannot support int
key. (#7081)
This commit is contained in:
parent
06575f32de
commit
beb0a18b22
@ -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
|
||||
|
@ -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]);
|
||||
|
@ -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']);
|
||||
|
Loading…
Reference in New Issue
Block a user