-
-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add methods getTranslationChanges() and wasTranslationChanged() #231
Changes from all commits
d50c17d
bec641d
8d5326d
5102892
777c337
17b0148
f2fbe38
845e8f1
0633a68
6986ffe
60d20de
8c635c2
6953156
7163a88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,9 +4,11 @@ | |||||
|
||||||
use Astrotomic\Translatable\Traits\Relationship; | ||||||
use Astrotomic\Translatable\Traits\Scopes; | ||||||
use Astrotomic\Translatable\Validation\RuleFactory; | ||||||
use Illuminate\Database\Eloquent\Collection; | ||||||
use Illuminate\Database\Eloquent\Model; | ||||||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||||||
use Illuminate\Support\Arr; | ||||||
use Illuminate\Support\Str; | ||||||
|
||||||
/** | ||||||
|
@@ -458,4 +460,69 @@ public function __isset($key) | |||||
{ | ||||||
return $this->isTranslationAttribute($key) || parent::__isset($key); | ||||||
} | ||||||
|
||||||
public function getTranslationChanges(): array | ||||||
{ | ||||||
$translationChanges = []; | ||||||
foreach ($this->translations as $translation) { | ||||||
$changes = $translation->getChanges(); | ||||||
if (! empty($changes)) { | ||||||
$translationChanges[$translation->locale] = $changes; | ||||||
} | ||||||
} | ||||||
|
||||||
return $translationChanges; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param string[]|string|null $attributes | ||||||
* @param string|null $locale | ||||||
*/ | ||||||
public function wasTranslationChanged($attributes = null, ?string $locale = null): bool | ||||||
{ | ||||||
$translationChanges = $this->getTranslationChanges(); | ||||||
if (empty($translationChanges)) { | ||||||
return false; | ||||||
} | ||||||
if (empty($attributes)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return true; | ||||||
} | ||||||
$attributes = is_array($attributes) ? $attributes : [$attributes]; | ||||||
if (config('translatable.rule_factory.format') === RuleFactory::FORMAT_KEY) { | ||||||
$attributes = array_map(function ($attribute) { | ||||||
return implode('.', array_reverse(explode(':', $attribute))); | ||||||
}, $attributes); | ||||||
} | ||||||
Comment on lines
+491
to
+495
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the user can provide attribute and locale I don't see a reason why we should do this "complex" string handling here. |
||||||
|
||||||
$attributesWithoutLocale = []; | ||||||
foreach ($attributes as $attribute) { | ||||||
if (Arr::get($translationChanges, $attribute)) { | ||||||
return true; | ||||||
} | ||||||
if (! Str::contains($attribute, '.')) { | ||||||
$attributesWithoutLocale[] = $attribute; | ||||||
} | ||||||
} | ||||||
if (! empty($locale)) { | ||||||
$localeChanges = Arr::get($translationChanges, $locale); | ||||||
if (empty($localeChanges)) { | ||||||
return false; | ||||||
} | ||||||
foreach ($attributesWithoutLocale as $attribute) { | ||||||
if (isset($localeChanges[$attribute])) { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
} else { | ||||||
foreach ($attributesWithoutLocale as $attribute) { | ||||||
foreach ($translationChanges as $localeChanges) { | ||||||
if (isset($localeChanges[$attribute])) { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are a few cases missing and therefore the check isn't complete: