DynamicAttribute   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 22
c 1
b 0
f 0
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dynamicUnit() 0 3 1
A customsAttributes() 0 3 1
A getThumbnailAttribute() 0 8 2
1
<?php
2
3
namespace SimpleCMS\DynamicUnit\Models;
4
5
use Spatie\MediaLibrary\HasMedia;
6
use Illuminate\Database\Eloquent\Model;
7
use SimpleCMS\Framework\Traits\MediaAttributeTrait;
8
use Spatie\MediaLibrary\MediaCollections\Models\Media;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
11
/**
12
 * DynamicAttribute
13
 * 
14
 * @property int $id 主键
15
 * @property int $dynamic_unit_id 属性ID
16
 * @property string $name 名称
17
 * @property ?string $code 键值参数
18
 * 
19
 * @property-read ?\Carbon\Carbon $created_at 创建时间
20
 * @property-read ?\Carbon\Carbon $updated_at 更新时间
21
 * @property-read ?array<string,string> $thumbnail 附件信息
22
 * @property-read ?DynamicUnit $dynamicUnit 属性
23
 * @property-read ?\Illuminate\Database\Eloquent\Collection<Media> $media 附件
24
 * @property-read ?\Illuminate\Database\Eloquent\Collection<CustomAttribute> $customsAttributes 自定义属性
25
 */
26
class DynamicAttribute extends Model implements HasMedia
27
{
28
    use MediaAttributeTrait,HasFactory;
29
30
    const MEDIA_FILE = 'file';
31
32
    protected $fillable = [
33
        'dynamic_unit_id',
34
        'name',
35
        'code',
36
    ];
37
38
    public $casts = [
39
        'thumbnail' => 'json',
40
        'dynamic_unit_id' => 'integer',
41
        'created_at' => 'datetime',
42
        'updated_at' => 'datetime'
43
    ];
44
45
    /**
46
     * 追加字段
47
     */
48
    public $appends = ['thumbnail'];
49
50
51
    /**
52
     * 隐藏关系
53
     */
54
    public $hidden = ['media'];
55
56
    /**
57
     * 属性类型
58
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
59
     */
60
    public function dynamicUnit()
61
    {
62
        return $this->belongsTo(DynamicAttribute::class);
63
    }
64
65
    /**
66
     * 自定义属性
67
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
68
     */
69
    public function customsAttributes()
70
    {
71
        return $this->morphMany(CustomAttribute::class,'model');
72
    }
73
74
    public function getThumbnailAttribute()
75
    {
76
        if (!$media = $this->getFirstMedia(self::MEDIA_FILE))
77
            return [];
78
        return [
79
            'name' => $media->file_name,
80
            'url' => $media->original_url,
81
            'uuid' => $media->uuid
82
        ];
83
    }
84
}
85