1 | <?php |
||
18 | class FileAdder |
||
19 | { |
||
20 | /** @var \Illuminate\Database\Eloquent\Model subject */ |
||
21 | protected $subject; |
||
22 | |||
23 | /** @var \Spatie\MediaLibrary\Filesystem\Filesystem */ |
||
24 | protected $filesystem; |
||
25 | |||
26 | /** @var bool */ |
||
27 | protected $preserveOriginal = false; |
||
28 | |||
29 | /** @var string|\Symfony\Component\HttpFoundation\File\UploadedFile */ |
||
30 | protected $file; |
||
31 | |||
32 | /** @var array */ |
||
33 | protected $properties = []; |
||
34 | |||
35 | /** @var array */ |
||
36 | protected $customProperties = []; |
||
37 | |||
38 | /** @var array */ |
||
39 | protected $manipulations = []; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $pathToFile; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $fileName; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $mediaName; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $diskName = ''; |
||
52 | |||
53 | /** @var null|callable */ |
||
54 | protected $fileNameSanitizer; |
||
55 | |||
56 | /** |
||
57 | * @param Filesystem $fileSystem |
||
58 | */ |
||
59 | public function __construct(Filesystem $fileSystem) |
||
60 | { |
||
61 | $this->filesystem = $fileSystem; |
||
62 | |||
63 | $this->fileNameSanitizer = function ($fileName) { |
||
64 | return $this->defaultSanitizer($fileName); |
||
65 | }; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param \Illuminate\Database\Eloquent\Model $subject |
||
70 | * |
||
71 | * @return FileAdder |
||
72 | */ |
||
73 | public function setSubject(Model $subject) |
||
74 | { |
||
75 | $this->subject = $subject; |
||
76 | |||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | /* |
||
81 | * Set the file that needs to be imported. |
||
82 | * |
||
83 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function setFile($file) |
||
88 | { |
||
89 | $this->file = $file; |
||
90 | |||
91 | if (is_string($file)) { |
||
92 | $this->pathToFile = $file; |
||
93 | $this->setFileName(pathinfo($file, PATHINFO_BASENAME)); |
||
94 | $this->mediaName = pathinfo($file, PATHINFO_FILENAME); |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | if ($file instanceof UploadedFile) { |
||
100 | $this->pathToFile = $file->getPath().'/'.$file->getFilename(); |
||
101 | $this->setFileName($file->getClientOriginalName()); |
||
102 | $this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | if ($file instanceof SymfonyFile) { |
||
108 | $this->pathToFile = $file->getPath().'/'.$file->getFilename(); |
||
109 | $this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME)); |
||
110 | $this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | throw UnknownType::create(); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * When adding the file to the media library, the original file |
||
120 | * will be preserved. |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function preservingOriginal() |
||
125 | { |
||
126 | $this->preserveOriginal = true; |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Set the name of the media object. |
||
133 | * |
||
134 | * @param string $name |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function usingName(string $name) |
||
139 | { |
||
140 | return $this->setName($name); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Set the name of the media object. |
||
145 | * |
||
146 | * @param string $name |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function setName(string $name) |
||
151 | { |
||
152 | $this->mediaName = $name; |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Set the name of the file that is stored on disk. |
||
159 | * |
||
160 | * @param string $fileName |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function usingFileName(string $fileName) |
||
165 | { |
||
166 | return $this->setFileName($fileName); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Set the name of the file that is stored on disk. |
||
171 | * |
||
172 | * @param string $fileName |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setFileName(string $fileName) |
||
177 | { |
||
178 | $this->fileName = $fileName; |
||
179 | |||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Set the metadata. |
||
185 | * |
||
186 | * @param array $customProperties |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function withCustomProperties(array $customProperties) |
||
191 | { |
||
192 | $this->customProperties = $customProperties; |
||
193 | |||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Set the manipulations. |
||
199 | * |
||
200 | * @param array $manipulations |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function withManipulations(array $manipulations) |
||
205 | { |
||
206 | $this->manipulations = $manipulations; |
||
207 | |||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Set properties on the model. |
||
213 | * |
||
214 | * @param array $properties |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function withProperties(array $properties) |
||
224 | |||
225 | /** |
||
226 | * Set attributes on the model. |
||
227 | * |
||
228 | * @param array $properties |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function withAttributes(array $properties) |
||
236 | |||
237 | /** |
||
238 | * Add the given additional headers when copying the file to a remote filesystem. |
||
239 | * |
||
240 | * @param array $customRemoteHeaders |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function addCustomHeaders(array $customRemoteHeaders) |
||
250 | |||
251 | /** |
||
252 | * @param string $collectionName |
||
253 | * |
||
254 | * @return \Spatie\MediaLibrary\Media |
||
255 | * |
||
256 | * @throws FileCannotBeAdded |
||
257 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
258 | */ |
||
259 | public function toMediaCollectionOnCloudDisk(string $collectionName = 'default') |
||
260 | { |
||
263 | |||
264 | /** |
||
265 | * @param string $collectionName |
||
266 | * @param string $diskName |
||
267 | * |
||
268 | * @return \Spatie\MediaLibrary\Media |
||
269 | * |
||
270 | * @throws FileCannotBeAdded |
||
271 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
272 | */ |
||
273 | public function toMediaCollection(string $collectionName = 'default', string $diskName = '') |
||
306 | |||
307 | /** |
||
308 | * @param string $diskName |
||
309 | * |
||
310 | * @return string |
||
311 | * |
||
312 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
313 | */ |
||
314 | protected function determineDiskName(string $diskName) |
||
326 | |||
327 | /** |
||
328 | * @param $fileName |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function defaultSanitizer(string $fileName): string |
||
336 | |||
337 | /** |
||
338 | * Sanitize the fileName of the file using a callable. |
||
339 | * |
||
340 | * @param callable $fileNameSanitizer |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function sanitizingFileName(callable $fileNameSanitizer) |
||
350 | |||
351 | /** |
||
352 | * @param Media $media |
||
353 | */ |
||
354 | protected function attachMedia(Media $media) |
||
372 | |||
373 | /** |
||
374 | * @param HasMedia $model |
||
375 | * @param Media $media |
||
376 | * @param FileAdder $fileAdder |
||
377 | */ |
||
378 | protected function processMediaItem(HasMedia $model, Media $media, self $fileAdder) |
||
388 | } |
||
389 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: