webhub / vat
kdeldycke's VAT rates wrapped in a PHP library
0.7.0
2019-03-26 08:56 UTC
Requires
- php: ^7.1
- nesbot/carbon: ^2.16
Requires (Dev)
- guzzlehttp/guzzle: ^6.3
- league/csv: ^9.2
- phpunit/phpunit: ^7.5
- zendframework/zend-code: ^3.3
README
This is a wrapper of kdeldycke/vat-rates.
🛠 Usage
Installation
composer require webhub/vat
Basic use
use Webhub\Vat\Rates; $rate = (new Rates)->in('NL')->current()->get(); $rate->rate(); // 0.21 $rate->currencyCode(); // 'EUR' $rate = (new Rates)->in('BE')->at('1990-01-01')->get(); $rate->rate(); // 0.12
Rates
A Rates
instance is a collection of rates.
Rates can be filtered:
->in(string $territory)
a territory likeDE
orNL
->at(string|Carbon $when)
a date like2018-01-05
, supports Carbon->current()
alias for->at(Carbon::now())
->type(string $type)
rate type, currently the database only contains standard rates.
When one rate remains, the ->get() : Rate
method retrieves it, otherwise it throws.
Obtain all rates through ->all() : array
.
Rate
A Rate
has:
->rate() : string
decimal fraction representation of the rate, e.g. '0.20' for 20%->rateType() : string
type of the rate, currentlystandard
.->(start|stop)Date() : Carbon
Carbon date instance of first valid day and subsequent first not-valid day.->currencyCode() : string
currency likeSEK
orEUR
->description() : ?string
optional description of the rate
Rates
proxies method calls to the underlying Rate
if it exists and is unique.
// with ->get() (new Rates)->in('DE')->current()->get()->rate(); // equals shorter: (new Rates)->in('DE')->current()->rate(); // non unique (new Rates)->in('FR')->rate(); // throws AmbiguousResultException (new Rates)->in('XX')->get(); // throws NoResultException
Rate array access
A Rate
implements ArrayAccess
, so when using with for example Laravel's Collection, this is perfectly possible:
collect((new Rates)->in('NL')->all()) ->sortBy('start_date') ->pluck('rate', 'start_date');
📝 Compiling a new dataset
Data is obtained from kdeldycke/vat-rates
and written to a PHP file data.php
that is included in Rates
.
composer install
composer run build // runs Generator::generate()