From 9ac6ac9fcf94c8b274a1ff188cff3a8e8e62036f Mon Sep 17 00:00:00 2001 From: okangr Date: Thu, 29 Feb 2024 21:14:46 +0200 Subject: [PATCH] Fix: null values are removed from the generated xml. --- src/Xml/XMLWriter.php | 4 +++ tests/XmlNullValuesTest.php | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/XmlNullValuesTest.php diff --git a/src/Xml/XMLWriter.php b/src/Xml/XMLWriter.php index ee5f062..00f7b4c 100644 --- a/src/Xml/XMLWriter.php +++ b/src/Xml/XMLWriter.php @@ -22,6 +22,10 @@ public function __construct() protected function build(DOMNode $parent, string $nodeName, mixed $nodeValue, $namespace = null): void { + if (is_null($nodeValue)) { + return; + } + if ($nodeValue instanceof Type) { $this->buildType($parent, $nodeName, $nodeValue); return; diff --git a/tests/XmlNullValuesTest.php b/tests/XmlNullValuesTest.php new file mode 100644 index 0000000..16ad9d8 --- /dev/null +++ b/tests/XmlNullValuesTest.php @@ -0,0 +1,66 @@ +setClassificationType(null); + $icls1->setClassificationCategory(IncomeClassificationCategory::CATEGORY_3); + $icls1->setAmount(0); + + $icls2 = new IncomeClassification(); + $icls2->setClassificationType(IncomeClassificationType::E3_106); + $icls2->setClassificationCategory(IncomeClassificationCategory::CATEGORY_1_3); + $icls2->setAmount(0); + + $ecls = new ExpensesClassification(); + $ecls->setClassificationType(null); + $ecls->setClassificationCategory(ExpenseClassificationCategory::CATEGORY_2_1); + $ecls->setAmount(0); + + $row = new InvoiceDetails(); + $row->addIncomeClassification($icls1); + $row->addIncomeClassification($icls2); + $row->addExpensesClassification($ecls); + + $invoice = new Invoice(); + $invoice->addInvoiceDetails($row); + + $classifications = $this->toXML($invoice)->InvoicesDoc->invoice->invoiceDetails; + + $income = $classifications->incomeClassification; + + $this->assertCount(2, $income); + $this->assertCount(2, $income[0]); + + $this->assertEquals(IncomeClassificationCategory::CATEGORY_3->value, $income[0]->get('icls:classificationCategory')); + $this->assertEquals(0, $income[0]->get('icls:amount')); + $this->assertArrayNotHasKey('icls:classificationType', $income[0]); + + $this->assertEquals(IncomeClassificationCategory::CATEGORY_1_3->value, $income[1]->get('icls:classificationCategory')); + $this->assertEquals(0, $income[1]->get('icls:amount')); + $this->assertEquals(IncomeClassificationType::E3_106->value, $income[1]->get('icls:classificationType')); + + $expenses = $classifications->expensesClassification; + $this->assertCount(2, $expenses); + + $this->assertEquals(ExpenseClassificationCategory::CATEGORY_2_1->value, $expenses->get('ecls:classificationCategory')); + $this->assertEquals(0, $expenses->get('ecls:amount')); + $this->assertArrayNotHasKey('ecls:classificationType', $expenses); + } +} \ No newline at end of file