1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\MediaCollection; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Traits\Macroable; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class MediaCollection |
9
|
|
|
{ |
10
|
|
|
use Macroable; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
public $name = ''; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
public $diskName = ''; |
17
|
|
|
|
18
|
|
|
/** @var callable */ |
19
|
|
|
public $mediaConversionRegistrations; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
public $generateResponsiveImages = false; |
23
|
|
|
|
24
|
|
|
/** @var callable */ |
25
|
|
|
public $acceptsFile; |
26
|
|
|
|
27
|
|
|
/** @var array $acceptsMimeTypes */ |
28
|
|
|
public $acceptsMimeTypes = []; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
public $collectionSizeLimit = false; |
32
|
|
|
|
33
|
|
|
public $singleFile = false; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
public $fallbackUrl = ''; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
public $fallbackPath = ''; |
40
|
|
|
|
41
|
|
|
public function __construct(string $name) |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
|
|
|
45
|
|
|
$this->mediaConversionRegistrations = function () { |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
$this->acceptsFile = function () { |
49
|
|
|
return true; |
50
|
|
|
}; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function create($name) |
54
|
|
|
{ |
55
|
|
|
return new static($name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function useDisk(string $diskName): self |
59
|
|
|
{ |
60
|
|
|
$this->diskName = $diskName; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function acceptsFile(callable $acceptsFile): self |
66
|
|
|
{ |
67
|
|
|
$this->acceptsFile = $acceptsFile; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function acceptsMimeTypes(array $mimeTypes): self |
73
|
|
|
{ |
74
|
|
|
$this->acceptsMimeTypes = $mimeTypes; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function singleFile(): self |
80
|
|
|
{ |
81
|
|
|
return $this->onlyKeepLatest(1); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function onlyKeepLatest(int $maximumNumberOfItemsInCollection): self |
85
|
|
|
{ |
86
|
|
|
if ($maximumNumberOfItemsInCollection < 1) { |
87
|
|
|
throw new InvalidArgumentException("You should pass a value higher than 0. `{$maximumNumberOfItemsInCollection}` given."); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->singleFile = ($maximumNumberOfItemsInCollection === 1); |
91
|
|
|
|
92
|
|
|
$this->collectionSizeLimit = $maximumNumberOfItemsInCollection; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function registerMediaConversions(callable $mediaConversionRegistrations) |
98
|
|
|
{ |
99
|
|
|
$this->mediaConversionRegistrations = $mediaConversionRegistrations; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function useFallbackUrl(string $url): self |
103
|
|
|
{ |
104
|
|
|
$this->fallbackUrl = $url; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function useFallbackPath(string $path): self |
110
|
|
|
{ |
111
|
|
|
$this->fallbackPath = $path; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function withResponsiveImages(): self |
117
|
|
|
{ |
118
|
|
|
$this->generateResponsiveImages = true; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|