Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Clients/IbericodeVatRatesClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function parseResponse(string $response_body): array
$return = [];
foreach ($result->items as $country => $periods) {
foreach ($periods as $i => $period) {
$periods[$i] = new Period(new \DateTimeImmutable($period->effective_from), (array) $period->rates);
$periods[$i] = new Period(new \DateTimeImmutable($period->effective_from), (array) $period->rates, $period->exceptions ?? []);
}

$return[$country] = $periods;
Expand Down
25 changes: 22 additions & 3 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,43 @@ class Period
{
private $effectiveFrom;
private $rates = [];
private $exceptions = [];

public function __construct(DateTimeInterface $effectiveFrom, array $rates)
public function __construct(DateTimeInterface $effectiveFrom, array $rates, array $exceptions = [])
{
$this->effectiveFrom = $effectiveFrom;
$this->rates = $rates;
$this->exceptions = $exceptions;
}

public function getEffectiveFrom(): DateTimeInterface
{
return $this->effectiveFrom;
}

public function getRate(string $level): float
public function getRate(string $level, ?string $postcode = null): float
{
if (!isset($this->rates[$level])) {
throw new InvalidArgumentException("Invalid rate level: {$level}");
}

return $this->rates[$level];
return $this->getExceptionRate($level, $postcode) ?? $this->rates[$level];
}

private function getExceptionRate(string $level, ?string $postcode): ?float
{
if (!$postcode) {
return null;
}

foreach ($this->exceptions as $exception) {
$exception = (array) $exception;
if (preg_match('/^'.$exception['postcode'].'$/', $postcode)) {
return $exception[$level] ?? $exception[Rates::RATE_STANDARD];
}
}

return null;
}

}
10 changes: 6 additions & 4 deletions src/Rates.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,26 @@ private function resolvePeriod(string $countryCode, DateTimeInterface $datetime)
/**
* @param string $countryCode ISO-3166-1-alpha2 country code
* @param string $level
* @param ?string $postcode
* @return float
* @throws \Exception
*/
public function getRateForCountry(string $countryCode, string $level = self::RATE_STANDARD): float
public function getRateForCountry(string $countryCode, string $level = self::RATE_STANDARD, ?string $postcode = null): float
{
$todayMidnight = new \DateTimeImmutable('today midnight');
return $this->getRateForCountryOnDate($countryCode, $todayMidnight, $level);
return $this->getRateForCountryOnDate($countryCode, $todayMidnight, $level, $postcode);
}

/**
* @param string $countryCode ISO-3166-1-alpha2 country code
* @param DateTimeInterface $datetime
* @param string $level
* @param ?string $postcode
* @return float
* @throws Exception
*/
public function getRateForCountryOnDate(string $countryCode, \DateTimeInterface $datetime, string $level = self::RATE_STANDARD): float
public function getRateForCountryOnDate(string $countryCode, \DateTimeInterface $datetime, string $level = self::RATE_STANDARD, ?string $postcode = null) : float
{
return $this->resolvePeriod($countryCode, $datetime)->getRate($level);
return $this->resolvePeriod($countryCode, $datetime)->getRate($level, $postcode);
}
}
20 changes: 20 additions & 0 deletions tests/RatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ private function getRatesClientMock(): \PHPUnit\Framework\MockObject\MockObject
new Period(new \DateTimeImmutable('2019/01/01'), [
'standard' => 21.00,
'reduced' => 9.00,
], [
[
"name" => "Park Frankendael",
"postcode" => "1097",
"standard" => 0
],
[
"name" => "Park de Meer",
"postcode" => "(1098|1099)",
"standard" => 0
]
])
]
]);
Expand All @@ -53,6 +64,15 @@ public function testGetRateForCountry()
$this->assertEquals(21.0, $rates->getRateForCountry('NL'));
}

public function testGetRateForCountryAndPostcode()
{
$client = $this->getRatesClientMock();
$rates = new Rates('vendor/rates', 30, $client);
$this->assertEquals(0, $rates->getRateForCountry('NL', Rates::RATE_STANDARD, '1097'));
$this->assertEquals(0, $rates->getRateForCountry('NL', Rates::RATE_STANDARD, '1099'));
$this->assertEquals(0, $rates->getRateForCountry('NL', Rates::RATE_STANDARD, '1098'));
}

public function testGetRateForCountryOnDate()
{
$client = $this->getRatesClientMock();
Expand Down