Skip to content

Commit

Permalink
[5.6] Return null if object property is undefined (#23267)
Browse files Browse the repository at this point in the history
* return null if object property is undefined

* add array test
  • Loading branch information
tillkruss authored and taylorotwell committed Feb 23, 2018
1 parent 237caed commit 5acb717
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($value)
public function __get($key)
{
if (is_object($this->value)) {
return $this->value->{$key};
return $this->value->{$key} ?? null;
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,15 +786,16 @@ public function something()

public function testOptionalWithArray()
{
$this->assertNull(optional(null)['missing']);

$this->assertEquals('here', optional(['present' => 'here'])['present']);
$this->assertNull(optional(null)['missing']);
$this->assertNull(optional(['present' => 'here'])->missing);
}

public function testOptionalReturnsObjectPropertyOrNull()
{
$this->assertSame('bar', optional((object) ['foo' => 'bar'])->foo);
$this->assertNull(optional(['foo' => 'bar'])->foo);
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);
}

public function testOptionalDeterminesWhetherKeyIsSet()
Expand Down

0 comments on commit 5acb717

Please sign in to comment.