aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/TaskProcessing/EShapeType.php
blob: f6cfab6b38f4d011af33dd168f070549c9f69185 (plain)
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
<?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\TaskProcessing\Exception\ValidationException;

/**
 * The input and output Shape types
 *
 * @since 30.0.0
 */
enum EShapeType: int {
	case Number = 0;
	case Text = 1;
	case Image = 2;
	case Audio = 3;
	case Video = 4;
	case File = 5;
	case Enum = 6;
	case ListOfNumbers = 10;
	case ListOfTexts = 11;
	case ListOfImages = 12;
	case ListOfAudios = 13;
	case ListOfVideos = 14;
	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::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)) {
			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))) > 0)) {
			throw new ValidationException('Non-image list item provided for ListOfImages slot');
		}
		if ($this === EShapeType::Audio && !is_string($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_string($item))) > 0)) {
			throw new ValidationException('Non-audio list item provided for ListOfAudio slot');
		}
		if ($this === EShapeType::Video && !is_string($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_string($item))) > 0)) {
			throw new ValidationException('Non-video list item provided for ListOfTexts slot');
		}
		if ($this === EShapeType::File && !is_string($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_string($item))) > 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);
	}
}