|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibraryPro\Dto; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Spatie\MediaLibraryPro\Models\TemporaryUpload; |
|
9
|
|
|
|
|
10
|
|
|
class PendingMediaItem |
|
11
|
|
|
{ |
|
12
|
|
|
public TemporaryUpload $temporaryUpload; |
|
13
|
|
|
public string $name; |
|
14
|
|
|
public int $order; |
|
15
|
|
|
public array $customProperties; |
|
16
|
|
|
public ?string $fileName; |
|
17
|
|
|
|
|
18
|
|
|
public static function createFromArray(array $pendingMediaItems): Collection |
|
19
|
|
|
{ |
|
20
|
|
|
return collect($pendingMediaItems) |
|
21
|
|
|
->map(fn (array $uploadAttributes) => new static( |
|
22
|
|
|
$uploadAttributes['uuid'], |
|
23
|
|
|
$uploadAttributes['name'] ?? '', |
|
24
|
|
|
$uploadAttributes['order'] ?? 0, |
|
25
|
|
|
$uploadAttributes['custom_properties'] ?? [], |
|
26
|
|
|
$uploadAttributes['fileName'] ?? null, |
|
|
|
|
|
|
27
|
|
|
)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
string $uuid, |
|
32
|
|
|
string $name, |
|
33
|
|
|
int $order, |
|
34
|
|
|
array $customProperties, |
|
35
|
|
|
array $customHeaders, |
|
|
|
|
|
|
36
|
|
|
string $fileName = null |
|
37
|
|
|
) { |
|
38
|
|
|
$temporaryUploadModelClass = config('media-library.temporary_upload_model'); |
|
39
|
|
|
|
|
40
|
|
|
if (! $temporaryUpload = $temporaryUploadModelClass::findByMediaUuidInCurrentSession($uuid)) { |
|
41
|
|
|
throw new Exception('invalid uuid'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->temporaryUpload = $temporaryUpload; |
|
45
|
|
|
|
|
46
|
|
|
$this->name = $name; |
|
47
|
|
|
|
|
48
|
|
|
$this->order = $order; |
|
49
|
|
|
|
|
50
|
|
|
$this->customProperties = $customProperties; |
|
51
|
|
|
|
|
52
|
|
|
$this->fileName = $fileName; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function toArray(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$media = $this->temporaryUpload->getFirstMedia(); |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'uuid' => $media->uuid, |
|
61
|
|
|
'name' => $this->name, |
|
62
|
|
|
'order' => $this->order, |
|
63
|
|
|
'custom_properties' => $this->customProperties, |
|
64
|
|
|
'size' => $media->size, |
|
65
|
|
|
'mime' => $media->mime, |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getCustomProperties(array $customPropertyNames): array |
|
70
|
|
|
{ |
|
71
|
|
|
if (! count($customPropertyNames)) { |
|
72
|
|
|
return $this->customProperties; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return collect($customPropertyNames) |
|
76
|
|
|
->filter(fn (string $customProperty) => Arr::has($this->customProperties, $customProperty)) |
|
77
|
|
|
->mapWithKeys(fn ($name) => [$name => Arr::get($this->customProperties, $name)]) |
|
78
|
|
|
->toArray(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|