Completed
Push — master ( 9e3da7...78b11e )
by Mike
03:45 queued 11s
created
src/Translatable.php 2 patches
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -8,84 +8,84 @@  discard block
 block discarded – undo
8 8
 
9 9
 trait Translatable
10 10
 {
11
-	public static $transtableFieldName = 'translatable';
11
+    public static $transtableFieldName = 'translatable';
12 12
 	
13
-	public $translatable;
14
-	public $model;
13
+    public $translatable;
14
+    public $model;
15 15
 	
16
-	protected $locale;
16
+    protected $locale;
17 17
 	
18
-	public static function bootTranslatable()
19
-	{
20
-		static::addGlobalScope(new TranslatableScope);
18
+    public static function bootTranslatable()
19
+    {
20
+        static::addGlobalScope(new TranslatableScope);
21 21
 		
22
-		static::saving(function ($model) {
23
-			$model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray();
24
-			$model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray();
25
-		});
22
+        static::saving(function ($model) {
23
+            $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray();
24
+            $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray();
25
+        });
26 26
 		
27
-		static::saved(function ($model) {
28
-			if ($model->translatable) {
29
-				(new self)->saveTranslation($model);
30
-			}
31
-		});
27
+        static::saved(function ($model) {
28
+            if ($model->translatable) {
29
+                (new self)->saveTranslation($model);
30
+            }
31
+        });
32 32
 		
33
-		static::deleted(function ($model) {
34
-			if ((new $model)->has('translations')) {
35
-				$model->translations()->delete();
36
-			}
37
-		});
38
-	}
33
+        static::deleted(function ($model) {
34
+            if ((new $model)->has('translations')) {
35
+                $model->translations()->delete();
36
+            }
37
+        });
38
+    }
39 39
 	
40
-	public function locale($value = null)
41
-	{
42
-		$this->locale = $value;
40
+    public function locale($value = null)
41
+    {
42
+        $this->locale = $value;
43 43
 		
44
-		return $this;
45
-	}
44
+        return $this;
45
+    }
46 46
 	
47
-	// Relationship
48
-	public function translations()
49
-	{
50
-		return $this->morphMany(Translation::class, 'translatable');
51
-	}
47
+    // Relationship
48
+    public function translations()
49
+    {
50
+        return $this->morphMany(Translation::class, 'translatable');
51
+    }
52 52
 	
53
-	// scope
54
-	public function scopeTranslated($query, ...$fields)
55
-	{
56
-		$this->locale = $this->locale ?? app()->getLocale();
53
+    // scope
54
+    public function scopeTranslated($query, ...$fields)
55
+    {
56
+        $this->locale = $this->locale ?? app()->getLocale();
57 57
 		
58
-		$fields = empty($fields) ? $this->getTranslatedFieldsForCurrentModel() : collect($fields);
58
+        $fields = empty($fields) ? $this->getTranslatedFieldsForCurrentModel() : collect($fields);
59 59
 		
60
-		if ($fields->isEmpty()) {
61
-			return $query;
62
-		}
60
+        if ($fields->isEmpty()) {
61
+            return $query;
62
+        }
63 63
 		
64
-		// Add select * if nothing has been selected yet
65
-		if ($query->getQuery()->columns === null) {
66
-			$query->select('*');
67
-		}
64
+        // Add select * if nothing has been selected yet
65
+        if ($query->getQuery()->columns === null) {
66
+            $query->select('*');
67
+        }
68 68
 		
69
-		// Build the sub select
70
-		$fields->each(function ($key) use ($query) {
71
-			$this->subSelectTranslation($query, $key);
72
-		});
69
+        // Build the sub select
70
+        $fields->each(function ($key) use ($query) {
71
+            $this->subSelectTranslation($query, $key);
72
+        });
73 73
 		
74
-		return $query;
75
-	}
74
+        return $query;
75
+    }
76 76
 	
77
-	protected function subSelectTranslation($query, $key): void
78
-	{
79
-		$query->addSelect([
80
-			$key => function ($query) use ($key) {
81
-				$query->select(Translation::getTableName() . '.value')
82
-					->from(Translation::getTableName())
83
-					->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this))
84
-					->where(Translation::getTableName() . '.locale', '=', $this->locale)
85
-					->where(Translation::getTableName() . '.key', '=', $key)
86
-					->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey));
87
-			}
88
-		]);
77
+    protected function subSelectTranslation($query, $key): void
78
+    {
79
+        $query->addSelect([
80
+            $key => function ($query) use ($key) {
81
+                $query->select(Translation::getTableName() . '.value')
82
+                    ->from(Translation::getTableName())
83
+                    ->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this))
84
+                    ->where(Translation::getTableName() . '.locale', '=', $this->locale)
85
+                    ->where(Translation::getTableName() . '.key', '=', $key)
86
+                    ->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey));
87
+            }
88
+        ]);
89 89
 
