Completed
Push — master ( 9c6422...d4c969 )
by Mohamed
01:18
created

Setting::getTypeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\MediaLibrary\HasMedia;
7
use Spatie\MediaLibrary\InteractsWithMedia;
8
9
class Setting extends Model implements HasMedia
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addMedia, addMediaConversion, clearMediaCollection, clearMediaCollectionExcept, copyMedia, getMedia, hasMedia, loadMedia, media, registerAllMediaConversions, registerMediaCollections, registerMediaConversions, shouldDeletePreservingMedia
Loading history...
10
{
11
    use InteractsWithMedia;
12
13
    /**
14
     * The attributes that are mass assignable.
15
     *
16
     * @var array
17
     */
18
    protected $fillable = [
19
        'key', 'name', 'value', 'cast'
20
    ];
21
22
    /**
23
     * The attributes that should be cast.
24
     *
25
     * @var array
26
     */
27
    protected $casts = [
28
        'cast' => 'collection'
29
    ];
30
31
    /**
32
     * The accessors to append to the model's array form.
33
     *
34
     * @var array
35
     */
36
    protected $appends = [
37
        'type', 'extra'
38
    ];
39
40
    /**
41
     * Return the value for the given key.
42
     *
43
     * @param string $key
44
     * @param null $default
45
     * @return string|null
46
     */
47
    public static function getValueFor($key, $default = null)
48
    {
49
        return optional(self::where('key', $key)->first())->value ?? $default;
50
    }
51
52
    /**
53
     * Get field's type.
54
     *
55
     * @return string
56
     */
57
    public function getTypeAttribute()
58
    {
59
        return $this->cast->get('type', 'text');
60
    }
61
62
    /**
63
     * Get Extra field's attributes.
64
     *
65
     * @return array
66
     */
67
    public function getExtraAttribute()
68
    {
69
        return json_decode($this->cast->get('extra', '{}'), true);
70
    }
71
}
72