1 | <?php |
||
13 | class ConversionCollection extends Collection |
||
14 | { |
||
15 | /** @var \Spatie\MediaLibrary\Media */ |
||
16 | protected $media; |
||
17 | |||
18 | /** |
||
19 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
20 | * |
||
21 | * @return static |
||
22 | */ |
||
23 | public static function createForMedia(Media $media) |
||
24 | { |
||
25 | return (new static())->setMedia($media); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function setMedia(Media $media) |
||
34 | { |
||
35 | $this->media = $media; |
||
36 | |||
37 | $this->items = []; |
||
38 | |||
39 | $this->addConversionsFromRelatedModel($media); |
||
40 | |||
41 | $this->addManipulationsFromDb($media); |
||
42 | |||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get a conversion by it's name. |
||
48 | * |
||
49 | * @param string $name |
||
50 | * |
||
51 | * @return mixed |
||
52 | * |
||
53 | * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
||
54 | */ |
||
55 | public function getByName(string $name) |
||
56 | { |
||
57 | $conversion = $this->first(function (Conversion $conversion) use ($name) { |
||
58 | return $conversion->getName() === $name; |
||
59 | }); |
||
60 | |||
61 | if (! $conversion) { |
||
62 | throw InvalidConversion::unknownName($name); |
||
63 | } |
||
64 | |||
65 | return $conversion; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Add the conversion that are defined on the related model of |
||
70 | * the given media. |
||
71 | * |
||
72 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
73 | */ |
||
74 | protected function addConversionsFromRelatedModel(Media $media) |
||
75 | { |
||
76 | $modelName = Arr::get(Relation::morphMap(), $media->model_type, $media->model_type); |
||
77 | |||
78 | /** @var \Spatie\MediaLibrary\HasMedia\HasMedia $model */ |
||
79 | $model = new $modelName(); |
||
80 | |||
81 | /* |
||
82 | * In some cases the user might want to get the actual model |
||
83 | * instance so conversion parameters can depend on model |
||
84 | * properties. This will causes extra queries. |
||
85 | */ |
||
86 | if ($model->registerMediaConversionsUsingModelInstance) { |
||
|
|||
87 | $model = $media->model; |
||
88 | |||
89 | $model->mediaConversion = []; |
||
90 | } |
||
91 | |||
92 | $model->registerAllMediaConversions($media); |
||
93 | |||
94 | $this->items = $model->mediaConversions; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Add the extra manipulations that are defined on the given media. |
||
99 | * |
||
100 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
101 | */ |
||
102 | protected function addManipulationsFromDb(Media $media) |
||
108 | |||
109 | public function getConversions(string $collectionName = ''): self |
||
110 | { |
||
111 | if ($collectionName === '') { |
||
112 | return $this; |
||
117 | |||
118 | /* |
||
119 | * Get all the conversions in the collection that should be queued. |
||
120 | */ |
||
121 | public function getQueuedConversions(string $collectionName = ''): self |
||
125 | |||
126 | /* |
||
127 | * Add the given manipulation to the conversion with the given name. |
||
128 | */ |
||
129 | protected function addManipulationToConversion(Manipulations $manipulations, string $conversionName) |
||
139 | |||
140 | /* |
||
141 | * Get all the conversions in the collection that should not be queued. |
||
142 | */ |
||
143 | public function getNonQueuedConversions(string $collectionName = ''): self |
||
147 | |||
148 | /* |
||
149 | * Return the list of conversion files. |
||
150 | */ |
||
151 | public function getConversionsFiles(string $collectionName = ''): self |
||
159 | } |
||
160 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: