/** * Return the rate as percent (0..100) for a $countryCode with $rateType * @throws \InvalidArgumentException if the provided 2-letter $countryCode has no such $rateType * @param string $countryCode * @param string $rateType * @return float Returns the rate as full percent (0..100), e. g. 13.5 */ protected static function getRateByCountryAndType($countryCode, $rateType) { $rate = null; $country = VATRates::getCountryRates($countryCode); switch ($rateType) { case VATRates::RATE_SUPER_REDUCED: $rate = $country['superReducedRate']; break; case VATRates::RATE_REDUCED: $reducedRate = $country['reducedRate']; if (is_array($reducedRate)) { $rate = $reducedRate[0]; } break; case VATRates::RATE_REDUCED2: $reducedRate = $country['reducedRate']; if (is_array($reducedRate) && count($reducedRate) > 1) { $rate = $reducedRate[1]; } break; case VATRates::RATE_PARKING: $rate = $country['parkingRate']; break; case VATRates::RATE_STANDARD: default: $rate = $country['standardRate']; break; } if ($rate === null) { throw new \InvalidArgumentException("Country '{$countryCode}' has no VAT rate for type '{$rateType}'."); } return $rate; }
<?php require 'EuropeVAT.php'; $results = []; /* * Working with taxes, adding and subtracting */ $results[] = EuropeVAT::addTax(100, 'DE'); $results[] = EuropeVAT::subtractTax(49, 'ES', VATRates::RATE_REDUCED); // 2-letter country code can be any case; it will be converted to uppercase $results[] = EuropeVAT::addTax(36.95, 'hu'); /** * Working with countries and their rates */ // gets all rates for a country $results[] = VATRates::getCountryRates('LU'); // more functions, if you want to populate a dropdown-list: // all standard rates as an array with this mapping: string countryCode => float rate $results[] = VATRates::getAllStandardRates(); // get everything $results[] = VATRates::getAll(); print_r($results);