Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Commit

Permalink
first usable version
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Apr 9, 2016
1 parent 1fe3cd3 commit 69171d2
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
composer.lock
vendor
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Danny van Kooten <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Laravel VAT Rates
================

Laravel VAT Rates is a small & lightweight Laravel library to grab VAT rates for any EU country.

This uses jsonvat.com to obtain its data. Full details can be seen [here](https://github.com/adamcooke/vat-rates).

## Installation

Either [PHP](https://php.net) 5.5+ or [HHVM](http://hhvm.com) 3.6+ are required.

To get the latest version of Laravel VAT Rates, simply require the project using [Composer](https://getcomposer.org):

```bash
$ composer require dannyvankooten/vat-rates
```

Once Laravel VAT Rates is installed, you need to register the service provider. Open up `config/app.php` and add the following to the `providers` key.

* `'DvK\VatRates\VatRatesServiceProvider'`

You can register the VatRates facade in the `aliases` key of your `config/app.php` file if you like.

* `'VatRates' => 'DvK\VatRates\Facades\VatRates'`

## Usage

If you registered the facade then using an instance of the class is as easy as this.

```php
use DvK\VatRates\Facades\VatRates;

VatRates::country( 'NL' ); // 21
VatRates::country( 'NL', 'reduced' ); // 6
```

If you'd prefer to use dependency injection, you can easily inject the class like this.


```php
use DvK\VatRates\VatRates;

class Foo
{
protected $rates;

public function __construct(VatRates $rates)
{
$this->rates = $rates;
}

public function bar()
{
return $this->rates->country('NL');
}
}
```

By default, VAT rates are cached for 24 hours using the default cache driver.

## License

Laravel VAT Rates is licensed under [The MIT License (MIT)](LICENSE).
114 changes: 98 additions & 16 deletions src/VatRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,135 @@

namespace DvK\VatRates;

class VatRates {
use Exception;
use Illuminate\Contracts\Cache\Repository as Cache;

class VatRates
{

/**
* @const string
*/
const URL = 'https://jsonvat.com/';

/**
* VatValidator constructor.
* @var array
*/
private $map = array();

/**
* @var Cache
*/
public function __construct( ) {
private $cache;

/**
* VatValidator constructor.
*
* @param Cache $cache
*/
public function __construct(Cache $cache = null)
{
$this->cache = $cache;
$this->map = $this->load();
}

private function fetch() {
$response = file_get_contents( self::URL );
$data = json_decode( $response );
var_dump( $data );
private function load()
{
// load from cache
if ($this->cache) {
$map = $this->cache->get('vat-rates');
}

// fetch from jsonvat.com
if (empty($map)) {
$map = $this->fetch();

// store in cache
if ($this->cache) {
$this->cache->put('vat-rates', $map, 86400);
}
}

return $map;
}

public function all() {
/**
* @return array
*
* @throws Exception
*/
private function fetch()
{
$url = self::URL;

// fetch data
$response = file_get_contents($url);
if( empty( $response ) ) {
throw new Exception( "Error fetching rates from {$url}.");
}
$data = json_decode($response);

// build map with country codes => rates
$map = array();
foreach ($data->rates as $rate) {
$map[$rate->country_code] = $rate->periods[0]->rates;
}

return $map;
}

public function country( $country ) {
$country = $this->getCountryCode( $country );
/**
* @return array
*/
public function all()
{
return $this->map;
}

/**
* @param string $country
* @param string $rate
*
* @return double
*
* @throws Exception
*/
public function country($country, $rate = 'standard')
{
$country = strtoupper($country);
$country = $this->getCountryCode($country);

if (!isset($this->map[$country])) {
throw new Exception('Invalid country code.');
}

if (!isset($this->map[$country]->$rate)) {
throw new Exception('Invalid rate.');
}

return $this->map[$country]->$rate;
}

/**
* Get normalized country code
*
* Fixes ISO-3166-1-alpha2 exceptions
*
* @param string $country
* @return string
*/
private function getCountryCode( $country ) {

// # Fix ISO-3166-1-alpha2 exceptions
if( $country == 'UK' ) {
private function getCountryCode($country)
{
if ($country == 'UK') {
$country = 'GB';
}

if( $country == 'EL' ) {
if ($country == 'EL') {
$country = 'GR';
}

return $country;
}



}
5 changes: 4 additions & 1 deletion src/VatRatesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;


class VatRatesServiceProvider extends ServiceProvider
{
/**
Expand All @@ -25,7 +26,9 @@ public function boot()
public function register()
{
$this->app->singleton('vatrates', function (Container $app) {
return new VatRates();
$defaultCacheDriver = $app['cache']->getDefaultDriver();
$cacheDriver = $app['cache']->driver( $defaultCacheDriver );
return new VatRates( $cacheDriver );
});
}

Expand Down

0 comments on commit 69171d2

Please sign in to comment.