1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Common\Records\Traits\Media\Files; |
4
|
|
|
|
5
|
|
|
use ByTIC\Common\Records\Media\Files\Model as ModelFile; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class RecordTrait |
9
|
|
|
* @package ByTIC\Common\Records\Traits\Media\Files |
10
|
|
|
*/ |
11
|
|
|
trait RecordTrait |
12
|
|
|
{ |
13
|
|
|
use \ByTIC\Common\Records\Traits\AbstractTrait\RecordTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ModelFile[] |
17
|
|
|
*/ |
18
|
|
|
public $files = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ModelFile $file |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function getFileURL($file) |
25
|
|
|
{ |
26
|
|
|
return $this->getUploadURL() . $file->getName(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $fileData |
31
|
|
|
* @return bool|ModelFile |
32
|
|
|
*/ |
33
|
|
|
public function uploadFile($fileData) |
34
|
|
|
{ |
35
|
|
|
$file = $this->getNewFile(); |
36
|
|
|
|
37
|
|
|
if ($file->upload($fileData)) { |
38
|
|
|
return $file; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* File factory |
46
|
|
|
* |
47
|
|
|
* @param null $type |
48
|
|
|
* |
49
|
|
|
* @return ModelFile |
50
|
|
|
*/ |
51
|
|
|
public function getNewFile($type = null) |
52
|
|
|
{ |
53
|
|
|
$class = $this->getFileModelName($type); |
54
|
|
|
/** @var ModelFile $file */ |
55
|
|
|
$file = new $class(); |
56
|
|
|
$file->setModel($this); |
57
|
|
|
$file->setFilesystem($this->getFilesystemDisk()); |
|
|
|
|
58
|
|
|
return $file; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param null $type |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getFileModelName($type = null) |
66
|
|
|
{ |
67
|
|
|
$name = $this->isNamespaced() ? $this->getFileModelNamespaced($type) : $this->getFileModelNameDefault($type); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param null $type |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getFileModelNamespaced($type = null) |
77
|
|
|
{ |
78
|
|
|
$type = $type ? $type : 'Generic'; |
79
|
|
|
|
80
|
|
|
return $this->getManager()->getModelNamespace() . 'Files\\' . ucfirst($type); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param null $type |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getFileModelNameDefault($type = null) |
88
|
|
|
{ |
89
|
|
|
if ($type) { |
90
|
|
|
return $this->getManager()->getModel() . "_File_" . ucfirst($type); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->getManager()->getModel() . "_File"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $request |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function removeFile($request) |
101
|
|
|
{ |
102
|
|
|
$this->checkFiles(); |
103
|
|
|
|
104
|
|
|
if ($this->files[$request['file']]) { |
105
|
|
|
$this->files[$request['file']]->delete(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Check if files have been inited |
113
|
|
|
* |
114
|
|
|
* @return ModelFile[]|null |
115
|
|
|
*/ |
116
|
|
|
public function checkFiles() |
117
|
|
|
{ |
118
|
|
|
if ($this->files === null) { |
119
|
|
|
$this->findFiles(); |
120
|
|
|
} |
121
|
|
|
return $this->files; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Find files |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function findFiles() |
130
|
|
|
{ |
131
|
|
|
$files = $this->getFilesystemDisk()->listContents($this->getFilesPath()); |
|
|
|
|
132
|
|
|
foreach ($files as $fileData) { |
133
|
|
|
$this->addNewFileFromArray($fileData); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getFilesPath() |
141
|
|
|
{ |
142
|
|
|
return '/files/' . $this->getManager()->getTable() . '/' . $this->id . '/'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $data |
147
|
|
|
*/ |
148
|
|
|
public function addNewFileFromArray($data) |
149
|
|
|
{ |
150
|
|
|
$file = $this->getNewFile(); |
151
|
|
|
$file->setPath($data['path']); |
152
|
|
|
$this->appendFile($file); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param ModelFile $file |
157
|
|
|
*/ |
158
|
|
|
public function appendFile($file) |
159
|
|
|
{ |
160
|
|
|
$this->files[$file->getName()] = $file; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return ModelFile[] |
165
|
|
|
*/ |
166
|
|
|
public function getFiles(): array |
167
|
|
|
{ |
168
|
|
|
$this->checkFiles(); |
169
|
|
|
return $this->files; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $files |
174
|
|
|
*/ |
175
|
|
|
public function setFiles($files = []) |
176
|
|
|
{ |
177
|
|
|
if ($files) { |
|
|
|
|
178
|
|
|
foreach ($files as $name) { |
179
|
|
|
$file = $this->getNewFile(); |
180
|
|
|
$file->setName($name); |
181
|
|
|
|
182
|
|
|
$this->files[$name] = $file; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.