Skip to content

Commit

Permalink
Merge pull request #7 from ARCANEDEV/develop
Browse files Browse the repository at this point in the history
Adding Currencies Collection
  • Loading branch information
arcanedev-maroc authored Dec 10, 2016
2 parents df62c53 + b2e78d5 commit 6c59323
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/Entities/Currencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php namespace Arcanedev\GeoIP\Entities;

use Arcanedev\Support\Collection;

/**
* Class Currencies
*
* @package Arcanedev\GeoIP\Entities
* @author ARCANEDEV <[email protected]>
*/
class Currencies extends Collection
{
/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
*/
/**
* Make the currencies collection.
*
* @param array $items
*
* @return self
*/
public static function make($items = [])
{
return new static(
config('geoip.currencies.data', $items)
);
}
}
12 changes: 4 additions & 8 deletions src/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Arcanedev\GeoIP\Contracts\GeoIP as GeoIPContract;
use Arcanedev\GeoIP\Contracts\GeoIPCache;
use Arcanedev\GeoIP\Contracts\GeoIPDriver;
use Arcanedev\GeoIP\Entities\Currencies;
use Arcanedev\GeoIP\Support\GeoIPLogger;
use Arcanedev\GeoIP\Support\IpDetector;
use Arcanedev\GeoIP\Support\IpValidator;
Expand Down Expand Up @@ -38,9 +39,6 @@ class GeoIP implements GeoIPContract
/** @var string */
protected $remoteIp;

/** @var array */
protected $currencies;

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -122,11 +120,9 @@ public function setDefaultLocation(array $location)
*/
public function getCurrency($iso)
{
if ($this->currencies === null && $this->config('currencies.included', false)) {
$this->currencies = $this->config('currencies.data', []);
}

return Arr::get($this->currencies, $iso);
return (bool) $this->config('currencies.included', false)
? Currencies::make()->get($iso, $iso)
: $iso;
}

/* ------------------------------------------------------------------------------------------------
Expand Down
75 changes: 75 additions & 0 deletions tests/Entities/CurrenciesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php namespace Arcanedev\GeoIP\Tests\Entities;

use Arcanedev\GeoIP\Tests\TestCase;

/**
* Class CurrenciesTest
*
* @package Arcanedev\GeoIP\Tests\Entities
* @author ARCANEDEV <[email protected]>
*/
class CurrenciesTest extends TestCase
{
/* ------------------------------------------------------------------------------------------------
| Properties
| ------------------------------------------------------------------------------------------------
*/
/** @var \Arcanedev\GeoIP\Entities\Currencies */
protected $currencies;

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
public function setUp()
{
parent::setUp();

$this->currencies = \Arcanedev\GeoIP\Entities\Currencies::make();
}

public function tearDown()
{
unset($this->currencies);

parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
| Test Functions
| ------------------------------------------------------------------------------------------------
*/
/** @test */
public function it_can_be_instantiated()
{
$expectations = [
\Illuminate\Support\Collection::class,
\Arcanedev\Support\Collection::class,
\Arcanedev\GeoIP\Entities\Currencies::class,
];

foreach ($expectations as $expected) {
$this->assertInstanceOf($expected, $this->currencies);
}

$this->assertFalse($this->currencies->isEmpty());
$this->assertCount(247, $this->currencies);
}

/** @test */
public function it_can_get_continent()
{
$expectations = [
'FR' => 'EUR',
'JP' => 'JPY',
'MA' => 'MAD',
];

foreach ($expectations as $key => $expected) {
$this->assertSame($expected, $this->currencies->get($key));
}

$this->assertNull($this->currencies->get('ZZ'));
$this->assertSame('Unknown', $this->currencies->get('ZZ', 'Unknown'));
}
}

0 comments on commit 6c59323

Please sign in to comment.