Skip to content

Commit

Permalink
Renaming $index into $key for set and get method
Browse files Browse the repository at this point in the history
Why: in my opinion "index" is more related to array integer keys . "key" is more generic and refers to generic keys (string for example)
  • Loading branch information
roberto-butti committed Jun 13, 2024
1 parent 3ef2562 commit 7d7aa34
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## 1.0.4 - 2024-06-13
## 1.1.0 - 2024-06-13
- Add the Arr `set()` method supports 'dot' (or custom) notation for nested arrays for setting nested values, for example, `$arr`->set('first-level.second-level.third-level', "something")`
- Renamed the `$index` parameter, into `$key` for `set()` and `get()` method. Why: in my opinion "index" is more related to array integer keys . "key" is more generic and refers to generic keys (string for example).

## 1.0.3 - 2024-05-25
- Add the Arr `getArr()` method for retrieving portions of complex nested arrays as Arr object.
Expand Down
46 changes: 23 additions & 23 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ public function arr(): array
}

/**
* Get the element with $index
* Get the element with $key
*
* @param non-empty-string $charNestedKey
*/
public function get(mixed $index, mixed $defaultValue = null, string $charNestedKey = "."): mixed
public function get(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
{
if (is_string($index)) {
$indexString = strval($index);
if (str_contains($indexString, $charNestedKey)) {
if (is_string($key)) {
$keyString = strval($key);
if (str_contains($keyString, $charNestedKey)) {
$nestedValue = $this->arr;
foreach (explode($charNestedKey, $indexString) as $nestedKey) {
foreach (explode($charNestedKey, $keyString) as $nestedKey) {
if (is_array($nestedValue) && array_key_exists($nestedKey, $nestedValue)) {
$nestedValue = $nestedValue[$nestedKey];
} else {
Expand All @@ -127,34 +127,34 @@ public function get(mixed $index, mixed $defaultValue = null, string $charNested
return $nestedValue;
}
}
return $this->arr[$index] ?? $defaultValue;
return $this->arr[$key] ?? $defaultValue;
}

/**
* Get the element with $index as Arr object
* Get the element with $key as Arr object
* This is helpful when the element is an array, and you
* need to get the Arr object instead of the classic array
* In the case the $index doesn't exist, an empty Arr can be returned
* In the case the $key doesn't exist, an empty Arr can be returned
* @param non-empty-string $charNestedKey
*/
public function getArr(mixed $index, mixed $defaultValue = null, string $charNestedKey = "."): Arr
public function getArr(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr
{
$value = $this->getArrNullable($index, $defaultValue, $charNestedKey);
$value = $this->getArrNullable($key, $defaultValue, $charNestedKey);
if (is_null($value)) {
return Arr::make([]);
}
return $value;
}
/**
* Get the element with $index as Arr object
* Get the element with $key as Arr object
* This is helpful when the element is an array, and you
* need to get the Arr object instead of the classic array
* In the case the $index doesn't exist, null can be returned
* In the case the $key doesn't exist, null can be returned
* @param non-empty-string $charNestedKey
*/
public function getArrNullable(mixed $index, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null
public function getArrNullable(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null
{
$value = $this->get($index, $defaultValue, $charNestedKey);
$value = $this->get($key, $defaultValue, $charNestedKey);
if (is_null($value)) {
return null;
}
Expand All @@ -173,15 +173,15 @@ public function getArrNullable(mixed $index, mixed $defaultValue = null, string


/**
* Set a value to a specific $index
* Set a value to a specific $key
* You can use the dot notation for setting a nested value.
* @param non-empty-string $charNestedKey
*/
public function set(int|string $index, mixed $value, string $charNestedKey = "."): void
public function set(int|string $key, mixed $value, string $charNestedKey = "."): void
{
if (is_string($index)) {
if (is_string($key)) {
$array = &$this->arr;
$keys = explode($charNestedKey, $index);
$keys = explode($charNestedKey, $key);
foreach ($keys as $i => $key) {
if (count($keys) === 1) {
break;
Expand All @@ -199,16 +199,16 @@ public function set(int|string $index, mixed $value, string $charNestedKey = "."
return;

}
$this->arr[$index] = $value;
$this->arr[$key] = $value;
}

/**
* Unset an array element by their key if it exists
*/
public function unset(mixed $index): bool
public function unset(mixed $key): bool
{
if ($this->get($index)) {
unset($this->arr[$index]);
if ($this->get($key)) {
unset($this->arr[$key]);
return true;
}

Expand Down

0 comments on commit 7d7aa34

Please sign in to comment.