diff --git a/src/Price.php b/src/Price.php index 2c5e2aa..c4ee677 100644 --- a/src/Price.php +++ b/src/Price.php @@ -39,7 +39,7 @@ class Price implements \JsonSerializable /** * The VAT definition */ - protected ?Vat $vat; + protected ?Vat $vat = null; /** * The price modifiers to apply @@ -241,7 +241,7 @@ public function jsonSerialize(): array 'base' => $this->getRational($this->base)->getAmount(), 'currency' => $this->base->getCurrency()->getCurrencyCode(), 'units' => $this->units, - 'vat' => $this->vat->percentage(), + 'vat' => $this->vat?->percentage(), 'total' => [ 'exclusive' => $this->getRational($excl)->getAmount(), 'inclusive' => $this->getRational($incl)->getAmount(), diff --git a/tests/Unit/HasVatTest.php b/tests/Unit/HasVatTest.php index 2146faf..dc85736 100644 --- a/tests/Unit/HasVatTest.php +++ b/tests/Unit/HasVatTest.php @@ -68,3 +68,9 @@ expect($instance->inclusive(true)->__toString())->toBe('EUR 5.50'); }); +it('does not throw an exception when serializing a price with no vat', function () { + $instance = Price::ofMinor(500, 'EUR')->setUnits(3); + + expect($instance->jsonSerialize()) + ->toBeArray(); +});