1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns; |
4
|
|
|
|
5
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadMode; |
6
|
|
|
use Mostafaznv\Larupload\Storage\Proxy\AttachmentProxy; |
7
|
|
|
use stdClass; |
8
|
|
|
use Mostafaznv\Larupload\Storage\Attachment; |
9
|
|
|
|
10
|
|
|
trait LaruploadTransformers |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Handle the dynamic setting of attachment objects |
14
|
|
|
* |
15
|
|
|
* @param string $key |
16
|
|
|
* @param mixed $value |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function setAttribute($key, $value): void |
20
|
|
|
{ |
21
|
|
|
if ($attachment = $this->getAttachment($key)) { |
22
|
|
|
$attachment->attach($value); |
23
|
|
|
} |
24
|
|
|
else { |
25
|
|
|
parent::setAttribute($key, $value); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handle the dynamic retrieval of attachment objects |
31
|
|
|
* |
32
|
|
|
* @param string $key |
33
|
|
|
* @return mixed|null |
34
|
|
|
*/ |
35
|
|
|
public function getAttribute($key): mixed |
36
|
|
|
{ |
37
|
|
|
if ($attachment = $this->getAttachment($key)) { |
38
|
|
|
return new AttachmentProxy($attachment); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return parent::getAttribute($key); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get All styles (original, cover and ...) of entities for this model |
46
|
|
|
* |
47
|
|
|
* @param string|null $name |
48
|
|
|
* @return object|null |
49
|
|
|
*/ |
50
|
|
|
public function getAttachments(string $name = null): object|null |
51
|
|
|
{ |
52
|
|
|
if ($name) { |
53
|
|
|
if ($attachment = $this->getAttachment($name)) { |
54
|
|
|
return $attachment->urls(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
else { |
60
|
|
|
$attachments = new stdClass(); |
61
|
|
|
|
62
|
|
|
foreach ($this->attachments as $attachment) { |
63
|
|
|
$attachments->{$attachment->getName()} = $attachment->urls(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $attachments; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @return AttachmentProxy|null |
73
|
|
|
*/ |
74
|
|
|
public function attachment(string $name): ?AttachmentProxy |
75
|
|
|
{ |
76
|
|
|
if ($attachment = $this->getAttachment($name)) { |
77
|
|
|
return new AttachmentProxy($attachment); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Override toArray method |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function toArray(): array |
89
|
|
|
{ |
90
|
|
|
$array = $this->hideLaruploadColumns(parent::toArray()); |
91
|
|
|
|
92
|
|
|
// attach attachment entities to array/json response |
93
|
|
|
foreach ($this->getAttachments() as $name => $attachment) { |
94
|
|
|
$array[$name] = $attachment; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $array; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieve attachment if exists, otherwise return null |
102
|
|
|
* |
103
|
|
|
* @param $name |
104
|
|
|
* @return Attachment|null |
105
|
|
|
*/ |
106
|
|
|
private function getAttachment($name): ?Attachment |
107
|
|
|
{ |
108
|
|
|
foreach ($this->attachments as $attachment) { |
109
|
|
|
if ($attachment->getName() == $name) { |
110
|
|
|
return $attachment; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Hide larupload columns from toArray function |
119
|
|
|
* |
120
|
|
|
* @param array $array |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
private function hideLaruploadColumns(array $array): array |
124
|
|
|
{ |
125
|
|
|
if ($this->hideLaruploadColumns) { |
126
|
|
|
foreach ($this->attachments as $attachment) { |
127
|
|
|
$name = $attachment->getName(); |
128
|
|
|
|
129
|
|
|
unset($array["{$name}_file_name"]); |
130
|
|
|
|
131
|
|
|
if ($attachment->getMode() === LaruploadMode::HEAVY) { |
132
|
|
|
unset($array["{$name}_file_id"]); |
133
|
|
|
unset($array["{$name}_file_original_name"]); |
134
|
|
|
unset($array["{$name}_file_size"]); |
135
|
|
|
unset($array["{$name}_file_type"]); |
136
|
|
|
unset($array["{$name}_file_mime_type"]); |
137
|
|
|
unset($array["{$name}_file_width"]); |
138
|
|
|
unset($array["{$name}_file_height"]); |
139
|
|
|
unset($array["{$name}_file_duration"]); |
140
|
|
|
unset($array["{$name}_file_dominant_color"]); |
141
|
|
|
unset($array["{$name}_file_format"]); |
142
|
|
|
unset($array["{$name}_file_cover"]); |
143
|
|
|
} |
144
|
|
|
else { |
145
|
|
|
unset($array["{$name}_file_meta"]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $array; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|