1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Thinktomorrow\AssetLibrary\Models\Asset; |
7
|
|
|
use Thinktomorrow\AssetLibrary\Models\AssetUploader; |
8
|
|
|
use Thinktomorrow\Locale\Locale; |
9
|
|
|
|
10
|
|
|
trait AssetTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @return mixed |
14
|
|
|
*/ |
15
|
21 |
|
public function assets() |
16
|
|
|
{ |
17
|
21 |
|
return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale'); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $type |
22
|
|
|
* @param string|null $locale |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
4 |
|
public function hasFile($type = '', $locale = null): bool |
26
|
|
|
{ |
27
|
4 |
|
$filename = $this->getFilename($type, $locale); |
28
|
|
|
|
29
|
4 |
|
return (bool) $filename && basename($filename) != 'other.png'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $type |
34
|
|
|
* @param string|null $locale |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
6 |
|
public function getFilename($type = '', $locale = null): string |
38
|
|
|
{ |
39
|
6 |
|
return basename($this->getFileUrl($type, '', $locale)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $type |
44
|
|
|
* @param string $size |
45
|
|
|
* @param string|null $locale |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
17 |
|
public function getFileUrl($type = '', $size = '', $locale = null): ?string |
49
|
|
|
{ |
50
|
17 |
|
if ($this->assets->first() === null || $this->assets->first()->pivot === null) { |
|
|
|
|
51
|
2 |
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
17 |
|
$locale = $this->normalizeLocale($locale); |
55
|
|
|
|
56
|
17 |
|
$assets = $this->assets->where('pivot.type', $type); |
57
|
17 |
|
if ($assets->count() > 1) { |
58
|
5 |
|
$assets = $assets->where('pivot.locale', $locale); |
59
|
|
|
} |
60
|
|
|
|
61
|
17 |
|
if ($assets->isEmpty()) { |
62
|
1 |
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
17 |
|
return $assets->first()->getFileUrl($size); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Adds a file to this model, accepts a type and locale to be saved with the file. |
70
|
|
|
* |
71
|
|
|
* @param $file |
72
|
|
|
* @param $type |
73
|
|
|
* @param string|null $locale |
74
|
|
|
*/ |
75
|
8 |
|
public function addFile($file, $type = '', $locale = null): void |
76
|
|
|
{ |
77
|
8 |
|
$locale = $this->normalizeLocale($locale); |
78
|
|
|
|
79
|
8 |
|
$asset = AssetUploader::upload($file); |
80
|
8 |
|
if($asset instanceof Asset){ |
81
|
8 |
|
$asset->attachToModel($this, $type, $locale); |
82
|
|
|
} |
83
|
8 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds multiple files to this model, accepts a type and locale to be saved with the file. |
87
|
|
|
* |
88
|
|
|
* @param $files |
89
|
|
|
* @param $type |
90
|
|
|
* @param string|null $locale |
91
|
|
|
*/ |
92
|
3 |
|
public function addFiles($files, $type = '', $locale = null): void |
93
|
|
|
{ |
94
|
3 |
|
$files = (array) $files; |
95
|
3 |
|
$locale = $this->normalizeLocale($locale); |
96
|
|
|
|
97
|
3 |
|
$asset = AssetUploader::upload($files); |
98
|
|
|
|
99
|
3 |
|
collect($asset)->each->attachToModel($this, $type, $locale); |
100
|
3 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function getAllImages() |
106
|
|
|
{ |
107
|
1 |
|
$images = $this->assets->filter(function ($asset) { |
108
|
1 |
|
return $asset->getExtensionForFilter() === 'image'; |
109
|
1 |
|
}); |
110
|
|
|
|
111
|
1 |
|
return $images; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Removes an asset completely. |
116
|
|
|
* |
117
|
|
|
* @param $ids |
118
|
|
|
*/ |
119
|
1 |
|
public function deleteAsset($ids): void |
120
|
|
|
{ |
121
|
1 |
|
Asset::remove($ids); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Remove the asset and attaches a new one. |
126
|
|
|
* |
127
|
|
|
* @param $replace |
128
|
|
|
* @param $with |
129
|
|
|
*/ |
130
|
1 |
|
public function replace($replace, $with): void |
131
|
|
|
{ |
132
|
1 |
|
$asset = Asset::findOrFail($replace); |
133
|
1 |
|
Asset::remove($replace); |
134
|
|
|
|
135
|
1 |
|
$this->addFile(Asset::findOrFail($with), $asset->type, $asset->locale); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param null $type |
140
|
|
|
* @param string|null $locale |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
4 |
|
public function getAllFiles($type = null, $locale = null) |
144
|
|
|
{ |
145
|
4 |
|
$locale = $this->normalizeLocale($locale); |
146
|
|
|
|
147
|
4 |
|
$files = $this->assets->where('pivot.type', $type)->where('pivot.locale', $locale); |
148
|
|
|
|
149
|
4 |
|
return $files; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string|null $locale |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
20 |
|
private function normalizeLocale($locale = null): string |
157
|
|
|
{ |
158
|
20 |
|
$locale = $locale ?? Locale::getDefault(); |
159
|
|
|
|
160
|
20 |
|
return $locale; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.