1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Collections\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Loaders\AbstractLoader; |
6
|
|
|
use ByTIC\MediaLibrary\Loaders\Filesystem; |
7
|
|
|
use ByTIC\MediaLibrary\Loaders\HasLoaderTrait; |
8
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
9
|
|
|
use ByTIC\MediaLibrary\MediaRepository\HasMediaRepositoryTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait LoadMediaTrait. |
13
|
|
|
*/ |
14
|
|
|
trait LoadMediaTrait |
15
|
|
|
{ |
16
|
|
|
use HasLoaderTrait; |
17
|
|
|
use HasMediaRepositoryTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $mediaType = 'files'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $mediaLoaded = false; |
28
|
|
|
|
29
|
7 |
|
protected function doLoad(): void |
30
|
|
|
{ |
31
|
7 |
|
$this->loadMedia(); |
32
|
7 |
|
} |
33
|
|
|
|
34
|
7 |
|
public function loadMedia() |
35
|
|
|
{ |
36
|
7 |
|
if ($this->isMediaLoaded()) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
7 |
|
$this->getLoader()->loadMedia(); |
41
|
|
|
|
42
|
7 |
|
$this->setMediaLoaded(true); |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
7 |
|
public function isMediaLoaded(): bool |
49
|
|
|
{ |
50
|
7 |
|
return $this->mediaLoaded; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param bool $mediaLoaded |
55
|
|
|
*/ |
56
|
7 |
|
public function setMediaLoaded(bool $mediaLoaded) |
57
|
|
|
{ |
58
|
7 |
|
$this->mediaLoaded = $mediaLoaded; |
59
|
7 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Media |
63
|
|
|
*/ |
64
|
8 |
|
public function newMedia() |
65
|
|
|
{ |
66
|
8 |
|
$mediaFile = new Media(); |
67
|
8 |
|
$mediaFile->setCollection($this); |
68
|
8 |
|
$mediaFile->setModel($this->getMediaRepository()->getRecord()); |
69
|
|
|
|
70
|
8 |
|
return $mediaFile; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Append a media object inside the collection. |
75
|
|
|
* |
76
|
|
|
* @param Media $media |
77
|
|
|
* @param AbstractLoader $loader |
78
|
|
|
*/ |
79
|
5 |
|
public function appendMediaFromLoader(Media $media, $loader) |
80
|
|
|
{ |
81
|
5 |
|
$method = 'validateMediaFromLoader'; |
82
|
5 |
|
$manager = $this->getRecord()->getManager(); |
|
|
|
|
83
|
5 |
|
if (!method_exists($manager, $method)) { |
84
|
5 |
|
$this->appendMedia($media); |
85
|
|
|
|
86
|
5 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($manager->$method($media, $loader)) { |
90
|
|
|
$this->appendMedia($media); |
91
|
|
|
|
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Append a media object inside the collection. |
98
|
|
|
* |
99
|
|
|
* @param Media $media |
100
|
|
|
*/ |
101
|
5 |
|
public function appendMedia(Media $media) |
102
|
|
|
{ |
103
|
5 |
|
$this->items[$media->getName()] = $media; |
|
|
|
|
104
|
5 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
11 |
|
public function getMediaType() |
110
|
|
|
{ |
111
|
11 |
|
return $this->mediaType; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $mediaType |
116
|
|
|
*/ |
117
|
11 |
|
public function setMediaType($mediaType) |
118
|
|
|
{ |
119
|
11 |
|
$this->mediaType = $mediaType; |
120
|
11 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param AbstractLoader $loader |
124
|
|
|
* |
125
|
|
|
* @return AbstractLoader |
126
|
|
|
*/ |
127
|
|
|
protected function hydrateLoader($loader) |
128
|
|
|
{ |
129
|
|
|
$loader->setCollection($this); |
130
|
|
|
$loader->setFilesystem($this->getFilesystem()); |
|
|
|
|
131
|
|
|
|
132
|
|
|
return $loader; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
7 |
|
protected function getLoaderClass() |
139
|
|
|
{ |
140
|
7 |
|
return Filesystem::class; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|