Skip to content

Commit

Permalink
Merge pull request #11 from Skysplit/laravel-7
Browse files Browse the repository at this point in the history
Added support for laravel 7
  • Loading branch information
duxthefux authored Apr 23, 2020
2 parents 8d225c3 + c0b8f92 commit eb95282
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 122 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- '7.2'
- '7.4'

cache:
Expand All @@ -14,4 +13,4 @@ before_script:

script:
- composer lint
- phpunit
- vendor/bin/phpunit
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
]
},
"require": {
"php": ">=7.2.0",
"php": ">=7.4.0",
"ext-intl": "*",
"illuminate/support": "^6",
"illuminate/translation": "^6",
"illuminate/validation": "^6"
"illuminate/support": "^7",
"illuminate/translation": "^7",
"illuminate/validation": "^7"
},
"require-dev": {
"orchestra/testbench": "^3.3",
"phpunit/phpunit": "^8.0",
"orchestra/testbench": "5.1.0",
"phpunit/phpunit": "^9.0",
"friendsofphp/php-cs-fixer": "^2.16"
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@
'string' => 'The {attribute} must be a string.',
'starts_with' => 'The {attribute} must start with one of the following: {values}',
'timezone' => 'The {attribute} must be a valid zone.',
'uploaded' => 'The {attribute} failed to upload.',
'unique' => 'The {attribute} has already been taken.',
'url' => 'The {attribute} format is invalid.',
'uuid' => 'The :attribute must be a valid UUID.',

/*
|--------------------------------------------------------------------------
Expand Down
14 changes: 5 additions & 9 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function register()
/**
* Bootstrap the application events.
*/
public function boot()
public function boot(): void
{
$this->publishConfig();
$this->publishLangFiles();
Expand All @@ -46,7 +46,7 @@ public function boot()
/**
* * Publish config files using php artisan vendor:publish.
*/
protected function publishConfig()
protected function publishConfig(): void
{
$configFile = 'translator.php';

Expand All @@ -60,7 +60,7 @@ protected function publishConfig()
/**
* Publish lang files using php artisan vendor:publish.
*/
protected function publishLangFiles()
protected function publishLangFiles(): void
{
$langPath = $this->getLangPath();

Expand All @@ -86,20 +86,16 @@ protected function publishLangFiles()

/**
* Get config files directory.
*
* @return string
*/
protected function getConfigPath()
protected function getConfigPath(): string
{
return __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'config' . \DIRECTORY_SEPARATOR;
}

/**
* Get language files directory.
*
* @return string
*/
protected function getLangPath()
protected function getLangPath(): string
{
return __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'resources' . \DIRECTORY_SEPARATOR . 'lang' . \DIRECTORY_SEPARATOR;
}
Expand Down
93 changes: 19 additions & 74 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,42 @@ class Translator extends NamespacedItemResolver implements TranslatorContract

/**
* The loader implementation.
*
* @varLoader
*/
protected $loader;
protected Loader $loader;

/**
* The default locale being used by the translator.
*
* @var string
*/
protected $locale;
protected ?string $locale = null;

/**
* The fallback locale used by the translator.
*
* @var string
*/
protected $fallback;
protected ?string $fallback = null;

/**
* Locale region used by translator.
*
* @var string
*/
protected $region;
protected ?string $region = null;

/**
* The array of loaded translation groups.
*
* @var array
*/
protected $loaded = [];
protected array $loaded = [];

/**
* Create a new translator instance.
*
* @param string $locale
*/
public function __construct(Loader $loader, $locale)
public function __construct(Loader $loader, string $locale)
{
$this->loader = $loader;
$this->setLocale($locale);
}

/**
* Determine if a translation exists.
*
* @param string $key
* @param null|string $locale
* @param bool $fallback
*
* @return bool
*/
public function has($key, $locale = null)
public function has(string $key, ?string $locale = null): bool
{
return $this->getMessage($key, $locale) !== $key;
}
Expand Down Expand Up @@ -180,50 +162,39 @@ public function locale()
*
* @param string $fallback
*/
public function setFallback($fallback)
public function setFallback(?string $fallback)
{
$this->fallback = $fallback;
}

/**
* Get the fallback locale being used.
*
* @return string
*/
public function getFallback()
public function getFallback(): ?string
{
return $this->fallback;
}

/**
* Set locale region.
*
* @param string $region
*/
public function setRegion($region)
public function setRegion(?string $region)
{
$this->region = $region;
}

/**
* Get locale region.
*
* @return null|string
*/
public function getRegion()
public function getRegion(): ?string
{
return $this->region;
}

/**
* Get locale with region separated by hypen.
*
* @param null|string $locale
* @param null|string $region
*
* @return string
* Get locale with region separated by hyphen.
*/
public function getLocaleRegion($locale = null, $region = null)
public function getLocaleRegion(?string $locale = null, ?string $region = null): string
{
$locale = $locale ?: ($this->getLocale() ?: $this->getFallback());
$region = $region ?: $this->getRegion();
Expand Down Expand Up @@ -255,12 +226,8 @@ public function parseKey($key)

/**
* Load the specified language group.
*
* @param string $namespace
* @param string $group
* @param string $locale
*/
public function load($namespace, $group, $locale)
public function load(string $namespace, string $group, string $locale)
{
if ($this->isLoaded($namespace, $group, $locale)) {
return;
Expand All @@ -276,25 +243,18 @@ public function load($namespace, $group, $locale)

/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
*/
public function addNamespace($namespace, $hint)
public function addNamespace(string $namespace, string $hint)
{
$this->loader->addNamespace($namespace, $hint);
}

/**
* Get the translation for the given key.
*
* @param string $key
* @param null|string $locale
* @param bool $fallback
*
* @return null|array|string
*/
public function getMessage($key, $locale = null)
public function getMessage(string $key, ?string $locale = null)
{
[$namespace, $group, $item] = $this->parseKey($key);

Expand All @@ -320,14 +280,9 @@ public function getMessage($key, $locale = null)
/**
* Retrieve a language line out the loaded array.
*
* @param string $namespace
* @param string $group
* @param string $locale
* @param string $item
*
* @return null|array|string
*/
protected function getLine($namespace, $group, $locale, $item)
protected function getLine(string $namespace, string $group, string $locale, string $item)
{
$line = Arr::get($this->loaded[$namespace][$group][$locale], $item);

Expand All @@ -340,26 +295,16 @@ protected function getLine($namespace, $group, $locale, $item)

/**
* Get the array of locales to be checked.
*
* @param null|string $locale
*
* @return array
*/
protected function parseLocale($locale)
protected function parseLocale(?string $locale): array
{
return array_filter([$locale ?: $this->locale, $this->fallback]);
}

/**
* Determine if the given group has been loaded.
*
* @param string $namespace
* @param string $group
* @param string $locale
*
* @return bool
*/
protected function isLoaded($namespace, $group, $locale)
protected function isLoaded(string $namespace, string $group, string $locale): bool
{
return isset($this->loaded[$namespace][$group][$locale]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ValidationServiceProvider extends LaravelProvider
{
public function boot()
public function boot(): void
{
app('validator')->resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
return new Validator($translator, $data, $rules, $messages, $customAttributes);
Expand Down
5 changes: 1 addition & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
*/
class TestCase extends Orchestra\Testbench\TestCase
{
/**
* @var null|string
*/
protected $fixturesPath;
protected ?string $fixturesPath;

public function setUp(): void
{
Expand Down
24 changes: 12 additions & 12 deletions tests/TranslationDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp(): void

$this->loadMigrationsFrom([
'--database' => 'testing',
'--realpath' => realpath(__DIR__ . '/database/migrations'),
'--path' => realpath(__DIR__ . '/database/migrations'),
]);
}

Expand All @@ -33,17 +33,7 @@ public function getEnvironmentSetUp($app)
]);
}

/**
* Create test user.
*
* @return User
*/
public function makeUser()
{
return User::create(['name' => 'test']);
}

public function testDatabaseValidators()
public function testDatabaseValidators(): void
{
$this->makeUser();

Expand All @@ -66,4 +56,14 @@ public function testDatabaseValidators()
$this->assertEquals('The selected exists is invalid.', $errors->first('exists'));
$this->assertEquals('The unique has already been taken.', $errors->first('unique'));
}

/**
* Create test user.
*
* @return User
*/
private function makeUser()
{
return User::create(['name' => 'test']);
}
}
Loading

0 comments on commit eb95282

Please sign in to comment.