-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathFormattingConditionGroupInput.php
More file actions
75 lines (62 loc) 路 2.22 KB
/
Copy pathFormattingConditionGroupInput.php
File metadata and controls
75 lines (62 loc) 路 2.22 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Tables\Model;
use InvalidArgumentException;
class FormattingConditionGroupInput {
private const VALID_OPERATORS = [
'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'between',
'contains', 'startsWith', 'isEmpty', 'isNotEmpty',
'in', 'before', 'after', 'isToday', 'isThisWeek',
'isTrue', 'isFalse',
];
private const MAX_CONDITIONS = 20;
/** @param list<array{columnId: int, columnType: string, operator: string, value?: scalar, values?: list<float|int|string>}> $conditions */
private function __construct(
private readonly array $conditions,
) {
}
public static function createFromInputArray(array $data): self {
if (!isset($data['conditions']) || !is_array($data['conditions'])) {
throw new InvalidArgumentException('conditions must be an array');
}
if (count($data['conditions']) > self::MAX_CONDITIONS) {
throw new InvalidArgumentException('Max ' . self::MAX_CONDITIONS . ' conditions per group');
}
$conditions = [];
foreach ($data['conditions'] as $raw) {
if (!is_array($raw)) {
throw new InvalidArgumentException('Each condition must be an array');
}
if (!isset($raw['columnId'], $raw['columnType'], $raw['operator'])) {
throw new InvalidArgumentException('Condition requires columnId, columnType and operator');
}
if (!in_array((string)$raw['operator'], self::VALID_OPERATORS, true)) {
throw new InvalidArgumentException('Unknown operator: ' . $raw['operator']);
}
$condition = [
'columnId' => (int)$raw['columnId'],
'columnType' => (string)$raw['columnType'],
'operator' => (string)$raw['operator'],
];
if (array_key_exists('value', $raw)) {
$condition['value'] = $raw['value'];
}
if (array_key_exists('values', $raw) && is_array($raw['values'])) {
$condition['values'] = array_values($raw['values']);
}
$conditions[] = $condition;
}
return new self($conditions);
}
public function toArray(): array {
return ['conditions' => $this->conditions];
}
/** @return int[] */
public function collectColumnIds(): array {
return array_column($this->conditions, 'columnId');
}
}