Completed
Push — master ( 4ca5d1...1046b9 )
by Sebastian
04:57 queued 02:23
created

Media::hasNestedCustomProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Illuminate\Support\Collection;
6
use Spatie\MediaLibrary\Helpers\File;
7
use Illuminate\Database\Eloquent\Model;
8
use Spatie\MediaLibrary\Conversion\Conversion;
9
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Pdf;
10
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Svg;
11
use Spatie\MediaLibrary\Conversion\ConversionCollection;
12
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Image;
13
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Video;
14
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory;
15
16
class Media extends Model
17
{
18
    use SortableTrait;
19
20
    const TYPE_OTHER = 'other';
21
    const TYPE_IMAGE = 'image';
22
    const TYPE_VIDEO = 'video';
23
    const TYPE_SVG = 'svg';
24
    const TYPE_PDF = 'pdf';
25
26
    protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id'];
27
28
    /**
29
     * The attributes that should be casted to native types.
30
     *
31
     * @var array
32
     */
33
    protected $casts = [
34
        'manipulations' => 'array',
35
        'custom_properties' => 'array',
36
    ];
37
38
    /**
39
     * Create the polymorphic relation.
40
     *
41
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
42
     */
43
    public function model()
44
    {
45
        return $this->morphTo();
46
    }
47
48
    /**
49
     * Get the original Url to a media-file.
50
     *
51
     * @param string $conversionName
52
     *
53
     * @return string
54
     *
55
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
56
     */
57
    public function getUrl(string $conversionName = '') : string
58
    {
59
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
60
61
        if ($conversionName !== '') {
62
            $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName));
63
        }
64
65
        return $urlGenerator->getUrl();
66
    }
67
68
    /**
69
     * Get the original path to a media-file.
70
     *
71
     * @param string $conversionName
72
     *
73
     * @return string
74
     *
75
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
76
     */
77
    public function getPath(string $conversionName = '') : string
78
    {
79
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
80
81
        if ($conversionName != '') {
82
            $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName));
83
        }
84
85
        return $urlGenerator->getPath();
86
    }
87
88
    /**
89
     * Collection of all ImageGenerator drivers.
90
     */
91
    public function getImageGenerators() : Collection
92
    {
93
        return collect([
94
            Image::class,
95
            Pdf::class,
96
            Svg::class,
97
            Video::class,
98
        ]);
99
    }
100
101
    /**
102
     * Determine the type of a file.
103
     *
104
     * @return string
105
     */
106
    public function getTypeAttribute()
107
    {
108
        $type = $this->type_from_extension;
109
        if ($type !== self::TYPE_OTHER) {
110
            return $type;
111
        }
112
113
        return $this->type_from_mime;
114
    }
115
116
    /**
117
     * Determine the type of a file from its file extension.
118
     *
119
     * @return string
120
     */
121
    public function getTypeFromExtensionAttribute()
122
    {
123
        $imageGenerators = $this->getImageGenerators()
124
            ->map(function (string $className) {
125
                return app($className);
126
            });
127
128
        foreach ($imageGenerators as $imageGenerator) {
129
            if ($imageGenerator->canHandleExtension(strtolower($this->extension))) {
130
                return $imageGenerator->getType();
131
            }
132
        }
133
134
        return static::TYPE_OTHER;
135
    }
136
137
    /*
138
     * Determine the type of a file from its mime type
139
     */
140
    public function getTypeFromMimeAttribute() : string
141
    {
142
        $imageGenerators = $this->getImageGenerators()
143
            ->map(function (string $className) {
144
                return app($className);
145
            });
146
147
        foreach ($imageGenerators as $imageGenerator) {
148
            if ($imageGenerator->canHandleMime($this->getMimeAttribute())) {
149
                return $imageGenerator->getType();
150
            }
151
        }
152
153
        return static::TYPE_OTHER;
154
    }
155
156
    public function getMimeAttribute() : string
157
    {
158
        return File::getMimetype($this->getPath());
159
    }
160
161
    public function getExtensionAttribute() : string
162
    {
163
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
164
    }
165
166
    public function getHumanReadableSizeAttribute() : string
167
    {
168
        return File::getHumanReadableSize($this->size);
169
    }
