Skip to content

Commit

Permalink
Add translate or new method + fix PSR2
Browse files Browse the repository at this point in the history
  • Loading branch information
acasar committed May 15, 2017
1 parent 8d904d6 commit 4eb4ecc
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions src/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function create(array $attributes = [], $translations = [])
{
$model = new static($attributes);

if($model->save() && is_array($translations)) {
if ($model->save() && is_array($translations)) {
$model->saveTranslations($translations);
}

Expand All @@ -59,7 +59,7 @@ public static function createInLocale($locale, array $attributes = [], $translat
{
$model = (new static($attributes))->setLocale($locale);

if($model->save() && is_array($translations)) {
if ($model->save() && is_array($translations)) {
$model->saveTranslations($translations);
}

Expand All @@ -77,7 +77,7 @@ public static function forceCreate(array $attributes, $translations = [])
{
$model = new static;

return static::unguarded(function () use ($model, $attributes, $translations) {
return static::unguarded(function () use ($model, $attributes, $translations){
return $model->create($attributes, $translations);
});
}
Expand All @@ -93,20 +93,20 @@ public static function forceCreateInLocale($locale, array $attributes, $translat
{
$model = new static;

return static::unguarded(function () use ($locale, $model, $attributes, $translations) {
return static::unguarded(function () use ($locale, $model, $attributes, $translations){
return $model->createInLocale($locale, $attributes, $translations);
});
}

/**
/**
* Reload a fresh model instance from the database.
*
* @param array|string $with
* @param array|string $with
* @return static|null
*/
public function fresh($with = [])
{
if (! $this->exists) {
if (!$this->exists) {
return;
}

Expand All @@ -128,7 +128,7 @@ public function saveTranslations(array $translations)
$success = true;
$fresh = parent::fresh();

foreach($translations as $locale => $attributes) {
foreach ($translations as $locale => $attributes) {
$model = clone $fresh;
$model->setLocale($locale);
$model->fill($attributes);
Expand All @@ -145,7 +145,7 @@ public function saveTranslations(array $translations)
*/
public function forceSaveTranslations(array $translations)
{
return static::unguarded(function () use ($translations) {
return static::unguarded(function () use ($translations){
return $this->saveTranslations($translations);
});
}
Expand All @@ -169,7 +169,7 @@ public function saveTranslation($locale, array $attributes)
*/
public function forceSaveTranslation($locale, array $attributes)
{
return static::unguarded(function () use ($locale, $attributes) {
return static::unguarded(function () use ($locale, $attributes){
return $this->saveTranslation($locale, $attributes);
});
}
Expand All @@ -181,7 +181,7 @@ public function forceSaveTranslation($locale, array $attributes)
*/
public function fill(array $attributes)
{
if(!isset(static::$i18nAttributes[$this->getTable()])) {
if (!isset(static::$i18nAttributes[$this->getTable()])) {
$this->initTranslatableAttributes();
}

Expand All @@ -195,7 +195,7 @@ protected function initTranslatableAttributes()
{
if (property_exists($this, 'translatable')) {
$attributes = $this->translatable;
} else {
}else {
$attributes = $this->getTranslatableAttributesFromSchema();
}

Expand All @@ -213,7 +213,7 @@ protected function getTranslatableAttributesFromSchema()
return [];
}

if($columns = TranslatableConfig::cacheGet($this->getI18nTable())) {
if ($columns = TranslatableConfig::cacheGet($this->getI18nTable())) {
return $columns;
}

Expand All @@ -235,13 +235,30 @@ public function translate($locale)
{
$found = $this->translations->where($this->getLocaleKey(), $locale)->first();

if(!$found && $this->shouldFallback($locale)) {
if (!$found && $this->shouldFallback($locale)) {
return $this->translate($this->getFallbackLocale());
}

return $found;
}

/**
* Get a collection of translated attributes in provided locale or create new one.
*
* @param $locale
* @return \Laraplus\Data\TranslationModel
*/
public function translateOrNew($locale)
{

if (is_null($instance = $this->translate($locale))) {
return $this->newModelInstance();
}

return $instance;

}

/**
* Translations relationship.
*
Expand Down Expand Up @@ -317,11 +334,11 @@ public function setLocale($locale)
*/
public function getLocale()
{
if($this->overrideLocale) {
if ($this->overrideLocale) {
return $this->overrideLocale;
}

if(property_exists($this, 'locale')) {
if (property_exists($this, 'locale')) {
return $this->locale;
}

Expand All @@ -348,11 +365,11 @@ public function setFallbackLocale($locale)
*/
public function getFallbackLocale()
{
if($this->overrideFallbackLocale) {
if ($this->overrideFallbackLocale) {
return $this->overrideFallbackLocale;
}

if(property_exists($this, 'fallbackLocale')) {
if (property_exists($this, 'fallbackLocale')) {
return $this->fallbackLocale;
}

Expand All @@ -379,11 +396,11 @@ public function setOnlyTranslated($onlyTranslated)
*/
public function getOnlyTranslated()
{
if(!is_null($this->overrideOnlyTranslated)) {
if (!is_null($this->overrideOnlyTranslated)) {
return $this->overrideOnlyTranslated;
}

if(property_exists($this, 'onlyTranslated')) {
if (property_exists($this, 'onlyTranslated')) {
return $this->onlyTranslated;
}

Expand All @@ -410,11 +427,11 @@ public function setWithFallback($withFallback)
*/
public function getWithFallback()
{
if(!is_null($this->overrideWithFallback)) {
if (!is_null($this->overrideWithFallback)) {
return $this->overrideWithFallback;
}

if(property_exists($this, 'withFallback')) {
if (property_exists($this, 'withFallback')) {
return $this->withFallback;
}

Expand Down Expand Up @@ -449,7 +466,7 @@ public function getTranslationTableSuffix()
*/
public function shouldFallback($locale = null)
{
if(!$this->getWithFallback() || !$this->getFallbackLocale()) {
if (!$this->getWithFallback() || !$this->getFallbackLocale()) {
return false;
}

Expand All @@ -461,7 +478,7 @@ public function shouldFallback($locale = null)
/**
* Create a new Eloquent query builder for the model.
*
* @param \Illuminate\Database\Query\Builder $query
* @param \Illuminate\Database\Query\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newEloquentBuilder($query)
Expand Down Expand Up @@ -494,12 +511,12 @@ public function getDirty()
{
$dirty = parent::getDirty();

if(! $this->localeChanged) {
if (!$this->localeChanged) {
return $dirty;
}

foreach ($this->translatableAttributes() as $key) {
if(isset($this->attributes[$key])) {
if (isset($this->attributes[$key])) {
$dirty[$key] = $this->attributes[$key];
}
}
Expand Down

0 comments on commit 4eb4ecc

Please sign in to comment.