Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4|^8.0"
"php": "^7.4|^8.0",
"yiisoft/strings": "^1.0.1"
},
"require-dev": {
"infection/infection": "^0.16.3 || ^0.17.0",
"phpunit/phpunit": "^9.3",
"vimeo/psalm": "^3.16"
"infection/infection": "^0.16.6 || ^0.17.7",
"phpunit/phpunit": "^9.4",
"vimeo/psalm": "^3.17"
},
"autoload": {
"psr-4": {
Expand Down
49 changes: 29 additions & 20 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Yiisoft\Arrays;

use Closure;
use InvalidArgumentException;
use Throwable;
use Yiisoft\Arrays\Modifier\ModifierInterface;
use Yiisoft\Arrays\Modifier\ReverseBlockMerge;
use Yiisoft\Strings\NumericHelper;

/**
* Yii array helper provides static methods allowing you to deal with arrays more efficiently.
Expand Down Expand Up @@ -220,7 +222,7 @@ public static function applyModifiers(array $data): array
* ```
*
* @param array|object $array array or object to extract value from
* @param string|int|float|\Closure|array $key key name of the array element,
* @param string|int|float|Closure|array $key key name of the array element,
* an array of keys or property name of the object, or an anonymous function
* returning the value. The anonymous function signature should be:
* `function($array, $defaultValue)`.
Expand All @@ -230,7 +232,7 @@ public static function applyModifiers(array $data): array
*/
public static function getValue($array, $key, $default = null)
{
if ($key instanceof \Closure) {
if ($key instanceof Closure) {
return $key($array, $default);
}

Expand Down Expand Up @@ -261,8 +263,9 @@ public static function getValue($array, $key, $default = null)
*/
private static function getRootValue($array, $key, $default)
{
if (is_array($array) && array_key_exists((string)$key, $array)) {
return $array[(string)$key];
if (is_array($array)) {
$key = static::normalizeArrayKey($key);
return array_key_exists($key, $array) ? $array[$key] : $default;
}

if (is_object($array)) {
Expand Down Expand Up @@ -308,7 +311,7 @@ private static function getRootValue($array, $key, $default)
* ```
*
* @param array|object $array array or object to extract value from
* @param string|int|float|\Closure|array $path key name of the array element, an array of keys or property name
* @param string|int|float|Closure|array $path key name of the array element, an array of keys or property name
* of the object, or an anonymous function returning the value. The anonymous function signature should be:
* `function($array, $defaultValue)`.
* @param mixed $default the default value to be returned if the specified array key does not exist. Not used when
Expand Down Expand Up @@ -366,7 +369,7 @@ public static function setValue(array &$array, $key, $value): void
$keys = is_array($key) ? $key : [$key];

while (count($keys) > 1) {
$k = array_shift($keys);
$k = static::normalizeArrayKey(array_shift($keys));
if (!isset($array[$k])) {
$array[$k] = [];
}
Expand All @@ -376,7 +379,7 @@ public static function setValue(array &$array, $key, $value): void
$array = &$array[$k];
}

$array[array_shift($keys)] = $value;
$array[static::normalizeArrayKey(array_shift($keys))] = $value;
}

/**
Expand Down Expand Up @@ -485,15 +488,15 @@ public static function remove(array &$array, $key, $default = null)
$keys = is_array($key) ? $key : [$key];

while (count($keys) > 1) {
$key = array_shift($keys);
$key = static::normalizeArrayKey(array_shift($keys));
if (!isset($array[$key]) || !is_array($array[$key])) {
return $default;
}
$array = &$array[$key];
}

$key = array_shift($keys);
if (array_key_exists((string)$key, $array)) {
$key = static::normalizeArrayKey(array_shift($keys));
if (array_key_exists($key, $array)) {
$value = $array[$key];
unset($array[$key]);
return $value;
Expand Down Expand Up @@ -651,8 +654,8 @@ public static function removeValue(array &$array, $value): array
* ```
*
* @param array $array the array that needs to be indexed or grouped
* @param string|\Closure|null $key the column name or anonymous function which result will be used to index the array
* @param string|string[]|\Closure[]|null $groups the array of keys, that will be used to group the input array
* @param string|Closure|null $key the column name or anonymous function which result will be used to index the array
* @param string|string[]|Closure[]|null $groups the array of keys, that will be used to group the input array
* by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not
* defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added
* to the result array without any key.
Expand Down Expand Up @@ -681,10 +684,7 @@ public static function index(array $array, $key, $groups = []): array
} else {
$value = static::getValue($element, $key);
if ($value !== null) {
if (is_float($value)) {
$value = str_replace(',', '.', (string)$value);
}
$lastArray[$value] = $element;
$lastArray[static::normalizeArrayKey($value)] = $element;
}
}
unset($lastArray);
Expand Down Expand Up @@ -714,7 +714,7 @@ public static function index(array $array, $key, $groups = []): array
* ```
*
* @param array $array
* @param string|\Closure $name
* @param string|Closure $name
* @param bool $keepKeys whether to maintain the array keys. If false, the resulting array
* will be re-indexed with integers.
* @return array the list of column values
Expand Down Expand Up @@ -771,9 +771,9 @@ public static function getColumn(array $array, $name, bool $keepKeys = true): ar
* ```
*
* @param array $array
* @param string|\Closure $from
* @param string|\Closure $to
* @param string|\Closure|null $group
* @param string|Closure $from
* @param string|Closure $to
* @param string|Closure|null $group
* @return array
*/
public static function map(array $array, $from, $to, $group = null): array
Expand Down Expand Up @@ -1107,4 +1107,13 @@ public static function getObjectVars(object $object): ?array
{
return get_object_vars($object);
}

/**
* @param int|string|float $key
* @return string
*/
private static function normalizeArrayKey($key): string
{
return is_float($key) ? NumericHelper::normalize($key) : (string)$key;
}
}
30 changes: 30 additions & 0 deletions tests/ArrayHelper/ApplyModifiersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Arrays\Tests\ArrayHelper;

use PHPUnit\Framework\TestCase;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Arrays\Modifier\ReplaceValue;
use Yiisoft\Arrays\Modifier\UnsetValue;

/**
* @see MergeTest
*/
final class ApplyModifiersTest extends TestCase
{
public function testBase(): void
{
$array = [
'a' => 1,
'b' => new ReplaceValue(2),
'c' => new UnsetValue(),
];

$this->assertSame([
'a' => 1,
'b' => 2,
], ArrayHelper::applyModifiers($array));
}
}
Loading