|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia; |
|
7
|
|
|
|
|
8
|
|
|
class MediaRepository |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var \Spatie\MediaLibrary\Media |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $model; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param \Spatie\MediaLibrary\Media $model |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(Media $model) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->model = $model; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get all media in the collection. |
|
25
|
|
|
* |
|
26
|
|
|
* @param HasMedia $model |
|
27
|
|
|
* @param string $collectionName |
|
28
|
|
|
* @param array|\Closure $filter |
|
29
|
|
|
* |
|
30
|
|
|
* @return Collection |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getCollection(HasMedia $model, $collectionName, $filter = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$mediaCollection = $this->loadMedia($model, $collectionName); |
|
35
|
|
|
|
|
36
|
|
|
$mediaCollection = $this->applyFilterToMediaCollection($mediaCollection, $filter); |
|
37
|
|
|
|
|
38
|
|
|
return Collection::make($mediaCollection); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Load media by collectionName. |
|
43
|
|
|
* |
|
44
|
|
|
* @param HasMedia $model |
|
45
|
|
|
* @param string $collectionName |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function loadMedia(HasMedia $model, $collectionName) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->mediaIsPreloaded($model)) { |
|
52
|
|
|
$media = $model->media->filter(function (Media $mediaItem) use ($collectionName) { |
|
53
|
|
|
|
|
54
|
|
|
if ($collectionName == '') { |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $mediaItem->collection_name == $collectionName; |
|
59
|
|
|
|
|
60
|
|
|
})->sortBy(function (Media $media) { |
|
61
|
|
|
|
|
62
|
|
|
return $media->order_column; |
|
63
|
|
|
|
|
64
|
|
|
})->values(); |
|
65
|
|
|
|
|
66
|
|
|
return $media; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$query = $model->media(); |
|
70
|
|
|
|
|
71
|
|
|
if ($collectionName != '') { |
|
72
|
|
|
$query = $query->where('collection_name', $collectionName); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$media = $query |
|
76
|
|
|
->orderBy('order_column') |
|
77
|
|
|
->get(); |
|
78
|
|
|
|
|
79
|
|
|
return $media; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Determine if media is already preloaded on this model. |
|
84
|
|
|
* |
|
85
|
|
|
* @param HasMedia $model |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function mediaIsPreloaded(HasMedia $model) |
|
90
|
|
|
{ |
|
91
|
|
|
return isset($model->media); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Apply given filters on media. |
|
96
|
|
|
* |
|
97
|
|
|
* @param \Illuminate\Support\Collection $media |
|
98
|
|
|
* @param array|\Closure $filter |
|
99
|
|
|
* |
|
100
|
|
|
* @return Collection |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function applyFilterToMediaCollection(Collection $media, $filter) |
|
103
|
|
|
{ |
|
104
|
|
|
if (is_array($filter)) { |
|
105
|
|
|
$filter = $this->getDefaultFilterFunction($filter); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $media->filter($filter); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get all media. |
|
113
|
|
|
* |
|
114
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
115
|
|
|
*/ |
|
116
|
|
|
public function all() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->model->all(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get all media for the given type. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $modelType |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getByModelType($modelType) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->model->where('model_type', $modelType)->get(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get media by ids. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $ids |
|
137
|
|
|
* |
|
138
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getByIds(array $ids) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->model->whereIn('id', $ids)->get(); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get all media for the given type and collection name. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $modelType |
|
149
|
|
|
* @param string $collectionName |
|
150
|
|
|
* |
|
151
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getByModelTypeAndCollectionName($modelType, $collectionName) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->model |
|
|
|
|
|
|
156
|
|
|
->where('model_type', $modelType) |
|
157
|
|
|
->where('collection_name', $collectionName) |
|
158
|
|
|
->get(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get all media for the given type and collection name. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $collectionName |
|
165
|
|
|
* |
|
166
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getByCollectionName($collectionName) |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->model |
|
|
|
|
|
|
171
|
|
|
->where('collection_name', $collectionName) |
|
172
|
|
|
->get(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Convert the given array to a filter function. |
|
177
|
|
|
* |
|
178
|
|
|
* @param $filters |
|
179
|
|
|
* |
|
180
|
|
|
* @return \Closure |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function getDefaultFilterFunction($filters) |
|
183
|
|
|
{ |
|
184
|
|
|
return function (Media $media) use ($filters) { |
|
185
|
|
|
|
|
186
|
|
|
$customProperties = $media->custom_properties; |
|
187
|
|
|
|
|
188
|
|
|
foreach ($filters as $property => $value) { |
|
189
|
|
|
if (!isset($customProperties[$property])) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
if ($customProperties[$property] != $value) { |
|
193
|
|
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return true; |
|
198
|
|
|
}; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: