|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection as DbCollection; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMedia; |
|
10
|
|
|
use Spatie\MediaLibrary\Models\Media; |
|
11
|
|
|
|
|
12
|
|
|
class MediaRepository |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Spatie\MediaLibrary\Models\Media */ |
|
15
|
|
|
protected $model; |
|
16
|
|
|
|
|
17
|
|
|
/** @param \Spatie\MediaLibrary\Models\Media $model */ |
|
18
|
|
|
public function __construct(Media $model) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->model = $model; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get all media in the collection. |
|
25
|
|
|
* |
|
26
|
|
|
* @param \Spatie\MediaLibrary\HasMedia\HasMedia $model |
|
27
|
|
|
* @param string $collectionName |
|
28
|
|
|
* @param array|callable $filter |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Support\Collection |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getCollection(HasMedia $model, string $collectionName, $filter = []): Collection |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->applyFilterToMediaCollection($model->loadMedia($collectionName), $filter); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Apply given filters on media. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Illuminate\Support\Collection $media |
|
41
|
|
|
* @param array|callable $filter |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Illuminate\Support\Collection |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function applyFilterToMediaCollection(Collection $media, $filter): Collection |
|
46
|
|
|
{ |
|
47
|
|
|
if (is_array($filter)) { |
|
48
|
|
|
$filter = $this->getDefaultFilterFunction($filter); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $media->filter($filter); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function all(): DbCollection |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->model->all(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getByModelType(string $modelType): DbCollection |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->model->where('model_type', $modelType)->get(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getByIds(array $ids): DbCollection |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->model->whereIn($this->model->getKeyName(), $ids)->get(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getByModelTypeAndCollectionName(string $modelType, string $collectionName): DbCollection |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->model |
|
72
|
|
|
->where('model_type', $modelType) |
|
73
|
|
|
->where('collection_name', $collectionName) |
|
74
|
|
|
->get(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getByCollectionName(string $collectionName): DbCollection |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->model |
|
80
|
|
|
->where('collection_name', $collectionName) |
|
81
|
|
|
->get(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Convert the given array to a filter function. |
|
86
|
|
|
* |
|
87
|
|
|
* @param $filters |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Closure |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getDefaultFilterFunction(array $filters): Closure |
|
92
|
|
|
{ |
|
93
|
|
|
return function (Media $media) use ($filters) { |
|
94
|
|
|
foreach ($filters as $property => $value) { |
|
95
|
|
|
if (! Arr::has($media->custom_properties, $property)) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (Arr::get($media->custom_properties, $property) !== $value) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return true; |
|
105
|
|
|
}; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|