Skip to content

Commit

Permalink
[5.x] Fix emptiness check on Value properties (#11402)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
godismyjudge95 and jasonvarga authored Feb 18, 2025
1 parent 40d0fb4 commit 212bb52
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Fields/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ public function __call(string $name, array $arguments)
return $this->value()->{$name}(...$arguments);
}

public function __isset($key)
{
return isset($this->value()?->{$key});
}

public function __get($key)
{
return $this->value()?->{$key} ?? null;
Expand Down
30 changes: 30 additions & 0 deletions tests/Fields/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,36 @@ public function it_can_iterate_over_values()
], $arr);
}

#[Test]
public function it_can_check_isset_on_properties()
{
$val = new Value((object) [
'a' => 'alfa',
'b' => '',
'c' => null,
]);

$this->assertTrue(isset($val->a));
$this->assertTrue(isset($val->b));
$this->assertFalse(isset($val->c));
$this->assertFalse(isset($val->d));
}

#[Test]
public function it_can_check_emptiness_on_properties()
{
$val = new Value((object) [
'a' => 'alfa',
'b' => '',
'c' => null,
]);

$this->assertFalse(empty($val->a));
$this->assertTrue(empty($val->b));
$this->assertTrue(empty($val->c));
$this->assertTrue(empty($val->d));
}

#[Test]
public function it_can_proxy_methods_to_value()
{
Expand Down

0 comments on commit 212bb52

Please sign in to comment.