Skip to content

Commit

Permalink
Merge pull request #9 from driftking301/version2
Browse files Browse the repository at this point in the history
Set a null value to attributes remove it from the collection
  • Loading branch information
eclipxe13 committed Oct 27, 2017
2 parents aba79fd + 639dc39 commit adff88d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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'));
}
}

0 comments on commit adff88d

Please sign in to comment.