Skip to content

Commit

Permalink
Backported isset/empty fix from 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
prewk committed Feb 28, 2018
1 parent d50a371 commit 2564781
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,25 @@ public function make($init = []) {
return $record;
}

/**
* Running isset($record->field) should:
* - Return false if the field isn't set
* - Return isset($value) if the field is set
*
* @param $name
* @return bool
*/
final public function __isset($name)
{
if (!$this->has($name)) {
return false;
}

$value = $this->get($name);

return isset($value);
}

/**
* Merge the record with another structure
*
Expand Down
47 changes: 47 additions & 0 deletions tests/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,51 @@ public function test_iterator_interface()
$this->assertEquals(["foo", "bar", "baz"], $keys);
$this->assertEquals([123, null, 456], $values);
}

public function test_that_magic_getter_works()
{
$record = new TestWithDefaultsRecord;

$this->assertEquals(123, $record->foo);
$this->assertEquals(null, $record->bar);
$this->assertEquals(456, $record->baz);
}

public function test_that_isset_works_on_magic_getters()
{
$record = new TestWithDefaultsRecord;

$this->assertTrue(isset($record->foo));
$this->assertFalse(isset($record->bar));
$this->assertTrue(isset($record->baz));
}

public function test_that_empty_works_on_magic_getters()
{
$record = (new TestWithoutDefaultsRecord)->make([
"foo" => [],
"bar" => [1, 2, 3],
"baz" => null,
]);

$this->assertTrue(empty($record->foo));
$this->assertFalse(empty($record->bar));
$this->assertTrue(empty($record->baz));

$record = $record->merge([
"foo" => 0,
"bar" => 1,
"baz" => "",
]);

$this->assertTrue(empty($record->foo));
$this->assertFalse(empty($record->bar));
$this->assertTrue(empty($record->baz));

$record = $record->merge([
"foo" => "test",
]);

$this->assertFalse(empty($record->foo));
}
}

0 comments on commit 2564781

Please sign in to comment.