Skip to content

Commit

Permalink
Merge pull request #112 from ARCANEDEV/patch-1
Browse files Browse the repository at this point in the history
Extracting the supported locales to a new dedicated class
  • Loading branch information
arcanedev-maroc authored Feb 6, 2018
2 parents a54affc + 9c8bf8c commit c6c52b8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/Entities/LocaleCollection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Arcanedev\Localization\Entities;

use Arcanedev\Support\Collection;
use Illuminate\Support\Collection;

/**
* Class LocaleCollection
Expand Down Expand Up @@ -62,13 +62,27 @@ public function first(callable $callback = null, $default = null)
/**
* Get supported locales collection.
*
* @return self
* @return \Arcanedev\Localization\Entities\SupportedLocaleCollection
*/
public function getSupported()
{
return $this->filter(function(Locale $locale) {
return in_array($locale->key(), $this->supported);
});
return new SupportedLocaleCollection(
$this->filter(function(Locale $locale) {
return in_array($locale->key(), $this->supported);
})
);
}

/**
* Transform the collection with only locale's native name.
*
* @return \Illuminate\Support\Collection
*/
public function toNative()
{
return $this->map(function (Locale $locale) {
return $locale->native();
})->toBase();
}

/**
Expand Down Expand Up @@ -97,7 +111,7 @@ public function loadFromConfig()
*/
public function loadFromArray(array $locales)
{
$this->reset();
$this->items = [];

foreach ($locales as $key => $locale) {
$this->put($key, Locale::make($key, $locale));
Expand Down
29 changes: 29 additions & 0 deletions src/Entities/SupportedLocaleCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Arcanedev\Localization\Entities;

use Illuminate\Support\Collection;

/**
* Class SupportedLocaleCollection
*
* @package Arcanedev\Localization\Entities
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class SupportedLocaleCollection extends Collection
{
/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Transform the collection with only locale's native name.
*
* @return \Illuminate\Support\Collection
*/
public function toNative()
{
return $this->map(function (Locale $locale) {
return $locale->native();
})->toBase();
}
}
40 changes: 39 additions & 1 deletion tests/Entities/LocaleCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ public function tearDown()
/** @test */
public function it_can_be_instantiated()
{
$this->assertInstanceOf(LocaleCollection::class, $this->locales);
$expectations = [
\Illuminate\Support\Collection::class,
\Arcanedev\Localization\Entities\LocaleCollection::class,
];

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

$this->assertTrue($this->locales->isEmpty());
$this->assertCount(0, $this->locales);
$this->assertSame(0, $this->locales->count());
Expand All @@ -73,12 +81,42 @@ public function it_can_get_supported_locales()

$supported = $this->locales->getSupported();

$expectations = [
\Illuminate\Support\Collection::class,
\Arcanedev\Localization\Entities\SupportedLocaleCollection::class,
];

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

$count = count($this->supportedLocales);
$this->assertFalse($supported->isEmpty());
$this->assertCount($count, $supported);
$this->assertSame($count, $supported->count());
}

/** @test */
public function it_can_transform_locales_to_native_names()
{
$this->locales
->loadFromArray(config('localization.locales', []))
->setSupportedKeys(config('localization.supported-locales', []));

foreach ($this->locales->toNative() as $key => $native) {
$this->assertTrue($this->locales->has($key), "Locale [$key] not found");
$this->assertSame($this->locales->get($key)->native(), $native);
}

$expected = [
'en' => 'English',
'es' => 'Español',
'fr' => 'Français',
];

$this->assertEquals($expected, $this->locales->getSupported()->toNative()->toArray());
}

/** @test */
public function it_can_load_locales_from_config()
{
Expand Down

0 comments on commit c6c52b8

Please sign in to comment.