170
171
    public function getDiskDriverName() : string
172
    {
173
        return strtolower(config("filesystems.disks.{$this->disk}.driver"));
174
    }
175
176
    /*
177
     * Determine if the media item has a custom property with the given name.
178
     */
179
    public function hasCustomProperty(string $propertyName) : bool
180
    {
181
        return array_key_exists($propertyName, $this->custom_properties);
182
    }
183
184
    /**
185
     * Determine if the media item has a custom property with the given name
186
     * using dot notation.
187
     *
188
     * @param string $propertyName
189
     *
190
     * @return bool
191
     *
192
     * @deprecated Will be removed in the next major version in favor of
193
     * changing `hasCustomProperty` to use dot notation.
194
     */
195
    public function hasNestedCustomProperty(string $propertyName) : bool
196
    {
197
        return array_has($this->custom_properties, $propertyName);
198
    }
199
200
    /**
201
     * Get if the value of custom property with the given name.
202
     *
203
     * @param string $propertyName
204
     * @param mixed $default
205
     *
206
     * @return mixed
207
     */
208
    public function getCustomProperty(string $propertyName, $default = null)
209
    {
210
        return $this->custom_properties[$propertyName] ?? $default;
211
    }
212
213
    /**
214
     * Get a custom property using dot notation.
215
     *
216
     * @param string $propertyName
217
     * @param mixed $default
218
     *
219
     * @return mixed
220
     *
221
     * @deprecated Will be removed in the next major version in favor of
222
     * changing `getCustomProperty` to use dot notation.
223
     */
224
    public function getNestedCustomProperty(string $propertyName, $default = null)
225
    {
226
        return array_get($this->custom_properties, $propertyName, $default);
227
    }
228
229
    /**
230
     * @param string $name
231
     * @param mixed $value
232
     */
233
    public function setCustomProperty(string $name, $value)
234
    {
235
        $this->custom_properties = array_merge($this->custom_properties, [$name => $value]);
236
    }
237
238
    /**
239
     * Set a custom property using dot notation.
240
     *
241
     * @param string $name
242
     * @param mixed $value
243
     *
244
     * @deprecated Will be removed in the next major version in favor of
245
     * changing `setCustomProperty` to use dot notation.
246
     */
247
    public function setNestedCustomProperty(string $name, $value)
248
    {
249
        // We need to assign `custom_properties` to a variable so we can
250
        // modify it by reference.
251
        $customProperties = $this->custom_properties;
252
253
        array_set($customProperties, $name, $value);
254
255
        $this->custom_properties = $customProperties;
256
    }
257
258
    /**
259
     * @param string $name
260
     *
261
     * @return $this
262
     */
263
    public function forgetCustomProperty(string $name)
264
    {
265
        if ($this->hasCustomProperty($name)) {
266
            $customProperties = $this->custom_properties;
267
268
            unset($customProperties[$name]);
269
270
            $this->custom_properties = $customProperties;
271
        }
272
273
        return $this;
274
    }
275
276
    /**
277
     * @param string $name
278
     *
279
     * @return $this
280
     *
281
     * @deprecated Will be renamed to `forgetCustomProperty` in the next
282
     * major version.
283
     */
284
    public function removeCustomProperty(string $name)
285
    {
286
        return $this->forgetCustomProperty($name);
287
    }
288
289
    /**
290
     * Forget a custom property using dot notation.
291
     *
292
     * @param string $name
293
     *
294
     * @deprecated Will be removed in the next major version in favor of
295
     * changing `forgetCustomProperty` to use dot notation.
296
     */
297
    public function forgetNestedCustomProperty(string $name)
298
    {
299
        // We need to assign `custom_properties` to a variable so we can
300
        // modify it by reference.
301
        $customProperties = $this->custom_properties;
302
303
        array_forget($customProperties, $name);
304
305
        $this->custom_properties = $customProperties;
306
    }
307
308
    /*
309
     * Get all the names of the registered media conversions.
310
     */
311
    public function getMediaConversionNames(): array
312
    {
313
        $conversions = ConversionCollection::createForMedia($this);
314
315
        return $conversions->map(function (Conversion $conversion) {
316
            return $conversion->getName();
317
        })->toArray();
318
    }
319
}
320