Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a null value to attributes remove it from the collection #9

Merged
merged 1 commit into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/CfdiUtils/Nodes/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function exists(string $name): bool
public function importArray(array $attributes): self
{
foreach ($attributes as $key => $value) {
$this->set((string) $key, $this->castValueToString($value));
$this[$key] = $value;
}
return $this;
}
Expand Down Expand Up @@ -79,7 +79,11 @@ public function offsetGet($offset)

public function offsetSet($offset, $value)
{
$this->set((string) $offset, $this->castValueToString($value));
if (null === $value) {
$this->remove((string) $offset);
} else {
$this->set((string) $offset, $this->castValueToString($value));
}
}

public function offsetUnset($offset)
Expand Down
22 changes: 22 additions & 0 deletions tests/CfdiUtilsTests/Nodes/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,26 @@ public function testIterator()
}
$this->assertEquals($data, $created);
}

public function testSetToNullPerformRemove()
{
$attributes = new Attributes([
'foo' => 'bar',
]);
$this->assertTrue($attributes->exists('foo'));
$attributes['foo'] = null;
$this->assertFalse($attributes->exists('foo'));
}

public function testImportWithNullPerformRemove()
{
$attributes = new Attributes([
'foo' => 'bar',
]);
$this->assertTrue($attributes->exists('foo'));
$attributes->importArray([
'foo' => null,
]);
$this->assertFalse($attributes->exists('foo'));
}
}