Issues (368)

src/Admin/Settings/Setting.php (1 issue)

Severity
1
<?php
2
3
namespace Arbory\Base\Admin\Settings;
4
5
use Arbory\Base\Files\ArboryFile;
6
use Illuminate\Database\Eloquent\Model;
7
use Arbory\Base\Services\SettingRegistry;
8
use Arbory\Base\Support\Translate\Translatable;
9
use Illuminate\Support\Arr;
10
11
class Setting extends Model
12
{
13
    use Translatable {
0 ignored issues
show
The trait Arbory\Base\Support\Translate\Translatable requires some properties which are not provided by Arbory\Base\Admin\Settings\Setting: $translations, $useTranslationFallback, $translationModel, $localeKey
Loading history...
14
        save as protected translatableSave;
15
        getAttribute as protected getTranslatableAttribute;
16
        setAttribute as protected setTranslatableAttribute;
17
        fill as protected translatableFill;
18
    }
19
20
    /**
21
     * @var string
22
     */
23
    protected $primaryKey = 'name';
24
25
    /**
26
     * @var string
27
     */
28
    protected $translationForeignKey = 'setting_name';
29
30
    /**
31
     * @var bool
32
     */
33
    public $incrementing = false;
34
35
    /**
36
     * @var array
37
     */
38
    protected $fillable = [
39
        'name', 'value', 'type',
40
    ];
41
42
    /**
43
     * @var array
44
     */
45
    protected $translatedAttributes = [
46
        'value',
47
    ];
48
49
    /**
50
     * @return string
51
     */
52
    public function __toString()
53
    {
54
        return (string) $this->name;
55
    }
56
57
    /**
58
     * @param  array  $attributes
59
     * @return Model|self
60
     *
61
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
62
     * @throws \ErrorException
63
     */
64
    public function fill(array $attributes)
65
    {
66
        $name = Arr::get($attributes, 'name');
67
68
        return $this->isTranslatable($name) ? $this->translatableFill($attributes) : parent::fill($attributes);
69
    }
70
71
    /**
72
     * @param  string  $key
73
     * @return mixed
74
     */
75
    public function getAttribute($key)
76
    {
77
        if (in_array($key, $this->translatedAttributes)) {
78
            if ($this->isTranslatable()) {
79
                return $this->getTranslatableAttribute($key);
80
            }
81
82
            return parent::getAttributeValue($key);
83
        }
84
85
        return parent::getAttribute($key);
86
    }
87
88
    /**
89
     * @param  string  $key
90
     * @param  mixed  $value
91
     * @return Model|self
92
     */
93
    public function setAttribute($key, $value)
94
    {
95
        if ($this->isTranslatable()) {
96
            return $this->setTranslatableAttribute($key, $value);
97
        }
98
99
        return parent::setAttribute($key, $value);
100
    }
101
102
    /**
103
     * @param  array  $options
104
     * @return bool
105
     */
106
    public function save(array $options = [])
107
    {
108
        if ($this->isTranslatable()) {
109
            return $this->translatableSave($options);
110
        }
111
112
        return parent::save($options);
113
    }
114
115
    /**
116
     * @param  mixed  $column
117
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
118
     */
119
    public function value($column = null)
120
    {
121
        return $column ? parent::value($column) : $this->file();
122
    }
123
124
    /**
125
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
126
     */
127
    public function file()
128
    {
129
        return $this->belongsTo(ArboryFile::class, 'value');
130
    }
131
132
    /**
133
     * @param  string|null  $settingName
134
     * @return bool
135
     */
136
    public function isTranslatable(string $settingName = null): bool
137
    {
138
        $settingName = $settingName ?? $this->name;
139
140
        if (! $settingName) {
141
            return false;
142
        }
143
144
        /**
145
         * @var SettingRegistry
146
         * @var SettingDefinition $definition
147
         */
148
        $registry = app(SettingRegistry::class);
149
        $definition = $registry->find($settingName);
150
151
        return $definition && $definition->getType() === \Arbory\Base\Admin\Form\Fields\Translatable::class;
152
    }
153
154
    /**
155
     * @return SettingDefinition|null
156
     */
157
    public function getDefinition()
158
    {
159
        /**
160
         * @var SettingRegistry
161
         * @var SettingDefinition $definition
162
         */
163
        $registry = app(SettingRegistry::class);
164
165
        return $registry->find($this->name) ?? new SettingDefinition($this->name);
166
    }
167
}
168