1 | <?php |
||||
2 | |||||
3 | namespace Fomvasss\MediaLibraryExtension\Models\Traits\HasMedia; |
||||
4 | |||||
5 | use Illuminate\Support\Str; |
||||
6 | use Spatie\MediaLibrary\Models\Media; |
||||
7 | |||||
8 | trait HasMediaTrait |
||||
9 | { |
||||
10 | use \Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
||||
11 | |||||
12 | /** |
||||
13 | * Define this in your model. |
||||
14 | * @var int |
||||
15 | */ |
||||
16 | // protected $mediaQuality; |
||||
17 | |||||
18 | /** |
||||
19 | * Define this in your model. |
||||
20 | * @var array |
||||
21 | */ |
||||
22 | // protected $mediaFieldsSingle = ['file', 'image',]; |
||||
23 | |||||
24 | /** |
||||
25 | * Define this in your model. |
||||
26 | * @var array |
||||
27 | */ |
||||
28 | // protected $mediaFieldsMultiple = ['files', 'images',]; |
||||
29 | |||||
30 | /** |
||||
31 | * Define this in your model if needed validation. |
||||
32 | * @var array |
||||
33 | */ |
||||
34 | /* |
||||
35 | protected $mediaFieldsValidation = [ |
||||
36 | 'file' => 'required|file', |
||||
37 | 'images' => 'required|array|max:4', |
||||
38 | 'images.*' => 'image|file|max:1024', |
||||
39 | ];*/ |
||||
40 | |||||
41 | /** |
||||
42 | * Redefine this in your model, |
||||
43 | * like spatie registerMediaConversions. |
||||
44 | * |
||||
45 | * @param \Spatie\MediaLibrary\Models\Media|null $media |
||||
46 | */ |
||||
47 | public function customMediaConversions(Media $media = null) |
||||
0 ignored issues
–
show
|
|||||
48 | { |
||||
49 | //... |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * @param string $collectionName |
||||
54 | * @param string $conversionName |
||||
55 | * @param string $defaultUrl |
||||
56 | * @return string |
||||
57 | */ |
||||
58 | public function getMyFirstMediaUrl(string $collectionName = 'default', string $conversionName = '', string $defaultUrl = ''): string |
||||
59 | { |
||||
60 | if ($media = $this->getFirstMedia($collectionName)) { |
||||
61 | return $media->getUrl($conversionName); |
||||
62 | } |
||||
63 | |||||
64 | return $defaultUrl; |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * @param int $mediaQuality |
||||
69 | * @return HasMediaTrait |
||||
70 | */ |
||||
71 | public function setMediaQuality(int $mediaQuality): self |
||||
72 | { |
||||
73 | $this->mediaQuality = $mediaQuality; |
||||
0 ignored issues
–
show
|
|||||
74 | |||||
75 | return $this; |
||||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * @return int |
||||
80 | */ |
||||
81 | public function getMediaQuality(): int |
||||
82 | { |
||||
83 | return isset($this->mediaQuality) && is_int($this->mediaQuality) ? $this->mediaQuality : config('medialibrary-extension.default_img_quantity'); |
||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * @param Media|null $media |
||||
88 | * @throws \Spatie\Image\Exceptions\InvalidManipulation |
||||
89 | */ |
||||
90 | public function defaultRegisterMediaConversions(Media $media = null) |
||||
0 ignored issues
–
show
The parameter
$media is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
91 | { |
||||
92 | foreach (config('medialibrary-extension.default_conversions') as $conversionName => $params) { |
||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
93 | if (is_array($params) && count($params)) { |
||||
94 | $this->addMediaConversion($conversionName) |
||||
95 | ->quality($params['quantity'] ?? $this->getMediaQuality()) |
||||
96 | ->crop($params['crop-method'] ?? 'crop-center', $params['width'] ?? 50, $params['height'] ?? 50) |
||||
97 | ->performOnCollections(...$this->getPerformOnImageCollections($params['regex_perform_to_collections'] ?? null)); |
||||
98 | } |
||||
99 | } |
||||
100 | } |
||||
101 | |||||
102 | /** |
||||
103 | * @return array |
||||
104 | */ |
||||
105 | public function getPerformOnImageCollections(string $pattern = null): array |
||||
106 | { |
||||
107 | $mediaFields = array_values(array_merge($this->getMediaFieldsMultiple(), $this->getMediaFieldsSingle())); |
||||
108 | $pattern = $pattern ?: '/img|image|photo|gallery|scr/i'; |
||||
109 | $performOnCollections = []; |
||||
110 | |||||
111 | foreach ($mediaFields as $field) { |
||||
112 | if (preg_match($pattern ,$field)) { |
||||
113 | $performOnCollections[] = $field; |
||||
114 | } |
||||
115 | } |
||||
116 | |||||
117 | return $performOnCollections; |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * @param \Spatie\MediaLibrary\Models\Media|null $media |
||||
122 | * @throws \Spatie\Image\Exceptions\InvalidManipulation |
||||
123 | */ |
||||
124 | public function registerMediaConversions(Media $media = null) |
||||
125 | { |
||||
126 | $this->defaultRegisterMediaConversions($media); |
||||
127 | |||||
128 | $this->customMediaConversions($media); |
||||
129 | } |
||||
130 | |||||
131 | /** |
||||
132 | * @return array |
||||
133 | */ |
||||
134 | public function getMediaFieldsSingle(): array |
||||
135 | { |
||||
136 | $res = isset($this->mediaFieldsSingle) ? (is_array($this->mediaFieldsSingle) ? $this->mediaFieldsSingle : [$this->mediaFieldsSingle]) : []; |
||||
137 | |||||
138 | return $res; |
||||
139 | } |
||||
140 | |||||
141 | /** |
||||
142 | * @return array |
||||
143 | */ |
||||
144 | public function getMediaFieldsMultiple(): array |
||||
145 | { |
||||
146 | $res = isset($this->mediaFieldsMultiple) ? (is_array($this->mediaFieldsMultiple) ? $this->mediaFieldsMultiple : [$this->mediaFieldsMultiple]) : []; |
||||
147 | |||||
148 | return $res; |
||||
149 | } |
||||
150 | |||||
151 | /** |
||||
152 | * @param array $mediaFieldsSingle |
||||
153 | * @return $this |
||||
154 | */ |
||||
155 | public function setMediaFieldsSingle(array $mediaFieldsSingle) |
||||
156 | { |
||||
157 | $this->mediaFieldsSingle = $mediaFieldsSingle; |
||||
0 ignored issues
–
show
|
|||||
158 | |||||
159 | return $this; |
||||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * @param array $mediaFieldsMultiple |
||||
164 | * @return $this |
||||
165 | */ |
||||
166 | public function setMediaFieldsMultiple(array $mediaFieldsMultiple) |
||||
167 | { |
||||
168 | $this->mediaFieldsMultiple = $mediaFieldsMultiple; |
||||
0 ignored issues
–
show
|
|||||
169 | |||||
170 | return $this; |
||||
171 | } |
||||
172 | |||||
173 | /** |
||||
174 | * @param string|null $field |
||||
175 | * @return array|mixed|string |
||||
176 | */ |
||||
177 | public function getMediaFieldsValidation(string $field = null): array |
||||
178 | { |
||||
179 | $allRules = isset($this->mediaFieldsValidation) && is_array($this->mediaFieldsValidation) ? $this->mediaFieldsValidation : []; |
||||
180 | |||||
181 | $rules = []; |
||||
182 | |||||
183 | if ($field) { |
||||
184 | $rules[$field] = $allRules[$field] ?? ''; |
||||
185 | if (isset($allRules[$field . '.*']) ) { |
||||
186 | $rules[$field . '.*'] = $allRules[$field . '.*']; |
||||
187 | } |
||||
188 | } else { |
||||
189 | return $allRules; |
||||
190 | } |
||||
191 | |||||
192 | return $rules; |
||||
193 | } |
||||
194 | |||||
195 | /** |
||||
196 | * @param array $rules |
||||
197 | * @return $this |
||||
198 | */ |
||||
199 | public function setMediaFieldsValidation(array $rules = []) |
||||
200 | { |
||||
201 | $this->mediaFieldsValidation = $rules; |
||||
0 ignored issues
–
show
|
|||||
202 | |||||
203 | return $this; |
||||
204 | } |
||||
205 | |||||
206 | /** |
||||
207 | * @param array $rules |
||||
208 | * @return $this |
||||
209 | */ |
||||
210 | public function addMediaFieldsValidation(array $rules = []) |
||||
211 | { |
||||
212 | $rules = array_merge($this->getMediaFieldsValidation(), $rules); |
||||
213 | |||||
214 | $this->setMediaFieldsValidation($rules); |
||||
215 | |||||
216 | return $this; |
||||
217 | } |
||||
218 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.