Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for BackedEnums to session #53235

Closed
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
53 changes: 40 additions & 13 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArgumentCountError;
use ArrayAccess;
use BackedEnum;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use Random\Randomizer;
Expand All @@ -27,7 +28,7 @@ public static function accessible($value)
* Add an element to an array using "dot" notation if it doesn't exist.
*
* @param array $array
* @param string|int|float $key
* @param string|\BackedEnum|int|float $key
* @param mixed $value
* @return array
*/
Expand Down Expand Up @@ -144,7 +145,7 @@ public static function undot($array)
* Get all of the given array except for a specified array of keys.
*
* @param array $array
* @param array|string|int|float $keys
* @param array|string|\BackedEnum|int|float $keys
* @return array
*/
public static function except($array, $keys)
Expand All @@ -158,11 +159,13 @@ public static function except($array, $keys)
* Determine if the given key exists in the provided array.
*
* @param \ArrayAccess|array $array
* @param string|int|float $key
* @param string|\BackedEnum|int|float $key
* @return bool
*/
public static function exists($array, $key)
{
$key = $key instanceof BackedEnum ? $key->value : $key;

if ($array instanceof Enumerable) {
return $array->has($key);
}
Expand Down Expand Up @@ -280,20 +283,24 @@ public static function flatten($array, $depth = INF)
* Remove one or many array items from a given array using "dot" notation.
*
* @param array $array
* @param array|string|int|float $keys
* @param array|string|\BackedEnum|int|float $keys
* @return void
*/
public static function forget(&$array, $keys)
{
$original = &$array;

$keys = $keys instanceof BackedEnum ? $keys->value : $keys;

$keys = (array) $keys;

if (count($keys) === 0) {
return;
}

foreach ($keys as $key) {
$key = $key instanceof BackedEnum ? $key->value : $key;

// if the exact key exists in the top-level, remove it
if (static::exists($array, $key)) {
unset($array[$key]);
Expand Down Expand Up @@ -324,7 +331,7 @@ public static function forget(&$array, $keys)
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string|int|null $key
* @param string|\BackedEnum|int|null $key
* @param mixed $default
* @return mixed
*/
Expand All @@ -338,6 +345,8 @@ public static function get($array, $key, $default = null)
return $array;
}

$key = $key instanceof BackedEnum ? $key->value : $key;

if (static::exists($array, $key)) {
return $array[$key];
}
Expand All @@ -361,18 +370,22 @@ public static function get($array, $key, $default = null)
* Check if an item or items exist in an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string|array $keys
* @param string|\BackedEnum|array $keys
* @return bool
*/
public static function has($array, $keys)
{
$keys = $keys instanceof BackedEnum ? $keys->value : $keys;

$keys = (array) $keys;

if (! $array || $keys === []) {
return false;
}

foreach ($keys as $key) {
$key = $key instanceof BackedEnum ? $key->value : $key;

$subKeyArray = $array;

if (static::exists($array, $key)) {
Expand All @@ -395,7 +408,7 @@ public static function has($array, $keys)
* Determine if any of the keys exist in an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string|array $keys
* @param string|\BackedEnum|array $keys
* @return bool
*/
public static function hasAny($array, $keys)
Expand All @@ -404,6 +417,8 @@ public static function hasAny($array, $keys)
return false;
}

$keys = $keys instanceof BackedEnum ? $keys->value : $keys;

$keys = (array) $keys;

if (! $array) {
Expand Down Expand Up @@ -504,29 +519,35 @@ public static function prependKeysWith($array, $prependWith)
* Get a subset of the items from the given array.
*
* @param array $array
* @param array|string $keys
* @param array|string|\BackedEnum $keys
* @return array
*/
public static function only($array, $keys)
{
$keys = $keys instanceof BackedEnum ? $keys->value : $keys;

return array_intersect_key($array, array_flip((array) $keys));
}

/**
* Select an array of values from an array.
*
* @param array $array
* @param array|string $keys
* @param array|string|\BackedEnum $keys
* @return array
*/
public static function select($array, $keys)
{
$keys = $keys instanceof BackedEnum ? $keys->value : $keys;

$keys = static::wrap($keys);

return static::map($array, function ($item) use ($keys) {
$result = [];

foreach ($keys as $key) {
$key = $key instanceof BackedEnum ? $key->value : $key;

if (Arr::accessible($item) && Arr::exists($item, $key)) {
$result[$key] = $item[$key];
} elseif (is_object($item) && isset($item->{$key})) {
Expand All @@ -543,7 +564,7 @@ public static function select($array, $keys)
*
* @param iterable $array
* @param string|array|int|null $value
* @param string|array|null $key
* @param string|\BackedEnum|array|null $key
* @return array
*/
public static function pluck($array, $value, $key = null)
Expand All @@ -553,6 +574,8 @@ public static function pluck($array, $value, $key = null)
[$value, $key] = static::explodePluckParameters($value, $key);

foreach ($array as $item) {
$value = $value instanceof BackedEnum ? $value->value : $value;

$itemValue = data_get($item, $value);

// If the key is "null", we will just append the value to the array and keep
Expand All @@ -578,13 +601,15 @@ public static function pluck($array, $value, $key = null)
* Explode the "value" and "key" arguments passed to "pluck".
*
* @param string|array $value
* @param string|array|null $key
* @param string|\BackedEnum|array|null $key
* @return array
*/
protected static function explodePluckParameters($value, $key)
{
$value = is_string($value) ? explode('.', $value) : $value;

$key = $key instanceof BackedEnum ? $key->value : $key;

$key = is_null($key) || is_array($key) ? $key : explode('.', $key);

return [$value, $key];
Expand Down Expand Up @@ -681,7 +706,7 @@ public static function prepend($array, $value, $key = null)
* Get a value from the array, and remove it.
*
* @param array $array
* @param string|int $key
* @param string|\BackedEnum|int $key
* @param mixed $default
* @return mixed
*/
Expand Down Expand Up @@ -758,7 +783,7 @@ public static function random($array, $number = null, $preserveKeys = false)
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param string|int|null $key
* @param string|\BackedEnum|int|null $key
* @param mixed $value
* @return array
*/
Expand All @@ -768,6 +793,8 @@ public static function set(&$array, $key, $value)
return $array = $value;
}

$key = $key instanceof BackedEnum ? $key->value : $key;

$keys = explode('.', $key);

foreach ($keys as $i => $key) {
Expand Down
Loading