Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/TwigHooks/src/Bag/DataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(private array $container = [])

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

public function offsetGet(mixed $offset): mixed
Expand All @@ -55,6 +55,11 @@ public function offsetUnset(mixed $offset): void
unset($this->container[$offset]);
}

public function __isset(string $name): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed privately, this is not cool, cause it changes the default behaviour of the native php method.

{
return array_key_exists($name, $this->container);
}

public function __get(string $name): mixed
{
return $this->container[$name] ?? null;
Expand Down
15 changes: 13 additions & 2 deletions src/TwigHooks/tests/Unit/Bag/DataBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public function testItCreatesEmptyBagByDefault(): void

public function testItReturnsWhetherGivenOffsetExists(): void
{
$dataBag = new DataBag(['foo' => 'bar']);
$dataBag = new DataBag(['foo' => 'bar', 'null_value' => null]);

$this->assertTrue(isset($dataBag['foo']));
$this->assertTrue(isset($dataBag['null_value']));
$this->assertFalse(isset($dataBag['bar']));
}

Expand Down Expand Up @@ -96,9 +97,19 @@ public function testItReturnsGivenProperty(): void

public function testItReturnsWhetherGivenPropertyExists(): void
{
$dataBag = new DataBag(['foo' => 'bar']);
$dataBag = new DataBag(['foo' => 'bar', 'null_value' => null]);

$this->assertTrue($dataBag->has('foo'));
$this->assertTrue($dataBag->has('null_value'));
$this->assertFalse($dataBag->has('bar'));
}

public function testItReturnsWhetherGivenPropertyIsSet(): void
{
$dataBag = new DataBag(['foo' => 'bar', 'null_value' => null]);

$this->assertTrue(isset($dataBag->foo));
$this->assertTrue(isset($dataBag->null_value));
$this->assertFalse(isset($dataBag->baz));
}
}