|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Media; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Collections\Collection; |
|
6
|
|
|
use ByTIC\MediaLibrary\Collections\Traits\LoadMediaTrait; |
|
7
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
|
8
|
|
|
use ByTIC\MediaLibrary\PathGenerator\PathGeneratorFactory; |
|
9
|
|
|
use Nip\Records\Record; |
|
10
|
|
|
use function Nip\url; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Media. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Media |
|
16
|
|
|
{ |
|
17
|
|
|
use Traits\FileMethodsTrait; |
|
18
|
|
|
use Traits\HasConversionsTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Record |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $model; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Collection |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $collection; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return Record|HasMediaTrait |
|
32
|
|
|
*/ |
|
33
|
8 |
|
public function getModel(): Record |
|
34
|
|
|
{ |
|
35
|
8 |
|
return $this->model; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Record|HasMediaTrait $record |
|
40
|
|
|
*/ |
|
41
|
9 |
|
public function setModel(Record $record) |
|
42
|
|
|
{ |
|
43
|
9 |
|
$this->model = $record; |
|
44
|
9 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function getExtension() |
|
50
|
|
|
{ |
|
51
|
4 |
|
return pathinfo($this->getName(), PATHINFO_EXTENSION); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $conversionName |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function getBasePath(string $conversionName = '') |
|
59
|
|
|
{ |
|
60
|
2 |
|
$path = PathGeneratorFactory::create()::getBasePathForMedia($this); |
|
61
|
2 |
|
if ($conversionName) { |
|
62
|
2 |
|
$path = $path . DIRECTORY_SEPARATOR . $conversionName; |
|
63
|
|
|
} |
|
64
|
2 |
|
return $path; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
* |
|
70
|
|
|
* @deprecated Use getFullUrl |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __toString() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->getFullUrl(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the full url to a original media file. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $conversionName |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getFullUrl(string $conversionName = ''): string |
|
85
|
|
|
{ |
|
86
|
|
|
return url()->to($this->getUrl($conversionName)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the url to a original media file. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $conversionName |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getUrl(string $conversionName = ''): string |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->hasFile()) { |
|
99
|
|
|
return $this->getFile()->getUrl(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->getCollection()->getDefaultMediaUrl(); |
|
103
|
|
|
// $urlGenerator = UrlGeneratorFactory::createForMedia($this); |
|
104
|
|
|
// if ($conversionName !== '') { |
|
105
|
|
|
//// $conversion = ConversionCollection::createForMedia($this)->getByName($conversionName); |
|
106
|
|
|
//// $urlGenerator->setConversion($conversion); |
|
107
|
|
|
// } |
|
108
|
|
|
// return $urlGenerator->getUrl(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Collection |
|
113
|
|
|
*/ |
|
114
|
8 |
|
public function getCollection(): Collection |
|
115
|
|
|
{ |
|
116
|
8 |
|
return $this->collection; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param Collection|LoadMediaTrait $collection |
|
121
|
|
|
*/ |
|
122
|
8 |
|
public function setCollection(Collection $collection) |
|
123
|
|
|
{ |
|
124
|
8 |
|
$this->collection = $collection; |
|
125
|
8 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function isDefault() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this === $this->getCollection()->getDefaultMedia(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.