Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Applied code style fix with php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsav committed Mar 6, 2015
1 parent 65e4904 commit 9064c59
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 263 deletions.
12 changes: 12 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$finder = Symfony\CS\Finder\DefaultFinder::create()
->exclude('coverage')
->in(__DIR__)
;

return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)
->fixers(['short_array_syntax'])
->finder($finder)
;
4 changes: 3 additions & 1 deletion src/Translatable/Exception/LocalesNotDefinedException.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php namespace Dimsav\Translatable\Exception;

class LocalesNotDefinedException extends \Exception {}
class LocalesNotDefinedException extends \Exception
{
}
142 changes: 59 additions & 83 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Database\Eloquent\Model;

trait Translatable {

trait Translatable
{
/*
* Alias for getTranslation()
*/
Expand All @@ -34,8 +34,9 @@ public function translateOrNew($locale)
}

/**
* @param null $locale
* @param null $locale
* @param bool|null $withFallback
*
* @return Model|null
*/
public function getTranslation($locale = null, $withFallback = null)
Expand All @@ -44,19 +45,14 @@ public function getTranslation($locale = null, $withFallback = null)

$withFallback = $withFallback === null ? $this->useFallback() : $withFallback;

if ($this->getTranslationByLocaleKey($locale))
{
if ($this->getTranslationByLocaleKey($locale)) {
$translation = $this->getTranslationByLocaleKey($locale);
}
elseif ($withFallback
} elseif ($withFallback
&& $this->getFallbackLocale()
&& $this->getTranslationByLocaleKey($this->getFallbackLocale())
)
{
) {
$translation = $this->getTranslationByLocaleKey($this->getFallbackLocale());
}
else
{
} else {
$translation = null;
}

Expand All @@ -67,10 +63,8 @@ public function hasTranslation($locale = null)
{
$locale = $locale ?: App::getLocale();

foreach ($this->translations as $translation)
{
if ($translation->getAttribute($this->getLocaleKey()) == $locale)
{
foreach ($this->translations as $translation) {
if ($translation->getAttribute($this->getLocaleKey()) == $locale) {
return true;
}
}
Expand All @@ -87,7 +81,7 @@ public function getTranslationModelNameDefault()
{
$config = App::make('config');

return get_class($this) . $config->get('translatable.translation_suffix', 'Translation');
return get_class($this).$config->get('translatable.translation_suffix', 'Translation');
}

public function getRelationKey()
Expand All @@ -98,6 +92,7 @@ public function getRelationKey()
public function getLocaleKey()
{
$config = App::make('config');

return $this->localeKey ?: $config->get('translatable.locale_key', 'locale');
}

Expand All @@ -108,88 +103,73 @@ public function translations()

public function getAttribute($key)
{
if ($this->isTranslationAttribute($key))
{
if ($this->getTranslation() === null)
{
return null;
if ($this->isTranslationAttribute($key)) {
if ($this->getTranslation() === null) {
return;
}

return $this->getTranslation()->$key;
}

return parent::getAttribute($key);
}

public function setAttribute($key, $value)
{
if (in_array($key, $this->translatedAttributes))
{
if (in_array($key, $this->translatedAttributes)) {
$this->getTranslationOrNew(App::getLocale())->$key = $value;
}
else
{
} else {
parent::setAttribute($key, $value);
}
}

public function save(array $options = [])
{
if ($this->exists)
{
if (count($this->getDirty()) > 0)
{
if ($this->exists) {
if (count($this->getDirty()) > 0) {
// If $this->exists and dirty, parent::save() has to return true. If not,
// an error has occurred. Therefore we shouldn't save the translations.
if (parent::save($options))
{
if (parent::save($options)) {
return $this->saveTranslations();
}

return false;
}
else
{
} else {
// If $this->exists and not dirty, parent::save() skips saving and returns
// false. So we have to save the translations
if($saved = $this->saveTranslations())
{
if ($saved = $this->saveTranslations()) {
$this->fireModelEvent('saved', false);
}

return $saved;
}
}
elseif (parent::save($options))
{
} elseif (parent::save($options)) {
// We save the translations only if the instance is saved in the database.
return $this->saveTranslations();
}

return false;
}

protected function getTranslationOrNew($locale)
{
if (($translation = $this->getTranslation($locale, false)) === null)
{
if (($translation = $this->getTranslation($locale, false)) === null) {
$translation = $this->getNewTranslation($locale);
}

return $translation;
}

public function fill(array $attributes)
{
$totallyGuarded = $this->totallyGuarded();

foreach ($attributes as $key => $values)
{
if ($this->isKeyALocale($key))
{
foreach ($values as $translationAttribute => $translationValue)
{
if ($this->alwaysFillable() or $this->isFillable($translationAttribute))
{
foreach ($attributes as $key => $values) {
if ($this->isKeyALocale($key)) {
foreach ($values as $translationAttribute => $translationValue) {
if ($this->alwaysFillable() or $this->isFillable($translationAttribute)) {
$this->getTranslationOrNew($key)->$translationAttribute = $translationValue;
}
elseif ($totallyGuarded)
{
} elseif ($totallyGuarded) {
throw new MassAssignmentException($key);
}
}
Expand All @@ -202,14 +182,13 @@ public function fill(array $attributes)

private function getTranslationByLocaleKey($key)
{
foreach ($this->translations as $translation)
{
if ($translation->getAttribute($this->getLocaleKey()) == $key)
{
foreach ($this->translations as $translation) {
if ($translation->getAttribute($this->getLocaleKey()) == $key) {
return $translation;
}
}
return null;

return;
}

private function getFallbackLocale()
Expand All @@ -222,10 +201,10 @@ private function getFallbackLocale()
*/
private function useFallback()
{
if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null)
{
if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) {
return $this->useTranslationFallback;
}

return App::make('config')->get('translatable.use_fallback');
}

Expand All @@ -237,6 +216,7 @@ protected function isTranslationAttribute($key)
protected function isKeyALocale($key)
{
$locales = $this->getLocales();

return in_array($key, $locales);
}

Expand All @@ -245,41 +225,42 @@ protected function getLocales()
$config = App::make('config');
$locales = (array) $config->get('translatable.locales', []);

if (empty($locales))
{
if (empty($locales)) {
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '.
' and that the locales configuration is defined.');
}

return $locales;
}

protected function saveTranslations()
{
$saved = true;
foreach ($this->translations as $translation)
{
if ($saved && $this->isTranslationDirty($translation))
{
foreach ($this->translations as $translation) {
if ($saved && $this->isTranslationDirty($translation)) {
$translation->setAttribute($this->getRelationKey(), $this->getKey());
$saved = $translation->save();
}
}

return $saved;
}

protected function isTranslationDirty(Model $translation)
{
$dirtyAttributes = $translation->getDirty();
unset($dirtyAttributes[$this->getLocaleKey()]);

return count($dirtyAttributes) > 0;
}

public function getNewTranslation($locale)
{
$modelName = $this->getTranslationModelName();
$translation = new $modelName;
$translation = new $modelName();
$translation->setAttribute($this->getLocaleKey(), $locale);
$this->translations->add($translation);

return $translation;
}

Expand All @@ -290,8 +271,7 @@ public function __isset($key)

public function scopeTranslatedIn(Builder $query, $locale)
{
return $query->whereHas('translations', function(Builder $q) use ($locale)
{
return $query->whereHas('translations', function (Builder $q) use ($locale) {
$q->where($this->getLocaleKey(), '=', $locale);
});
}
Expand All @@ -312,7 +292,7 @@ public function scopeTranslated(Builder $query)
* ]
*
* @param Builder $query
* @param string $translationField
* @param string $translationField
*/
public function scopeListsTranslations(Builder $query, $translationField)
{
Expand All @@ -323,15 +303,13 @@ public function scopeListsTranslations(Builder $query, $translationField)
->leftJoin($this->getTranslationsTable(), $this->getTranslationsTable().'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName())
->where('locale', App::getLocale())
;
if ($withFallback)
{
$query->orWhere(function(Builder $q){
$q->where($this->getTranslationsTable() .'.'. $this->getLocaleKey(), $this->getFallbackLocale())
->whereNotIn($this->getTranslationsTable() . '.' . $this->getRelationKey(), function(QueryBuilder $q)
{
$q->select($this->getTranslationsTable() . '.' . $this->getRelationKey())
if ($withFallback) {
$query->orWhere(function (Builder $q) {
$q->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->getFallbackLocale())
->whereNotIn($this->getTranslationsTable().'.'.$this->getRelationKey(), function (QueryBuilder $q) {
$q->select($this->getTranslationsTable().'.'.$this->getRelationKey())
->from($this->getTranslationsTable())
->where($this->getTranslationsTable() . '.' . $this->getLocaleKey(), App::getLocale());
->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), App::getLocale());
});
});
}
Expand All @@ -341,10 +319,8 @@ public function toArray()
{
$attributes = parent::toArray();

foreach($this->translatedAttributes AS $field)
{
if ($translations = $this->getTranslation())
{
foreach ($this->translatedAttributes as $field) {
if ($translations = $this->getTranslation()) {
$attributes[$field] = $translations->$field;
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/Translatable/TranslatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

use Illuminate\Support\ServiceProvider;

class TranslatableServiceProvider extends ServiceProvider {

class TranslatableServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__ .'/../config/translatable.php'=> config_path('translatable.php'),
__DIR__.'/../config/translatable.php' => config_path('translatable.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
Expand Down
1 change: 0 additions & 1 deletion src/config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,4 @@
*/
'always_fillable' => false,


];
Loading

0 comments on commit 9064c59

Please sign in to comment.