90 90
 //        $query->addSelect(\DB::raw('(
91 91
 //			SELECT      `' . Translation::getTableName() . '`.`value`
@@ -96,52 +96,52 @@  discard block
 block discarded – undo
96 96
 //			   AND      `' . Translation::getTableName() . '`.`translatable_id` = `' . $this->getTable() . '`.`' . $this->primaryKey . '`
97 97
 //			) as `' . $key . '`')
98 98
 //        );
99
-	}
99
+    }
100 100
 	
101
-	/**
102
-	 * Query the translations table for possible keys if none are provided.
103
-	 * @return Collection
104
-	 */
105
-	public function getTranslatedFieldsForCurrentModel(): Collection
106
-	{
107
-		$table = Translation::getTableName();
101
+    /**
102
+     * Query the translations table for possible keys if none are provided.
103
+     * @return Collection
104
+     */
105
+    public function getTranslatedFieldsForCurrentModel(): Collection
106
+    {
107
+        $table = Translation::getTableName();
108 108
 		
109
-		return DB::table($table)->select($table . '.key')
110
-			->where($table . '.translatable_type', \get_class($this))
111
-			->where($table . '.locale', $this->locale)
112
-			->groupBy($table . '.key')
113
-			->pluck('key');
114
-	}
109
+        return DB::table($table)->select($table . '.key')
110
+            ->where($table . '.translatable_type', \get_class($this))
111
+            ->where($table . '.locale', $this->locale)
112
+            ->groupBy($table . '.key')
113
+            ->pluck('key');
114
+    }
115 115
 	
116
-	public function saveTranslation($model): int
117
-	{
118
-		return (new Translation)->store([
119
-			'translatable'      => $model->translatable[static::$transtableFieldName],
120
-			'translatable_type' => \get_class($model),
121
-			'translatable_id'   => $model->id,
122
-		]);
123
-	}
116
+    public function saveTranslation($model): int
117
+    {
118
+        return (new Translation)->store([
119
+            'translatable'      => $model->translatable[static::$transtableFieldName],
120
+            'translatable_type' => \get_class($model),
121
+            'translatable_id'   => $model->id,
122
+        ]);
123
+    }
124 124
 	
125
-	protected function fullTextWildcards($term)
126
-	{
127
-		return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) {
128
-			return strlen($word) >= 3 ? '+' . $word . '*' : '';
129
-		})->implode(' ');
130
-	}
125
+    protected function fullTextWildcards($term)
126
+    {
127
+        return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) {
128
+            return strlen($word) >= 3 ? '+' . $word . '*' : '';
129
+        })->implode(' ');
130
+    }
131 131
 	
