From 2c9c57f8faa13b2a13b1af11d33dd14f994e2b80 Mon Sep 17 00:00:00 2001 From: ElMatella Date: Fri, 31 May 2019 12:16:28 +0200 Subject: [PATCH 1/3] normalize foreign key --- src/Translatable/Translatable.php | 68 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index c7e3578..ef6a99a 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -2,8 +2,10 @@ namespace Dimsav\Translatable; +use Illuminate\Database\Eloquent\MassAssignmentException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -21,7 +23,7 @@ trait Translatable * @param string|null $locale * @param bool $withFallback * - * @return \Illuminate\Database\Eloquent\Model|null + * @return Model|null */ public function translate($locale = null, $withFallback = false) { @@ -33,7 +35,7 @@ public function translate($locale = null, $withFallback = false) * * @param string $locale * - * @return \Illuminate\Database\Eloquent\Model|null + * @return Model|null */ public function translateOrDefault($locale = null) { @@ -45,7 +47,7 @@ public function translateOrDefault($locale = null) * * @param string $locale * - * @return \Illuminate\Database\Eloquent\Model|null + * @return Model|null */ public function translateOrNew($locale = null) { @@ -56,7 +58,7 @@ public function translateOrNew($locale = null) * @param string|null $locale * @param bool $withFallback * - * @return \Illuminate\Database\Eloquent\Model|null + * @return Model|null */ public function getTranslation($locale = null, $withFallback = null) { @@ -134,14 +136,10 @@ public function getTranslationModelNamespace() public function getRelationKey() { if ($this->translationForeignKey) { - $key = $this->translationForeignKey; - } elseif ($this->primaryKey !== 'id') { - $key = $this->primaryKey; - } else { - $key = $this->getForeignKey(); + return $this->translationForeignKey; } - return $key; + return $this->getForeignKey(); } /** @@ -153,7 +151,7 @@ public function getLocaleKey() } /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @return HasMany */ public function translations() { @@ -278,7 +276,7 @@ public function save(array $options = []) /** * @param string $locale * - * @return \Illuminate\Database\Eloquent\Model + * @return Model */ protected function getTranslationOrNew($locale = null) { @@ -294,7 +292,7 @@ protected function getTranslationOrNew($locale = null) /** * @param array $attributes * - * @throws \Illuminate\Database\Eloquent\MassAssignmentException + * @throws MassAssignmentException * @return $this */ public function fill(array $attributes) @@ -392,8 +390,8 @@ public function isTranslationAttribute($key) /** * @param string $key * - * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException * @return bool + * @throws LocalesNotDefinedException */ protected function isKeyALocale($key) { @@ -403,8 +401,8 @@ protected function isKeyALocale($key) } /** - * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException * @return array + * @throws LocalesNotDefinedException */ protected function getLocales() { @@ -466,7 +464,7 @@ protected function saveTranslations() /** * @param array * - * @return \Illuminate\Database\Eloquent\Model + * @return Model */ public function replicateWithTranslations(array $except = null) { @@ -482,7 +480,7 @@ public function replicateWithTranslations(array $except = null) } /** - * @param \Illuminate\Database\Eloquent\Model $translation + * @param Model $translation * * @return bool */ @@ -497,7 +495,7 @@ protected function isTranslationDirty(Model $translation) /** * @param string $locale * - * @return \Illuminate\Database\Eloquent\Model + * @return Model */ public function getNewTranslation($locale) { @@ -520,10 +518,10 @@ public function __isset($key) } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $locale * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeTranslatedIn(Builder $query, $locale = null) { @@ -535,10 +533,10 @@ public function scopeTranslatedIn(Builder $query, $locale = null) } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $locale * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeNotTranslatedIn(Builder $query, $locale = null) { @@ -550,9 +548,9 @@ public function scopeNotTranslatedIn(Builder $query, $locale = null) } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeTranslated(Builder $query) { @@ -568,7 +566,7 @@ public function scopeTranslated(Builder $query) * 'name' => 'Griechenland' // The translated name * ]. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $translationField */ public function scopeListsTranslations(Builder $query, $translationField) @@ -622,12 +620,12 @@ public function scopeWithTranslation(Builder $query) /** * This scope filters results by checking the translation fields. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $key * @param string $value * @param string $locale * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) { @@ -642,12 +640,12 @@ public function scopeWhereTranslation(Builder $query, $key, $value, $locale = nu /** * This scope filters results by checking the translation fields. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $key * @param string $value * @param string $locale * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) { @@ -662,12 +660,12 @@ public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = /** * This scope filters results by checking the translation fields. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $key * @param string $value * @param string $locale * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) { @@ -682,12 +680,12 @@ public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale /** * This scope filters results by checking the translation fields. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $key * @param string $value * @param string $locale * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) { @@ -702,11 +700,11 @@ public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $local /** * This scope sorts results by the given translation field. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param Builder $query * @param string $key * @param string $sortmethod * - * @return \Illuminate\Database\Eloquent\Builder|static + * @return Builder|static */ public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') { From 56fd95d72fa9cecfa477d422c99cd5ec458f17a0 Mon Sep 17 00:00:00 2001 From: Gummibeer Date: Fri, 31 May 2019 12:18:37 +0200 Subject: [PATCH 2/3] revert auto phpstorm changes --- src/Translatable/Translatable.php | 60 +++++++++++++++---------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index ef6a99a..be8930f 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -2,10 +2,8 @@ namespace Dimsav\Translatable; -use Illuminate\Database\Eloquent\MassAssignmentException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -23,7 +21,7 @@ trait Translatable * @param string|null $locale * @param bool $withFallback * - * @return Model|null + * @return \Illuminate\Database\Eloquent\Model|null */ public function translate($locale = null, $withFallback = false) { @@ -35,7 +33,7 @@ public function translate($locale = null, $withFallback = false) * * @param string $locale * - * @return Model|null + * @return \Illuminate\Database\Eloquent\Model|null */ public function translateOrDefault($locale = null) { @@ -47,7 +45,7 @@ public function translateOrDefault($locale = null) * * @param string $locale * - * @return Model|null + * @return \Illuminate\Database\Eloquent\Model|null */ public function translateOrNew($locale = null) { @@ -58,7 +56,7 @@ public function translateOrNew($locale = null) * @param string|null $locale * @param bool $withFallback * - * @return Model|null + * @return \Illuminate\Database\Eloquent\Model|null */ public function getTranslation($locale = null, $withFallback = null) { @@ -151,7 +149,7 @@ public function getLocaleKey() } /** - * @return HasMany + * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function translations() { @@ -276,7 +274,7 @@ public function save(array $options = []) /** * @param string $locale * - * @return Model + * @return \Illuminate\Database\Eloquent\Model */ protected function getTranslationOrNew($locale = null) { @@ -292,7 +290,7 @@ protected function getTranslationOrNew($locale = null) /** * @param array $attributes * - * @throws MassAssignmentException + * @throws \Illuminate\Database\Eloquent\MassAssignmentException * @return $this */ public function fill(array $attributes) @@ -390,8 +388,8 @@ public function isTranslationAttribute($key) /** * @param string $key * + * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException * @return bool - * @throws LocalesNotDefinedException */ protected function isKeyALocale($key) { @@ -401,8 +399,8 @@ protected function isKeyALocale($key) } /** + * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException * @return array - * @throws LocalesNotDefinedException */ protected function getLocales() { @@ -464,7 +462,7 @@ protected function saveTranslations() /** * @param array * - * @return Model + * @return \Illuminate\Database\Eloquent\Model */ public function replicateWithTranslations(array $except = null) { @@ -480,7 +478,7 @@ public function replicateWithTranslations(array $except = null) } /** - * @param Model $translation + * @param \Illuminate\Database\Eloquent\Model $translation * * @return bool */ @@ -495,7 +493,7 @@ protected function isTranslationDirty(Model $translation) /** * @param string $locale * - * @return Model + * @return \Illuminate\Database\Eloquent\Model */ public function getNewTranslation($locale) { @@ -518,10 +516,10 @@ public function __isset($key) } /** - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $locale * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeTranslatedIn(Builder $query, $locale = null) { @@ -533,10 +531,10 @@ public function scopeTranslatedIn(Builder $query, $locale = null) } /** - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $locale * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeNotTranslatedIn(Builder $query, $locale = null) { @@ -548,9 +546,9 @@ public function scopeNotTranslatedIn(Builder $query, $locale = null) } /** - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeTranslated(Builder $query) { @@ -566,7 +564,7 @@ public function scopeTranslated(Builder $query) * 'name' => 'Griechenland' // The translated name * ]. * - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $translationField */ public function scopeListsTranslations(Builder $query, $translationField) @@ -620,12 +618,12 @@ public function scopeWithTranslation(Builder $query) /** * This scope filters results by checking the translation fields. * - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $key * @param string $value * @param string $locale * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) { @@ -640,12 +638,12 @@ public function scopeWhereTranslation(Builder $query, $key, $value, $locale = nu /** * This scope filters results by checking the translation fields. * - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $key * @param string $value * @param string $locale * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) { @@ -660,12 +658,12 @@ public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = /** * This scope filters results by checking the translation fields. * - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $key * @param string $value * @param string $locale * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) { @@ -680,12 +678,12 @@ public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale /** * This scope filters results by checking the translation fields. * - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $key * @param string $value * @param string $locale * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) { @@ -700,11 +698,11 @@ public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $local /** * This scope sorts results by the given translation field. * - * @param Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $key * @param string $sortmethod * - * @return Builder|static + * @return \Illuminate\Database\Eloquent\Builder|static */ public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') { From e70bbeaa0581fa29bf778c73209d45e7a37a2ada Mon Sep 17 00:00:00 2001 From: Gummibeer Date: Fri, 31 May 2019 12:19:43 +0200 Subject: [PATCH 3/3] fix php cs --- src/Translatable/Translatable.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index be8930f..f00e880 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -200,7 +200,7 @@ private function getAttributeOrFallback($locale, $attribute) */ public function getAttribute($key) { - list($attribute, $locale) = $this->getAttributeAndLocale($key); + [$attribute, $locale] = $this->getAttributeAndLocale($key); if ($this->isTranslationAttribute($attribute)) { if ($this->getTranslation($locale) === null) { @@ -230,7 +230,7 @@ public function getAttribute($key) */ public function setAttribute($key, $value) { - list($attribute, $locale) = $this->getAttributeAndLocale($key); + [$attribute, $locale] = $this->getAttributeAndLocale($key); if ($this->isTranslationAttribute($attribute)) { $this->getTranslationOrNew($locale)->$attribute = $value; @@ -300,7 +300,7 @@ public function fill(array $attributes) $this->getTranslationOrNew($key)->fill($values); unset($attributes[$key]); } else { - list($attribute, $locale) = $this->getAttributeAndLocale($key); + [$attribute, $locale] = $this->getAttributeAndLocale($key); if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) { $this->getTranslationOrNew($locale)->fill([$attribute => $values]); unset($attributes[$key]);