Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Issues (866)

Branch: main

Traits/SpatieTranslatable/HasTranslations.php (1 issue)

1
<?php
2
3
namespace Backpack\CRUD\app\Models\Traits\SpatieTranslatable;
4
5
use Illuminate\Support\Arr;
6
use Spatie\Translatable\HasTranslations as OriginalHasTranslations;
7
8
trait HasTranslations
9
{
10
    use OriginalHasTranslations;
11
12
    /**
13
     * @var bool
14
     */
15
    public $locale = false;
16
17
    /*
18
    |--------------------------------------------------------------------------
19
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
20
    |--------------------------------------------------------------------------
21
    */
22
23
    /**
24
     * Use the forced locale if present.
25
     *
26
     * @param  string  $key
27
     * @return mixed
28
     */
29
    public function getAttributeValue($key)
30
    {
31
        if (! $this->isTranslatableAttribute($key)) {
32
            return parent::getAttributeValue($key);
33
        }
34
35
        $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'));
0 ignored issues
show
It seems like $this->locale ?: config('app.locale') can also be of type true; however, parameter $locale of Backpack\CRUD\app\Models...tions::getTranslation() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $translation = $this->getTranslation($key, /** @scrutinizer ignore-type */ $this->locale ?: config('app.locale'));
Loading history...
36
37
        // if it's a fake field, json_encode it
38
        if (is_array($translation)) {
39
            return json_encode($translation, JSON_UNESCAPED_UNICODE);
40
        }
41
42
        return $translation;
43
    }
44
45
    public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)
46
    {
47
        $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale);
48
49
        $translations = $this->getTranslations($key);
50
51
        $translation = $translations[$locale] ?? '';
52
53
        if ($this->hasGetMutator($key)) {
54
            return $this->mutateAttribute($key, $translation);
55
        }
56
57
        return $translation;
58
    }
59
60
    /*
61
    |--------------------------------------------------------------------------
62
    |                            ELOQUENT OVERWRITES
63
    |--------------------------------------------------------------------------
64
    */
65
66
    /**
67
     * Create translated items as json.
68
     *
69
     * @param  array  $attributes
70
     * @return static
71
     */
72
    public static function create(array $attributes = [])
73
    {
74
        $locale = $attributes['locale'] ?? \App::getLocale();
75
        $attributes = Arr::except($attributes, ['locale']);
76
        $non_translatable = [];
77
78
        $model = new static();
79
80
        // do the actual saving
81
        foreach ($attributes as $attribute => $value) {
82
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
83
                $model->setTranslation($attribute, $locale, $value);
84
            } else { // the attribute is NOT translatable
85
                $non_translatable[$attribute] = $value;
86
            }
87
        }
88
        $model->fill($non_translatable)->save();
89
90
        return $model;
91
    }
92
93
    /**
94
     * Update translated items as json.
95
     *
96
     * @param  array  $attributes
97
     * @param  array  $options
98
     * @return bool
99
     */
100
    public function update(array $attributes = [], array $options = [])
101
    {
102
        if (! $this->exists) {
103
            return false;
104
        }
105
106
        $locale = $attributes['_locale'] ?? \App::getLocale();
107
        $attributes = Arr::except($attributes, ['_locale']);
108
        $non_translatable = [];
109
110
        // do the actual saving
111
        foreach ($attributes as $attribute => $value) {
112
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
113
                $this->setTranslation($attribute, $locale, $value);
114
            } else { // the attribute is NOT translatable
115
                $non_translatable[$attribute] = $value;
116
            }
117
        }
118
119
        return $this->fill($non_translatable)->save($options);
120
    }
121
122
    /*
123
    |--------------------------------------------------------------------------
124
    |                            CUSTOM METHODS
125
    |--------------------------------------------------------------------------
126
    */
127
128
    /**
129
     * Check if a model is translatable, by the adapter's standards.
130
     *
131
     * @return bool
132
     */
133
    public function translationEnabledForModel()
134
    {
135
        return property_exists($this, 'translatable');
136
    }
137
138
    /**
139
     * Get all locales the admin is allowed to use.
140
     *
141
     * @return array
142
     */
143
    public function getAvailableLocales()
144
    {
145
        return config('backpack.crud.locales');
146
    }
147
148
    /**
149
     * Set the locale property. Used in normalizeLocale() to force the translation
150
     * to a different language that the one set in app()->getLocale();.
151
     *
152
     * @param string
153
     */
154
    public function setLocale($locale)
155
    {
156
        $this->locale = $locale;
157
    }
158
159
    /**
160
     * Get the locale property. Used in SpatieTranslatableSluggableService
161
     * to save the slug for the appropriate language.
162
     *
163
     * @param string
164
     */
165
    public function getLocale()
166
    {
167
        if ($this->locale) {
168
            return $this->locale;
169
        }
170
171
        return \Request::input('_locale', \App::getLocale());
172
    }
173
174
    /**
175
     * Magic method to get the db entries already translated in the wanted locale.
176
     *
177
     * @param  string  $method
178
     * @param  array  $parameters
179
     * @return
180
     */
181
    public function __call($method, $parameters)
182
    {
183
        switch ($method) {
184
            // translate all find methods
185
            case 'find':
186
            case 'findOrFail':
187
            case 'findMany':
188
            case 'findBySlug':
189
            case 'findBySlugOrFail':
190
                $translation_locale = \Request::input('_locale', \App::getLocale());
191
192
                if ($translation_locale) {
193
                    $item = parent::__call($method, $parameters);
194
195
                    if ($item instanceof \Traversable) {
196
                        foreach ($item as $instance) {
197
                            $instance->setLocale($translation_locale);
198
                        }
199
                    } elseif ($item) {
200
                        $item->setLocale($translation_locale);
201
                    }
202
203
                    return $item;
204
                }
205
206
                return parent::__call($method, $parameters);
207
                break;
208
209
                // do not translate any other methods
210
            default:
211
                return parent::__call($method, $parameters);
212
                break;
213
        }
214
    }
215
}
216