132
-	public function scopeSearchFullText($query, $term, $fields = [])
133
-	{
134
-		$alias = Str::random(8);
135
-		return $query->join(\DB::raw('translations as ' . $alias . ' ON ' . $alias . '.translatable_id = ' . $this->getTable() . '.' . $this->primaryKey))
136
-			->addSelect([
137
-				'relevance' => function ($query) use ($term, $alias) {
138
-					$query->selectRaw("MATCH (`' . $alias . '`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)")
139
-						->from('translations')
140
-						->where($alias . '.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey))
141
-						->limit(1);
142
-				}
143
-			])
144
-			->whereRaw("MATCH (`translations`.`value`) AGAINST (? IN BOOLEAN MODE)", $this->fullTextWildcards($term))
145
-			->orderByDesc('relevance');
146
-	}
132
+    public function scopeSearchFullText($query, $term, $fields = [])
133
+    {
134
+        $alias = Str::random(8);
135
+        return $query->join(\DB::raw('translations as ' . $alias . ' ON ' . $alias . '.translatable_id = ' . $this->getTable() . '.' . $this->primaryKey))
136
+            ->addSelect([
137
+                'relevance' => function ($query) use ($term, $alias) {
138
+                    $query->selectRaw("MATCH (`' . $alias . '`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)")
139
+                        ->from('translations')
140
+                        ->where($alias . '.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey))
141
+                        ->limit(1);
142
+                }
143
+            ])
144
+            ->whereRaw("MATCH (`translations`.`value`) AGAINST (? IN BOOLEAN MODE)", $this->fullTextWildcards($term))
145
+            ->orderByDesc('relevance');
146
+    }
147 147
 }
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -19,18 +19,18 @@  discard block
 block discarded – undo
19 19
 	{
20 20
 		static::addGlobalScope(new TranslatableScope);
21 21
 		
22
-		static::saving(function ($model) {
22
+		static::saving(function($model) {
23 23
 			$model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray();
24 24
 			$model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray();
25 25
 		});
26 26
 		
27
-		static::saved(function ($model) {
27
+		static::saved(function($model) {
28 28
 			if ($model->translatable) {
29 29
 				(new self)->saveTranslation($model);
30 30
 			}
31 31
 		});
32 32
 		
33
-		static::deleted(function ($model) {
33
+		static::deleted(function($model) {
34 34
 			if ((new $model)->has('translations')) {
35 35
 				$model->translations()->delete();
36 36
 			}
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 		}
68 68
 		
69 69
 		// Build the sub select
70
-		$fields->each(function ($key) use ($query) {
70
+		$fields->each(function($key) use ($query) {
71 71
 			$this->subSelectTranslation($query, $key);
72 72
 		});
73 73
 		
@@ -77,13 +77,13 @@  discard block
 block discarded – undo
77 77
 	protected function subSelectTranslation($query, $key): void
78 78
 	{
79 79
 		$query->addSelect([
80
-			$key => function ($query) use ($key) {
81
-				$query->select(Translation::getTableName() . '.value')
80
+			$key => function($query) use ($key) {
81
+				$query->select(Translation::getTableName().'.value')
82 82
 					->from(Translation::getTableName())
83
-					->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this))
84
-					->where(Translation::getTableName() . '.locale', '=', $this->locale)
85
-					->where(Translation::getTableName() . '.key', '=', $key)
86
-					->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey));
83
+					->where(Translation::getTableName().'.translatable_type', '=', \get_class($this))
84
+					->where(Translation::getTableName().'.locale', '=', $this->locale)
85
+					->where(Translation::getTableName().'.key', '=', $key)
86
+					->where(Translation::getTableName().'.translatable_id', '=', \DB::raw($this->getTable().'.'.$this->primaryKey));
87 87
 			}
88 88
 		]);
89 89
 
@@ -106,10 +106,10 @@  discard block
 block discarded – undo
106 106
 	{
107 107
 		$table = Translation::getTableName();
108 108
 		
109
-		return DB::table($table)->select($table . '.key')
110
-			->where($table . '.translatable_type', \get_class($this))
111
-			->where($table . '.locale', $this->locale)
112
-			->groupBy($table . '.key')
109
+		return DB::table($table)->select($table.'.key')
110
+			->where($table.'.translatable_type', \get_class($this))
111
+			->where($table.'.locale', $this->locale)
112
+			->groupBy($table.'.key')
113 113
 			->pluck('key');
114 114
 	}
115 115
 	
@@ -124,20 +124,20 @@  discard block
 block discarded – undo
124 124
 	
125 125
 	protected function fullTextWildcards($term)
126 126
 	{
127
-		return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) {
128
-			return strlen($word) >= 3 ? '+' . $word . '*' : '';
127
+		return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function($word, $key) {
128
+			return strlen($word) >= 3 ? '+'.$word.'*' : '';
129 129
 		})->implode(' ');
130 130
 	}
131 131
 	
132 132
 	public function scopeSearchFullText($query, $term, $fields = [])
133 133
 	{
134 134
 		$alias = Str::random(8);
135
-		return $query->join(\DB::raw('translations as ' . $alias . ' ON ' . $alias . '.translatable_id = ' . $this->getTable() . '.' . $this->primaryKey))
135
+		return $query->join(\DB::raw('translations as '.$alias.' ON '.$alias.'.translatable_id = '.$this->getTable().'.'.$this->primaryKey))
136 136
 			->addSelect([
137
-				'relevance' => function ($query) use ($term, $alias) {
138
-					$query->selectRaw("MATCH (`' . $alias . '`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)")
137
+				'relevance' => function($query) use ($term, $alias) {
138
+					$query->selectRaw("MATCH (`' . $alias . '`.`value`) AGAINST ('".$this->fullTextWildcards($term)."' IN BOOLEAN MODE)")
139 139
 						->from('translations')
140
-						->where($alias . '.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey))
140
+						->where($alias.'.translatable_id', DB::raw($this->getTable().'.'.$this->primaryKey))
141 141
 						->limit(1);
142 142
 				}
143 143
 			])
Please login to merge, or discard this patch.