-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathEShapeType.php
More file actions
238 lines (226 loc) · 8.78 KB
/
Copy pathEShapeType.php
File metadata and controls
238 lines (226 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\TaskProcessing;
use OCP\AppFramework\Attribute\Consumable;
use OCP\TaskProcessing\Exception\ValidationException;
/**
* The input and output Shape types
*
* @since 30.0.0
*/
#[Consumable(since: '30.0.0')]
enum EShapeType: int {
/**
* @since 30.0.0
*/
case Number = 0;
/**
* @since 30.0.0
*/
case Text = 1;
/**
* @since 30.0.0
*/
case Image = 2;
/**
* @since 30.0.0
*/
case Audio = 3;
/**
* @since 30.0.0
*/
case Video = 4;
/**
* @since 30.0.0
*/
case File = 5;
/**
* @since 30.0.0
*/
case Enum = 6;
/**
* @since 30.0.0
*/
case ListOfNumbers = 10;
/**
* @since 30.0.0
*/
case ListOfTexts = 11;
/**
* @since 30.0.0
*/
case ListOfImages = 12;
/**
* @since 30.0.0
*/
case ListOfAudios = 13;
/**
* @since 30.0.0
*/
case ListOfVideos = 14;
/**
* @since 30.0.0
*/
case ListOfFiles = 15;
/**
* @param mixed $value
* @param ShapeEnumValue[] $enumValues
* @return void
* @throws ValidationException
* @since 30.0.0
*/
public function validateEnum(mixed $value, array $enumValues): void {
if ($this !== EShapeType::Enum) {
throw new ValidationException('Provider provided enum values for non-enum slot');
}
foreach ($enumValues as $enumValue) {
if ($value === $enumValue->getValue()) {
return;
}
}
throw new ValidationException('Wrong value given for Enum slot. Got "' . $value . '", but expected one of the provided enum values: "' . implode('", "', array_map(fn ($enumValue) => $enumValue->getValue(), $enumValues)) . '"');
}
/**
* @param mixed $value
* @return void
* @throws ValidationException
* @since 30.0.0
*/
private function validateNonFileType(mixed $value): void {
if ($this === EShapeType::Enum && !is_string($value)) {
throw new ValidationException('Non-text item provided for Enum slot');
}
if ($this === EShapeType::Text && !is_string($value)) {
throw new ValidationException('Non-text item provided for Text slot');
}
if ($this === EShapeType::ListOfTexts && (!is_array($value) || count(array_filter($value, fn ($item) => !is_string($item))) > 0)) {
throw new ValidationException('Non-text list item provided for ListOfTexts slot');
}
if ($this === EShapeType::Number && !is_numeric($value)) {
throw new ValidationException('Non-numeric item provided for Number slot');
}
if ($this === EShapeType::ListOfNumbers && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-numeric list item provided for ListOfNumbers slot');
}
}
/**
* @param mixed $value
* @return void
* @throws Exception\ValidationException
* @since 30.0.0
*/
public function validateInput(mixed $value): void {
$this->validateNonFileType($value);
if ($this === EShapeType::Text && is_string($value) && strlen($value) > 512_000) {
throw new ValidationException('Text is too long');
}
if ($this === EShapeType::Image && !is_numeric($value)) {
throw new ValidationException('Non-image item provided for Image slot');
}
if ($this === EShapeType::ListOfImages && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-image list item provided for ListOfImages slot');
}
if ($this === EShapeType::Audio && !is_numeric($value)) {
throw new ValidationException('Non-audio item provided for Audio slot');
}
if ($this === EShapeType::ListOfAudios && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-audio list item provided for ListOfAudio slot');
}
if ($this === EShapeType::Video && !is_numeric($value)) {
throw new ValidationException('Non-video item provided for Video slot');
}
if ($this === EShapeType::ListOfVideos && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-video list item provided for ListOfTexts slot');
}
if ($this === EShapeType::File && !is_numeric($value)) {
throw new ValidationException('Non-file item provided for File slot');
}
if ($this === EShapeType::ListOfFiles && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-audio list item provided for ListOfFiles slot');
}
}
/**
* @throws ValidationException
* @since 30.0.0
*/
public function validateOutputWithFileData(mixed $value): void {
$this->validateNonFileType($value);
if ($this === EShapeType::Image && !is_string($value) && !($value instanceof FileShaped && $value->getShapeType() === EShapeType::Image)) {
throw new ValidationException('Non-image item provided for Image slot');
}
if ($this === EShapeType::ListOfImages && (!is_array($value) || count(array_filter($value, fn ($item) => !is_string($item) && !($item instanceof FileShaped && $item->getShapeType() === EShapeType::Image))) > 0)) {
throw new ValidationException('Non-image list item provided for ListOfImages slot');
}
if ($this === EShapeType::Audio && !is_string($value) && !($value instanceof FileShaped && $value->getShapeType() === EShapeType::Audio)) {
throw new ValidationException('Non-audio item provided for Audio slot');
}
if ($this === EShapeType::ListOfAudios && (!is_array($value) || count(array_filter($value, fn ($item) => !is_string($item) && !($item instanceof FileShaped && $item->getShapeType() === EShapeType::Audio))) > 0)) {
throw new ValidationException('Non-audio list item provided for ListOfAudio slot');
}
if ($this === EShapeType::Video && !is_string($value) && !($value instanceof FileShaped && $value->getShapeType() === EShapeType::Video)) {
throw new ValidationException('Non-video item provided for Video slot');
}
if ($this === EShapeType::ListOfVideos && (!is_array($value) || count(array_filter($value, fn ($item) => !is_string($item) && !($item instanceof FileShaped && $item->getShapeType() === EShapeType::Video))) > 0)) {
throw new ValidationException('Non-video list item provided for ListOfVideos slot');
}
if ($this === EShapeType::File && !is_string($value) && !($value instanceof FileShaped && $value->getShapeType() === EShapeType::File)) {
throw new ValidationException('Non-file item provided for File slot');
}
if ($this === EShapeType::ListOfFiles && (!is_array($value) || count(array_filter($value, fn ($item) => !is_string($item) && !($item instanceof FileShaped && $item->getShapeType() === EShapeType::File))) > 0)) {
throw new ValidationException('Non-audio list item provided for ListOfFiles slot');
}
}
/**
* @param mixed $value
* @return void
* @throws ValidationException
* @since 30.0.0
*/
public function validateOutputWithFileIds(mixed $value): void {
$this->validateNonFileType($value);
if ($this === EShapeType::Image && !is_numeric($value)) {
throw new ValidationException('Non-image item provided for Image slot');
}
if ($this === EShapeType::ListOfImages && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-image list item provided for ListOfImages slot');
}
if ($this === EShapeType::Audio && !is_numeric($value)) {
throw new ValidationException('Non-audio item provided for Audio slot');
}
if ($this === EShapeType::ListOfAudios && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-audio list item provided for ListOfAudio slot');
}
if ($this === EShapeType::Video && !is_numeric($value)) {
throw new ValidationException('Non-video item provided for Video slot');
}
if ($this === EShapeType::ListOfVideos && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-video list item provided for ListOfTexts slot');
}
if ($this === EShapeType::File && !is_numeric($value)) {
throw new ValidationException('Non-file item provided for File slot');
}
if ($this === EShapeType::ListOfFiles && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
throw new ValidationException('Non-audio list item provided for ListOfFiles slot');
}
}
/**
* @param EShapeType $type
* @return EShapeType
* @since 30.0.0
*/
public static function getScalarType(EShapeType $type): EShapeType {
return EShapeType::from($type->value % 10);
}
/**
* @param EShapeType $type
* @return bool
* @since 30.0.0
*/
public static function isFileType(EShapeType $type): bool {
return in_array(EShapeType::getScalarType($type), [EShapeType::File, EShapeType::Image, EShapeType::Audio, EShapeType::Video], true);
}
}