Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exceptions rates support #61

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
7 changes: 4 additions & 3 deletions src/Clients/IbericodeVatRatesClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@

private function parseResponse(string $response_body): array
{
$result = json_decode($response_body, false);
$result = json_decode($response_body, true);


$return = [];
foreach ($result->items as $country => $periods) {
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'] ?? []);

Check failure on line 45 in src/Clients/IbericodeVatRatesClient.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 1 space after comma in argument list; 2 found
}

$return[$country] = $periods;
Expand Down
6 changes: 4 additions & 2 deletions src/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ class Countries implements \Iterator, \ArrayAccess
'VI' => 'U.S. Virgin Islands',
'VN' => 'Vietnam',
'VU' => 'Vanuatu',
'WF' => 'Wallis & Futuna',
'WF' => 'Wallis And Futuna',
'EH' => 'Western Sahara',
'XI' => 'Northern Ireland',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xi is not a valid IsoCode cf https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes

It's only used for VAT but it's not an isocode.

'WS' => 'Samoa',
'YE' => 'Yemen',
'YT' => 'Mayotte',
Expand All @@ -285,7 +287,7 @@ public function hasCountryCode(string $code): bool
*/
public function getCountryCodesInEU(): array
{
return ['AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HU', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'];
return ['AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HU', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK', 'XI'];
}

/**
Expand Down
23 changes: 20 additions & 3 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,41 @@
{
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

Check failure on line 34 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

There must not be a space before the colon in a return type declaration
{
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){

Check failure on line 45 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 1 space(s) after IF keyword; 0 found

Check failure on line 45 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 1 space(s) after closing parenthesis; found 0
return null;
}

foreach ($this->exceptions as $exception){

Check failure on line 49 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 1 space(s) after closing parenthesis; found 0
if(preg_match('/^'.$exception['postcode'].'$/', $postcode)){

Check failure on line 50 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 1 space(s) after IF keyword; 0 found

Check failure on line 50 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected at least 1 space before "."; 0 found

Check failure on line 50 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected at least 1 space after "."; 0 found

Check failure on line 50 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected at least 1 space before "."; 0 found

Check failure on line 50 in src/Period.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected at least 1 space after "."; 0 found
return $exception[$level] ?? $exception[Rates::RATE_STANDARD];
}
}

return null;
}
}
11 changes: 7 additions & 4 deletions src/Rates.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private function load(): void
private function loadFromFile(): void
{
$contents = file_get_contents($this->storagePath);

$data = unserialize($contents, [
'allowed_classes' => [
Period::class,
Expand Down Expand Up @@ -131,25 +132,27 @@ 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
{
$activePeriod = $this->resolvePeriod($countryCode, $datetime);
return $activePeriod->getRate($level);
return $activePeriod->getRate($level, $postcode);
}
}
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Validator
'SI' => '\d{8}',
'SK' => '\d{10}',
'SM' => '\d{5}',
'XI' => '(\d{9}|\d{12}|(GD|HA)\d{3})',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/CountriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testIterator()
{
$countries = new Countries();

$this->assertCount(249, $countries);
$this->assertCount(250, $countries);
}

public function testArrayAccess()
Expand Down
21 changes: 20 additions & 1 deletion tests/RatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Ibericode\Vat\Exception;
use Ibericode\Vat\Period;
use Ibericode\Vat\Rates;
use PHPUnit\Framework\Error\Error;
use PHPUnit\Framework\TestCase;

class RatesTest extends TestCase
Expand Down Expand Up @@ -39,6 +38,17 @@ private function getRatesClientMock()
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 +63,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
Loading