-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTable.php
More file actions
188 lines (170 loc) · 5.9 KB
/
Table.php
File metadata and controls
188 lines (170 loc) · 5.9 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
<?php
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Tables\Db;
use JsonSerializable;
use OCA\Tables\Model\Permissions;
use OCA\Tables\Model\SortRuleSet;
use OCA\Tables\ResponseDefinitions;
use OCA\Tables\Service\ValueObject\ColumnOrderInformation;
/**
* @psalm-suppress PropertyNotSetInConstructor
*
* @psalm-import-type TablesTable from ResponseDefinitions
* @psalm-import-type TablesView from ResponseDefinitions
*
* @method getTitle(): string
* @method getId(): int
* @method setTitle(string $title)
* @method getEmoji(): string
* @method setEmoji(string $emoji)
* @method getArchived(): bool
* @method setArchived(bool $archived)
* @method getDescription(): string
* @method setDescription(string $description)
* @method getOwnership(): ?string
* @method setOwnership(string $ownership)
* @method getOwnerDisplayName(): string
* @method setOwnerDisplayName(string $ownerDisplayName)
* @method getIsShared(): bool
* @method setIsShared(bool $isShared)
* @method getOnSharePermissions(): ?Permissions
* @method setOnSharePermissions(Permissions $onSharePermissions)
* @method getHasShares(): bool
* @method setHasShares(bool $hasShares)
* @method getFavorite(): bool
* @method setFavorite(bool $favorite)
* @method getRowsCount(): int
* @method setRowsCount(int $rowsCount)
* @method getColumnsCount(): int
* @method setColumnsCount(int $columnsCount)
* @method getViews(): ?array<TablesView>
* @method setViews(array $views)
* @method getColumns(): array
* @method setColumns(array $columns)
* @method getColumnOrder(): ?string
* @method setColumnOrder(?string $columnOrder)
* @method getSort(): ?string
* @method setSort(?string $sort)
* @method getCreatedBy(): string
* @method setCreatedBy(string $createdBy)
* @method getCreatedAt(): string
* @method setCreatedAt(string $createdAt)
* @method getLastEditBy(): string
* @method setLastEditBy(string $lastEditBy)
* @method getLastEditAt(): string
* @method setLastEditAt(string $lastEditAt)
*/
class Table extends EntitySuper implements JsonSerializable {
protected ?string $title = null;
protected ?string $emoji = null;
protected ?string $ownership = null;
protected ?string $createdBy = null;
protected ?string $createdAt = null;
protected ?string $lastEditBy = null;
protected ?string $lastEditAt = null;
protected bool $archived = false;
protected ?string $description = null;
protected ?string $columnOrder = null; // json
protected ?string $sort = null; // json
// virtual properties
protected ?bool $isShared = null;
protected ?Permissions $onSharePermissions = null;
protected ?bool $hasShares = false;
protected ?bool $favorite = false;
protected ?int $rowsCount = 0;
protected ?int $columnsCount = 0;
protected ?array $views = null;
protected ?array $columns = null;
protected ?string $ownerDisplayName = null;
protected const VIRTUAL_PROPERTIES = ['isShared', 'onSharePermissions', 'hasShares', 'favorite', 'rowsCount', 'columnsCount', 'views', 'columns', 'ownerDisplayName'];
public function __construct() {
$this->addType('id', 'integer');
$this->addType('archived', 'boolean');
}
public function isArchived(): bool {
return $this->archived;
}
/**
* @psalm-return TablesTable
*/
public function jsonSerialize(): array {
return [
'id' => $this->id,
'title' => $this->title ?: '',
'emoji' => $this->emoji,
'ownership' => $this->ownership ?: '',
'ownerDisplayName' => $this->ownerDisplayName ?: '',
'createdBy' => $this->createdBy ?: '',
'createdAt' => $this->createdAt ?: '',
'lastEditBy' => $this->lastEditBy ?: '',
'lastEditAt' => $this->lastEditAt ?: '',
'archived' => $this->archived,
'isShared' => (bool)$this->isShared,
'favorite' => (bool)$this->favorite,
'onSharePermissions' => $this->getSharePermissions()?->jsonSerialize(),
'hasShares' => (bool)$this->hasShares,
'rowsCount' => $this->rowsCount ?: 0,
'columnsCount' => $this->columnsCount ?: 0,
'description' => $this->description ?: '',
'views' => array_values($this->getViewsArray()),
'columnOrder' => array_map(
static fn (ColumnOrderInformation $c) => ['columnId' => $c->getId(), 'order' => $c->getOrder()],
$this->getColumnOrderSettingsArray()
),
'sort' => $this->getSortArray(),
];
}
private function getSharePermissions(): ?Permissions {
return $this->onSharePermissions;
}
/**
* @return TablesView[]
*/
private function getViewsArray(): array {
return $this->getViews() ?: [];
}
/**
* @return int[]
*/
public function getColumnOrderArray(): array {
$columnSettings = $this->getColumnOrderSettingsArray();
usort($columnSettings, static function (ColumnOrderInformation $a, ColumnOrderInformation $b) {
return $a->getOrder() - $b->getOrder();
});
/** @var list<ColumnOrderInformation> $columnSettings */
return array_map(static fn (ColumnOrderInformation $vci): int => $vci->getId(), $columnSettings);
}
/**
* @return list<ColumnOrderInformation>
*/
public function getColumnOrderSettingsArray(): array {
$columns = $this->getArray($this->getColumnOrder());
if (empty($columns)) {
return [];
}
if (is_array($columns[array_key_first($columns)] ?? null)) {
return array_values(array_map(static fn (array $a): ColumnOrderInformation => ColumnOrderInformation::fromArray($a), $columns));
}
$result = [];
foreach ($columns as $index => $columnId) {
$result[] = new ColumnOrderInformation((int)$columnId, order: (int)$index + 1);
}
return $result;
}
/**
* @return list<array{columnId: int, mode: 'ASC'|'DESC'}>
*/
public function getSortArray(): array {
$rawSortRules = $this->getArray($this->getSort());
return SortRuleSet::createFromInputArray($rawSortRules)->jsonSerialize();
}
private function getArray(?string $json): array {
if ($json !== '' && $json !== null && $json !== 'null') {
return \json_decode($json, true) ?? [];
}
return [];
}
}