Skip to content

Commit

Permalink
Fixed ArrayHelper::traverseHas() not returning true for null value
Browse files Browse the repository at this point in the history
Fixed ArrayHelper::traverseGet() not returning a null value
  • Loading branch information
ElGigi committed Apr 2, 2021
1 parent e7d04a3 commit 858ecf7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.1.4] - 2021-04-02

### Fixed

- Fixed ArrayHelper::traverseHas() not returning true for null value
- Fixed ArrayHelper::traverseGet() not returning a null value

## [1.1.3] - 2021-03-31

### Fixed
Expand Down
17 changes: 15 additions & 2 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Berlioz\Helpers;

use ArrayObject;
use InvalidArgumentException;
use SimpleXMLElement;
use Traversable;
Expand Down Expand Up @@ -146,7 +147,13 @@ public static function traverseExists(&$mixed, string $path): bool
return false;
}

if (!isset($temp[$key])) {
// An array, so we check existent of key
if (is_array($temp) && !array_key_exists($key, $temp)) {
return false;
}

// Not an array, so isset
if (!is_array($temp) && !isset($key, $temp)) {
return false;
}

Expand Down Expand Up @@ -180,7 +187,13 @@ public static function traverseGet(&$mixed, string $path, $default = null)
return $default;
}

if (!isset($temp[$key])) {
// An array, so we check existent of key
if ((is_array($temp) || $temp instanceof ArrayObject) && !array_key_exists($key, $temp)) {
return $default;
}

// Not an array, so isset
if (!(is_array($temp) || $temp instanceof ArrayObject) && !isset($key, $temp)) {
return $default;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ public function testTraverseExists()
'foo6' => [
'foo7' => 'bar7',
'foo8' => 'bar8',
'foo9' => null,
],
],
];

$this->assertTrue(ArrayHelper::traverseExists($tArray, 'foo'));
$this->assertTrue(ArrayHelper::traverseExists($tArray, 'foo2.foo6'));
$this->assertTrue(ArrayHelper::traverseExists($tArray, 'foo2.foo6.foo8'));
$this->assertTrue(ArrayHelper::traverseExists($tArray, 'foo2.foo6.foo9'));
$this->assertFalse(ArrayHelper::traverseExists($tArray, 'bar'));
$this->assertFalse(ArrayHelper::traverseExists($tArray, 'foo2.foo999.foo8'));
$this->assertFalse(ArrayHelper::traverseExists($tArray, 'foo3.foo4'));
Expand All @@ -157,12 +159,14 @@ public function testTraverseGet()
'foo6' => [
'foo7' => 'bar7',
'foo8' => 'bar8',
'foo9' => null,
],
],
];

$this->assertEquals('bar', ArrayHelper::traverseGet($tArray, 'foo'));
$this->assertEquals('bar8', ArrayHelper::traverseGet($tArray, 'foo2.foo6.foo8'));
$this->assertEquals(null, ArrayHelper::traverseGet($tArray, 'foo2.foo6.foo9'));
$this->assertEquals(null, ArrayHelper::traverseGet($tArray, 'foo2.foo999.foo8'));
$this->assertEquals('bar', ArrayHelper::traverseGet($tArray, 'foo2.foo999.foo8', 'bar'));
$this->assertEquals(null, ArrayHelper::traverseGet($tArray, 'foo3.foo4'));
Expand Down

0 comments on commit 858ecf7

Please sign in to comment.