1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Upload; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use stdClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class resource upload |
10
|
|
|
*/ |
11
|
|
|
abstract class Resource |
12
|
|
|
{ |
13
|
|
|
use TraitUpload; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var object|array|null |
17
|
|
|
*/ |
18
|
|
|
private $files; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private string $path; |
23
|
|
|
/** |
24
|
|
|
* @var array|null |
25
|
|
|
*/ |
26
|
|
|
private ?array $mimeType; |
27
|
|
|
/** |
28
|
|
|
* @var array|object |
29
|
|
|
*/ |
30
|
|
|
private array|object $data; |
31
|
|
|
/** |
32
|
|
|
* @var object |
33
|
|
|
*/ |
34
|
|
|
private object $response; |
35
|
|
|
/** |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected ?string $url; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var stdClass |
42
|
|
|
*/ |
43
|
|
|
protected stdClass $file; |
44
|
|
|
/** |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
protected ?string $key; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
public function __construct(?string $url = null, ?string $key = null) |
53
|
|
|
{ |
54
|
|
|
$this->url = $url; |
55
|
|
|
$this->key = $key ?? 'file'; |
56
|
|
|
$this->setFiles(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return object|null |
61
|
|
|
*/ |
62
|
|
|
protected function getFiles(): ?object |
63
|
|
|
{ |
64
|
|
|
$upload = (object) $this->files; |
65
|
|
|
if(isset($upload->upload_file) && isset($upload->upload_url)){ |
66
|
|
|
$key = $this->key; |
67
|
|
|
$upload->upload_file->$key = $upload->upload_url->$key; |
68
|
|
|
$this->files = $upload->upload_file; |
69
|
|
|
unset($upload->upload_file, $upload->upload_url); |
70
|
|
|
} |
71
|
|
|
if(isset($upload->upload_file)){ |
72
|
|
|
$this->files = $upload->upload_file; |
73
|
|
|
unset($upload->upload_file); |
74
|
|
|
} |
75
|
|
|
if(isset($upload->upload_url)){ |
76
|
|
|
$this->files = $upload->upload_url; |
77
|
|
|
unset($upload->upload_url); |
78
|
|
|
} |
79
|
|
|
return (object) $this->files; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* convert in object and count files |
84
|
|
|
*/ |
85
|
|
|
private function setFiles(): bool |
86
|
|
|
{ |
87
|
|
|
$this->setMimeType(); |
88
|
|
|
if (!empty($_FILES)) { |
89
|
|
|
$this->uploadFiles(); |
90
|
|
|
} |
91
|
|
|
if ($this->url) { |
92
|
|
|
$this->uploadUrl(); |
93
|
|
|
} |
94
|
|
|
if (isset($this->files)) { |
95
|
|
|
$this->setResponse(200, "success", "defined attribute", "upload"); |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string|null |
103
|
|
|
*/ |
104
|
|
|
private function getPath(): ?string |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
return $this->path; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $type |
111
|
|
|
*/ |
112
|
|
|
private function setPath(string $type): void |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$this->path = UPLOAD_DIR . "/$type/" . date('Y') . "/" . date('m') . "/" . date('d'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array|null |
119
|
|
|
*/ |
120
|
|
|
private function getMimeType(): ?array |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return $this->mimeType; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* valide mimetype accept |
127
|
|
|
*/ |
128
|
|
|
private function setMimeType(): void |
129
|
|
|
{ |
130
|
|
|
$this->mimeType = UPLOAD_MIMETYPE; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return object |
135
|
|
|
*/ |
136
|
|
|
protected function getData(): object |
137
|
|
|
{ |
138
|
|
|
return (object)$this->data; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $key |
143
|
|
|
* @param string $value |
144
|
|
|
*/ |
145
|
|
|
protected function setData(string $key, string $value): void |
146
|
|
|
{ |
147
|
|
|
$this->data[$key] = $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return object |
152
|
|
|
*/ |
153
|
|
|
protected function getResponse(): object |
154
|
|
|
{ |
155
|
|
|
if(isset($this->response)){ |
156
|
|
|
return $this->response; |
157
|
|
|
} |
158
|
|
|
$this->setResponse( |
159
|
|
|
404, |
160
|
|
|
"error", |
161
|
|
|
'response must not be accessed before initialization ' . __METHOD__, |
162
|
|
|
"upload", |
163
|
|
|
dynamic: __METHOD__ |
164
|
|
|
); |
165
|
|
|
return $this->getResponse(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param int $code |
170
|
|
|
* @param string $type |
171
|
|
|
* @param string $text |
172
|
|
|
* @param string $model |
173
|
|
|
* @param object|null $data |
174
|
|
|
* @param string|null $dynamic |
175
|
|
|
*/ |
176
|
|
|
protected function setResponse(int $code, string $type, string $text, string $model, ?object $data = null, ?string $dynamic = null): void |
177
|
|
|
{ |
178
|
|
|
http_response_code($code); |
179
|
|
|
$this->response = (object)[ |
180
|
|
|
"code" => $code, |
181
|
|
|
"type" => $type, |
182
|
|
|
"text" => $text, |
183
|
|
|
"model" => $model, |
184
|
|
|
"data" => $data, |
185
|
|
|
"dynamic" => $dynamic |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
} |
This check looks for private methods that have been defined, but are not used inside the class.