diff --git a/src/Models/Response.php b/src/Models/Response.php index 7fd2218..ecca686 100644 --- a/src/Models/Response.php +++ b/src/Models/Response.php @@ -187,4 +187,13 @@ public function isSuccessful(): bool { return $this->getStatusCode() === 'Success'; } + + public function set($key, $value): static + { + if ($key === 'receptionEmails' && empty($value)) { + return parent::set('receptionEmails', new ReceptionEmails()); + } + + return parent::set($key, $value); + } } \ No newline at end of file diff --git a/src/Models/TypeArray.php b/src/Models/TypeArray.php index ba3d238..93e3c86 100644 --- a/src/Models/TypeArray.php +++ b/src/Models/TypeArray.php @@ -125,7 +125,7 @@ public function offsetUnset($offset): void public function count(): int { - return count($this->attributes[$this->childKey]); + return count($this->attributes[$this->childKey] ?? []); } /** diff --git a/stubs/response-doc-with-empty-reception-emails.xml b/stubs/response-doc-with-empty-reception-emails.xml new file mode 100644 index 0000000..d24a886 --- /dev/null +++ b/stubs/response-doc-with-empty-reception-emails.xml @@ -0,0 +1,11 @@ + + + + 1 + 6F825B9B74717280D1F9A38252D0B65604C6D162 + 480301204040191 + https://mydataapidev.aade.gr/TimologioQR/QRInfo?q=sSsxThUx5i3O8ankrp8XUDiL0q12vbzuWZP0nEfMgAyQ2nbm7s6vSz3CYYcmod6i0uCrPCvSLrQ5N1qxEHF8JsfMNz6NmibNhBNXJiR8ecC%3d + Success + + + \ No newline at end of file diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 15dc745..d6bff43 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -26,6 +26,21 @@ public function test_it_reads_response_correctly() $this->assertEquals("invoices@test.gr", $doc[0]->getReceptionEmails()->offsetGet(1)); } + public function test_it_reads_response_with_empty_reception_emails_correctly() + { + $doc = $this->getResponseDocFromXml('response-doc-with-empty-reception-emails'); + + $this->assertCount(1, $doc); + $this->assertEquals(1, $doc[0]->getIndex()); + $this->assertEquals("6F825B9B74717280D1F9A38252D0B65604C6D162", $doc[0]->getInvoiceUid()); + $this->assertEquals("480301204040191", $doc[0]->getInvoiceMark()); + $this->assertEquals("https://mydataapidev.aade.gr/TimologioQR/QRInfo?q=sSsxThUx5i3O8ankrp8XUDiL0q12vbzuWZP0nEfMgAyQ2nbm7s6vSz3CYYcmod6i0uCrPCvSLrQ5N1qxEHF8JsfMNz6NmibNhBNXJiR8ecC%3d", $doc[0]->getQrUrl()); + $this->assertEquals("Success", $doc[0]->getStatusCode()); + + $this->assertIsArray($doc[0]->getReceptionEmails()->all()); + $this->assertCount(0, $doc[0]->getReceptionEmails()); + } + public function test_it_reads_errors_correctly() { $doc = $this->getResponseDocFromXml('response-doc-with-errors');