Skip to content

Commit

Permalink
Merge pull request #7 from aheenam/refactor/type-hints
Browse files Browse the repository at this point in the history
♻️ Change from doc block to type-hints
  • Loading branch information
rathesDot authored Sep 7, 2018
2 parents 8912012 + 59c3acd commit 566a2d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 82 deletions.
89 changes: 16 additions & 73 deletions src/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace Aheenam\Translatable;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;

trait Translatable
{
/**
* @return \Illuminate\Support\Collection
*/
public function allTranslations()
public function allTranslations(): Collection
{
$translations = collect([]);

Expand All @@ -35,7 +33,7 @@ public function allTranslations()
}

/**
* @param $key
* @param string $key
*
* @return mixed
*/
Expand All @@ -48,12 +46,7 @@ public function getAttributeValue($key)
return $this->getTranslation($key, App::getLocale());
}

/**
* returns all attributes that are translatable.
*
* @return array
*/
public function getTranslatableAttributes()
public function getTranslatableAttributes(): array
{
/* @noinspection PhpUndefinedFieldInspection */
return (property_exists(static::class, 'translatable') && is_array($this->translatable))
Expand All @@ -62,11 +55,9 @@ public function getTranslatableAttributes()
}

/**
* @param $locale
*
* @return Translatable
*/
public function in($locale)
public function in(string $locale)
{
$translatedModel = new self();

Expand All @@ -85,21 +76,14 @@ public function in($locale)
return $translatedModel;
}

/**
* @param $locale
*/
public function removeTranslationIn($locale)
public function removeTranslationIn(string $locale)
{
$this->translations()
->where('locale', $locale)
->delete();
}

/**
* @param $locale
* @param $attribute
*/
public function removeTranslation($locale, $attribute)
public function removeTranslation(string $locale, string $attribute)
{
$this->translations()
->where('locale', $locale)
Expand All @@ -118,26 +102,17 @@ public function translations()
/**
* returns the translation of a key for a given key/locale pair.
*
* @param $key
* @param $locale
*
* @return mixed
*/
protected function getTranslation($key, $locale)
protected function getTranslation(string $key, string $locale)
{
return $this->translations()
->where('key', $key)
->where('locale', $locale)
->value('translation');
}

/**
* @param $locale
* @param $attribute
*
* @return bool
*/
protected function hasTranslation($locale, $attribute)
protected function hasTranslation(string $locale, string $attribute): bool
{
$translation = $this->translations()
->where('locale', $locale)
Expand All @@ -147,26 +122,12 @@ protected function hasTranslation($locale, $attribute)
return $translation !== null;
}

/**
* returns if given key is translatable.
*
* @param $key
*
* @return bool
*/
protected function isTranslatableAttribute($key)
protected function isTranslatableAttribute(string $key): bool
{
return in_array($key, $this->getTranslatableAttributes());
}

/**
* @param $locale
* @param $attribute
* @param $translation
*
* @return void
*/
protected function setTranslation($locale, $attribute, $translation)
protected function setTranslation(string $locale, string $attribute, string $translation): void
{
$this->translations()->create([
'key' => $attribute,
Expand All @@ -175,13 +136,7 @@ protected function setTranslation($locale, $attribute, $translation)
]);
}

/**
* @param $locale
* @param $translations
*
* @return void
*/
protected function setTranslationByArray($locale, $translations)
protected function setTranslationByArray(string $locale, array $translations): void
{
foreach ($translations as $attribute => $translation) {
if ($this->isTranslatableAttribute($attribute)) {
Expand All @@ -200,14 +155,9 @@ protected function setTranslationByArray($locale, $translations)
}

/**
* returns the translation of a key for a given key/locale pair.
*
* @param $key
* @param $locale
*
* @return mixed
*/
protected function translateAttribute($key, $locale)
protected function translateAttribute(string $key, string $locale)
{
if (! $this->isTranslatableAttribute($key) || config('app.fallback_locale') == $locale) {
return parent::getAttributeValue($key);
Expand All @@ -216,14 +166,7 @@ protected function translateAttribute($key, $locale)
return $this->getTranslation($key, $locale);
}

/**
* @param $locale
* @param $attribute
* @param $translation
*
* @return void
*/
protected function updateTranslation($locale, $attribute, $translation)
protected function updateTranslation(string $locale, string $attribute, string $translation): void
{
$this->translations()
->where('key', $attribute)
Expand All @@ -234,8 +177,8 @@ protected function updateTranslation($locale, $attribute, $translation)
}

/**
* @param $method
* @param $arguments
* @param string $method
* @param array $parameters
*
* @return mixed
*/
Expand Down
8 changes: 2 additions & 6 deletions src/TranslatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ class TranslatableServiceProvider extends ServiceProvider

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations/');
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
public function register(): void
{
}
}
5 changes: 2 additions & 3 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aheenam\Translatable;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Translation extends Model
{
Expand All @@ -15,10 +16,8 @@ class Translation extends Model

/**
* Get all of the owning translatable models.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function translatable()
public function translatable(): MorphTo
{
return $this->morphTo();
}
Expand Down

0 comments on commit 566a2d6

Please sign in to comment.