Skip to content

Commit

Permalink
implement __isset on Optional (#24031)
Browse files Browse the repository at this point in the history
  • Loading branch information
binotaliu committed Apr 29, 2018
1 parent 4a49ba5 commit b4fdee2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Support/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ public function __call($method, $parameters)
}
}

/**
* Dynamically check a property exists on the underlying object.
*
* @param $name
* @return bool
*/
public function __isset($name)
{
if (is_object($this->value)) {
return isset($this->value->{$name});
}

if (is_array($this->value) || $this->value instanceof \ArrayObject) {
return isset($this->value[$name]);
}

return false;
}

/**
* Determine if an item exists at an offset.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Support/SupportOptionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ public function testGetNotExistItemOnObject()
$this->assertNull($optional->item);
}

public function testIssetExistItemOnObject()
{
$targetObj = new \stdClass;
$targetObj->item = '';

$optional = new Optional($targetObj);

$this->assertTrue(isset($optional->item));
}

public function testIssetNotExistItemOnObject()
{
$targetObj = new \stdClass;

$optional = new Optional($targetObj);

$this->assertFalse(isset($optional->item));
}

public function testGetExistItemOnArray()
{
$expected = 'test';
Expand All @@ -49,4 +68,24 @@ public function testGetNotExistItemOnArray()

$this->assertNull($optional['item']);
}

public function testIssetExistItemOnArray()
{
$targetArr = [
'item' => '',
];

$optional = new Optional($targetArr);

$this->assertTrue(isset($optional['item']));
}

public function testIssetNotExistItemOnArray()
{
$targetArr = [];

$optional = new Optional($targetArr);

$this->assertFalse(isset($optional['item']));
}
}

0 comments on commit b4fdee2

Please sign in to comment.