1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nord\Lumen\FileManager\Eloquent; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Nord\Lumen\FileManager\Contracts\File as FileContract; |
8
|
|
|
use Nord\Lumen\FileManager\Facades\FileManager; |
9
|
|
|
|
10
|
|
|
class File extends Model implements FileContract |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @inheritdoc |
15
|
|
|
*/ |
16
|
|
|
protected $fillable = [ |
17
|
|
|
'file_id', |
18
|
|
|
'name', |
19
|
|
|
'extension', |
20
|
|
|
'path', |
21
|
|
|
'mime_type', |
22
|
|
|
'byte_size', |
23
|
|
|
'data', |
24
|
|
|
'disk', |
25
|
|
|
'saved_at', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $hidden = [ |
32
|
|
|
'id', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $casts = [ |
39
|
|
|
'data' => 'array', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $dates = [ |
46
|
|
|
'saved_at', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $guarded = ['*']; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
public $timestamps = false; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getId() |
64
|
|
|
{ |
65
|
|
|
return $this->attributes['file_id']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getName() |
73
|
|
|
{ |
74
|
|
|
return $this->name; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getExtension() |
82
|
|
|
{ |
83
|
|
|
return $this->extension; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getMimeType() |
91
|
|
|
{ |
92
|
|
|
return $this->attributes['mime_type']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
|
|
public function getByteSize() |
100
|
|
|
{ |
101
|
|
|
return $this->attributes['byte_size']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getData() |
109
|
|
|
{ |
110
|
|
|
return $this->data; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getDisk() |
118
|
|
|
{ |
119
|
|
|
return $this->disk; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return Carbon |
125
|
|
|
*/ |
126
|
|
|
public function getSavedAt() |
127
|
|
|
{ |
128
|
|
|
return $this->attributes['saved_at']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getSavedAtAsTimestamp() |
136
|
|
|
{ |
137
|
|
|
return $this->getSavedAt()->getTimestamp(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getFilename() |
145
|
|
|
{ |
146
|
|
|
return $this->name . '-' . $this->getId() . '.' . $this->extension; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
|
|
public function getFilePath() |
154
|
|
|
{ |
155
|
|
|
return $this->getPath() . $this->getFilename(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
*/ |
162
|
|
|
public function getUrl(array $options = []) |
163
|
|
|
{ |
164
|
|
|
return FileManager::getFileUrl($this, $options); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
private function getPath() |
172
|
|
|
{ |
173
|
|
|
return isset($this->path) ? $this->path . '/' : ''; |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritdoc |
179
|
|
|
*/ |
180
|
|
|
public function toArray() |
181
|
|
|
{ |
182
|
|
|
return array_merge(parent::toArray(), ['url' => $this->getUrl()]); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $id |
188
|
|
|
* |
189
|
|
|
* @return File |
190
|
|
|
*/ |
191
|
|
|
public static function findByFileId($id) |
192
|
|
|
{ |
193
|
|
|
return self::where('file_id', $id)->first(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.