From 639dc3947b0b49e018e2c093af24a8ed7be35a13 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 26 Oct 2017 18:21:52 -0500 Subject: [PATCH] Set a null value to attributes remove it from the collection --- src/CfdiUtils/Nodes/Attributes.php | 8 +++++-- tests/CfdiUtilsTests/Nodes/AttributesTest.php | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/CfdiUtils/Nodes/Attributes.php b/src/CfdiUtils/Nodes/Attributes.php index ccd08996..7b9bb705 100644 --- a/src/CfdiUtils/Nodes/Attributes.php +++ b/src/CfdiUtils/Nodes/Attributes.php @@ -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; } @@ -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) diff --git a/tests/CfdiUtilsTests/Nodes/AttributesTest.php b/tests/CfdiUtilsTests/Nodes/AttributesTest.php index f0559ebe..f13b5682 100644 --- a/tests/CfdiUtilsTests/Nodes/AttributesTest.php +++ b/tests/CfdiUtilsTests/Nodes/AttributesTest.php @@ -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')); + } }