CompanyApply::getFrontAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SimpleCMS\Company\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Collection;
8
use SimpleCMS\Framework\Contracts\SimpleMedia;
9
use SimpleCMS\Framework\Traits\MediaAttributeTrait;
10
use SimpleCMS\Framework\Traits\PrimaryKeyUuidTrait;
11
use Illuminate\Database\Eloquent\Factories\HasFactory;
12
use Spatie\MediaLibrary\MediaCollections\Models\Media;
13
14
/**
15
 * Banner Model
16
 * @author Dennis Lui <[email protected]>
17
 *
18
 * @property string $id 主键
19
 * @property string<"company","person"> $type 资料类型
20
 * @property string $id_number 企业注册号/身份证号码
21
 * @property string $name 企业名称
22
 * @property string $legal 法人/注册人
23
 * @property ?string $phone 联系电话
24
 * @property ?string $email 联系邮箱
25
 * @property ?string $address 注册地址
26
 * @property ?string $account 登录账号
27
 * @property ?string $password 登录密码
28
 * @property int $status 申请状态
29
 * @property ?string $reason 申请理由
30
 * @property ?string $remark 备注
31
 * @property ?string $reject 拒绝理由
32
 * @property ?Carbon $success_at 通过时间
33
 * @property ?Carbon $reject_at 拒绝时间
34
 * @property-read ?Carbon $created_at 创建时间
35
 * @property-read ?Carbon $updated_at 更新时间
36
 * @property-read ?Company $company 审核通过后的公司信息
37
 * @property-read ?Collection<Media> $media 附件
38
 * @property-read ?array<array<string,string>> $files 辅助资料
39
 * @property-read ?array<string,string> $logo Logo
40
 * @property-read ?array<string,string> $front 证件正面
41
 * @property-read ?array<string,string> $back 证件反面
42
 */
43
class CompanyApply extends Model implements SimpleMedia
44
{
45
    use HasFactory, PrimaryKeyUuidTrait, MediaAttributeTrait;
46
47
    /**
48
     * Media Key
49
     */
50
    const MEDIA_FILE = 'file';
51
52
    /**
53
     * Logo
54
     */
55
    const MEDIA_LOGO = 'logo';
56
57
    /**
58
     * 证件正面
59
     * @var string
60
     */
61
    const MEDIA_FRONT = 'front';
62
63
    /**
64
     * 证件背面
65
     * @var string
66
     */
67
    const MEDIA_BACK = 'back';
68
69
    const HAS_ONE_MEDIA = ['logo', 'front', 'back'];
70
71
    /**
72
     * 可输入字段
73
     */
74
    protected $fillable = [
75
        'id',
76
        'type',
77
        'id_number',
78
        'name',
79
        'legal',
80
        'phone',
81
        'email',
82
        'address',
83
        'account',
84
        'password',
85
        'status',
86
        'reason',
87
        'remark',
88
        'reject',
89
        'success_at',
90
        'reject_at',
91
    ];
92
93
    /**
94
     * 显示字段类型
95
     */
96
    public $casts = [
97
        'status' => 'integer',
98
        'success_at' => 'datetime',
99
        'reject_at' => 'datetime',
100
        'created_at' => 'datetime',
101
        'updated_at' => 'datetime'
102
    ];
103
104
    /**
105
     * 追加字段
106
     */
107
    public $appends = ['logo', 'front', 'back', 'files'];
108
109
    /**
110
     * 隐藏关系
111
     */
112
    public $hidden = ['media', 'password'];
113
114
115
    public function getFilesAttribute()
116
    {
117
        if (!$medias = $this->getMedia(self::MEDIA_FILE))
118
            return [];
119
        return $medias->map(function ($item) {
120
            return [
121
                'name' => $item->file_name,
122
                'url' => $item->original_url,
123
                'uuid' => $item->uuid
124
            ];
125
        });
126
    }
127
128
    public function getLogoAttribute()
129
    {
130
        if (!$media = $this->getFirstMedia(self::MEDIA_LOGO))
131
            return [];
132
        return [
133
            'name' => $media->file_name,
134
            'url' => $media->original_url,
135
            'uuid' => $media->uuid
136
        ];
137
    }
138
139
    public function getBackAttribute()
140
    {
141
        if (!$media = $this->getFirstMedia(self::MEDIA_BACK))
142
            return [];
143
        return [
144
            'name' => $media->file_name,
145
            'url' => $media->original_url,
146
            'uuid' => $media->uuid
147
        ];
148
    }
149
150
    public function getFrontAttribute()
151
    {
152
        if (!$media = $this->getFirstMedia(self::MEDIA_FRONT))
153
            return [];
154
        return [
155
            'name' => $media->file_name,
156
            'url' => $media->original_url,
157
            'uuid' => $media->uuid
158
        ];
159
    }
160
161
    /**
162
     * 申请通过后的公司信息
163
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
164
     */
165
    public function company()
166
    {
167
        return $this->hasOne(Company::class);
168
    }
169
}
170