Skip to content

Commit

Permalink
Merge branch '9.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Jun 23, 2023
2 parents 3dd52ff + 740c869 commit 76454c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 9.1.1 <small>(2023-06-23)</small>

#### New

* Added `Arr::append()` method.

--------------------------------------------------------

### 9.1.0 <small>(2023-04-15)</small>

#### New
Expand Down
26 changes: 26 additions & 0 deletions src/mako/utility/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ public static function set(array &$array, string $path, mixed $value): void
$array[array_shift($segments)] = $value;
}

/**
* Appends an array value using "dot notation".
*
* @param array &$array Array you want to modify
* @param string $path Array path
* @param mixed $value Value to append
*/
public static function append(array &$array, string $path, mixed $value): void
{
$segments = explode('.', $path);

while(count($segments) > 1)
{
$segment = array_shift($segments);

if(!isset($array[$segment]) || !is_array($array[$segment]))
{
$array[$segment] = [];
}

$array =& $array[$segment];
}

$array[array_shift($segments)][] = $value;
}

/**
* Search for an array value using "dot notation". Returns TRUE if the array key exists and FALSE if not.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/utility/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ public function testSet(): void
$this->assertEquals(['foo' => '123', 'bar' => ['baz' => '456', 'bax' => ['789']]], $arr);
}

/**
*
*/
public function testAppend(): void
{
$arr = ['foo' => ['bar' => []]];

Arr::append($arr, 'foo.bar', 'a');

Arr::append($arr, 'foo.bar', 'b');

Arr::append($arr, 'foo.bar', 'c');

$this->assertEquals(['a', 'b', 'c'], $arr['foo']['bar']);
}

/**
*
*/
Expand Down

0 comments on commit 76454c3

Please sign in to comment.