Skip to content

Commit

Permalink
fix null value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed May 26, 2017
1 parent 9d0ab11 commit f5335f3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ protected static function call($path, $target, callable $outOfBoundsHandler)
foreach (self::normalizePath($path) as $key) {
$trace[] = $key;

if (isset($value->$key)) {
if (is_object($value) && property_exists($value, $key)) {
$value = $value->$key;
continue;
}

if (is_array($value) && isset($value[$key])) {
if (is_array($value) && array_key_exists($key, $value)) {
$value = $value[$key];
continue;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/ArrayAccessObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class ArrayAccessObject implements \ArrayAccess
/**
* Create new ArrayAccessObject.
*
* @param array|null $container prefilled container
* @param array $container prefilled container
*/
public function __construct(array $container = null)
public function __construct(array $container = array())
{
$this->container = $container ?: array();
$this->container = $container;
}

public function offsetSet($offset, $value)
Expand All @@ -37,12 +37,12 @@ public function offsetSet($offset, $value)

public function offsetExists($offset)
{
return isset($this->container[$offset]);
return array_key_exists($offset, $this->container);
}

public function offsetGet($offset)
{
return isset($this->container[$offset]) ? $this->container[$offset] : null;
return $this->offsetExists($offset) ? $this->container[$offset] : null;
}

public function offsetUnset($offset)
Expand Down
40 changes: 40 additions & 0 deletions tests/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
Expand Down Expand Up @@ -126,6 +127,45 @@ public function testValueShouldWorkWithNull()
$this->assertEquals(value(['foo', 'bar'], null, 42), 42);
}


public function testValueShouldReturnArrayNullValues()
{
$array = ['foo' => null];
$this->assertEquals(value('foo', $array, 'FAIL'), null);
}

public function testValueOrFailShouldReturnArrayNullValues()
{
$array = ['foo' => null];
$this->assertEquals(valueOrFail('foo', $array, 'FAIL'), null);
}

public function testValueShouldReturnArrayAccessNullValues()
{
$arrayAccesss = new ArrayAccessObject(['foo' => null]);
$this->assertEquals(value('foo', $arrayAccesss, 'FAIL'), null);
}

public function testValueOrFailShouldReturnArrayAccessNullValues()
{
$arrayAccesss = new ArrayAccessObject(['foo' => null]);
$this->assertEquals(valueOrFail('foo', $arrayAccesss, 'FAIL'), null);
}

public function testValueShouldReturnObjectNullProperties()
{
$object = new \stdClass();
$object->foo = null;
$this->assertEquals(value('foo', $object, 'FAIL'), null);
}

public function testValueOrFailShouldReturnObjectNullProperties()
{
$object = new \stdClass();
$object->foo = null;
$this->assertEquals(valueOrFail('foo', $object, 'FAIL'), null);
}

public function testValueOrFailWithArrayShouldReturn()
{
$this->assertEquals(valueOrFail(1, ['foo', 'bar']), 'bar');
Expand Down

0 comments on commit f5335f3

Please sign in to comment.