-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathViewUpdateInput.php
More file actions
166 lines (145 loc) · 5.45 KB
/
ViewUpdateInput.php
File metadata and controls
166 lines (145 loc) · 5.45 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Tables\Model;
use Generator;
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Constants\ViewUpdatableParameters;
use OCA\Tables\Service\ValueObject\Emoji;
use OCA\Tables\Service\ValueObject\Title;
use OCA\Tables\Service\ValueObject\ViewColumnInformation;
use OCP\Server;
use Psr\Log\LoggerInterface;
use function json_encode;
class ViewUpdateInput {
protected ?array $sort = null;
public function __construct(
protected readonly ?Title $title = null,
protected readonly ?string $description = null,
protected readonly ?Emoji $emoji = null,
protected readonly ?ColumnSettings $columnSettings = null,
protected readonly ?FilterSet $filterSet = null,
protected readonly ?SortRuleSet $sortRuleSet = null,
protected readonly ?string $layout = null,
protected readonly ?ViewSettings $viewSettings = null,
) {
}
public function updateDetail(): Generator {
if ($this->title) {
yield ViewUpdatableParameters::TITLE => $this->title;
}
if ($this->description) {
yield ViewUpdatableParameters::DESCRIPTION => $this->description;
}
if ($this->emoji) {
yield ViewUpdatableParameters::EMOJI => $this->emoji;
}
if ($this->columnSettings) {
yield ViewUpdatableParameters::COLUMN_SETTINGS => $this->columnSettings;
}
if ($this->filterSet) {
yield ViewUpdatableParameters::FILTER => $this->filterSet;
}
if ($this->sortRuleSet) {
yield ViewUpdatableParameters::SORT => $this->sortRuleSet;
}
if ($this->layout !== null) {
yield ViewUpdatableParameters::LAYOUT => $this->layout;
}
if ($this->viewSettings !== null) {
yield ViewUpdatableParameters::VIEW_SETTINGS => $this->viewSettings;
}
}
/**
* @param array{
* title?: string,
* emoji?: string,
* description?: string,
* columns?: list<int>,
* columnSettings?: list<array{columnId?: int, order?: int, readonly?: bool, mandatory?: bool}>,
* sort?: list<array{columnId: int, mode: 'ASC'|'DESC'}>,
* layout?: 'table'|'tiles'|'gallery'|null,
* viewSettings?: array{cardBackgroundSource?: int|null, cardTitleSource?: int|null}|string,
* filter?: list<list<array{columnId: int, operator: 'begins-with'|'ends-with'|'contains'|'does-not-contain'|'is-equal'|'is-not-equal'|'is-greater-than'|'is-greater-than-or-equal'|'is-lower-than'|'is-lower-than-or-equal'|'is-empty', value: string|int|float}>>
* } $data
*/
public static function fromInputArray(array $data): self {
$data = self::transformJsonToArrayInPayload($data, ['columnSettings', 'filter', 'sort', 'viewSettings']);
if (isset($data['columns']) && !isset($data['columnSettings'])) {
$logger = Server::get(LoggerInterface::class);
$logger->info('The old columns format is deprecated. Please use the new format with columnId and order properties.', ['app' => Application::APP_ID]);
$value = [];
foreach ($data['columns'] as $order => $columnId) {
$value[] = new ViewColumnInformation($columnId, order: $order);
}
$value = json_encode($value);
$data['columnSettings'] = $value;
}
$layout = self::normalizeLayout($data['layout'] ?? null);
$viewSettings = self::createViewSettingsFromInputData($data);
return new self(
title: ($data['title'] ?? null) ? new Title($data['title']) : null,
description: $data['description'] ?? null,
emoji: ($data['emoji'] ?? null) ? new Emoji($data['emoji']) : null,
columnSettings: ($data['columnSettings'] ?? null) ? ColumnSettings::createViewSettingsFromInputArray($data['columnSettings']) : null,
filterSet: ($data['filter'] ?? null) ? FilterSet::createFromInputArray($data['filter']) : null,
sortRuleSet: ($data['sort'] ?? null) ? SortRuleSet::createFromInputArray($data['sort']) : null,
layout: $layout,
viewSettings: $viewSettings,
);
}
private static function createViewSettingsFromInputData(array $data): ?ViewSettings {
if (array_key_exists('viewSettings', $data)) {
if ($data['viewSettings'] === null) {
return new ViewSettings();
}
if (!is_array($data['viewSettings'])) {
throw new \InvalidArgumentException('Invalid viewSettings value.');
}
return ViewSettings::createFromInputArray($data['viewSettings']);
}
$legacyKeys = ['cardBackgroundSource', 'cardTitleSource'];
$hasLegacySettings = false;
foreach ($legacyKeys as $legacyKey) {
if (array_key_exists($legacyKey, $data)) {
$hasLegacySettings = true;
break;
}
}
if (!$hasLegacySettings) {
return null;
}
return ViewSettings::createFromInputArray([
'cardBackgroundSource' => $data['cardBackgroundSource'] ?? null,
'cardTitleSource' => $data['cardTitleSource'] ?? null,
]);
}
private static function normalizeLayout(mixed $layout): ?string {
if ($layout === null || $layout === '') {
return null;
}
if (!is_string($layout)) {
throw new \InvalidArgumentException('Invalid layout value.');
}
if (!in_array($layout, ['table', 'tiles', 'gallery'], true)) {
throw new \InvalidArgumentException('Invalid layout value.');
}
return $layout;
}
protected static function transformJsonToArrayInPayload(array $input, array $keys): array {
$output = $input;
foreach ($keys as $targetKey) {
if (!isset($input[$targetKey]) || !is_string($input[$targetKey])) {
continue;
}
$decoded = \json_decode($input[$targetKey], true);
if (is_array($decoded)) {
$output[$targetKey] = $decoded;
}
}
return $output;
}
}