-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldDefinitionService.php
More file actions
126 lines (106 loc) · 4.25 KB
/
FieldDefinitionService.php
File metadata and controls
126 lines (106 loc) · 4.25 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
<?php
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\ProfileFields\Service;
use DateTime;
use InvalidArgumentException;
use JsonException;
use OCA\ProfileFields\Db\FieldDefinition;
use OCA\ProfileFields\Db\FieldDefinitionMapper;
use OCA\ProfileFields\Db\FieldValueMapper;
use OCP\IL10N;
class FieldDefinitionService {
public function __construct(
private FieldDefinitionMapper $fieldDefinitionMapper,
private FieldValueMapper $fieldValueMapper,
private FieldDefinitionValidator $validator,
private IL10N $l10n,
) {
}
/**
* @param array<string, mixed> $definition
*/
public function create(array $definition): FieldDefinition {
$validated = $this->validator->validate($definition);
if ($this->fieldDefinitionMapper->findByFieldKey($validated['field_key']) !== null) {
throw new InvalidArgumentException($this->l10n->t('field_key already exists'));
}
$createdAt = $this->parseImportedDate($definition['created_at'] ?? null) ?? new DateTime();
$updatedAt = $this->parseImportedDate($definition['updated_at'] ?? null) ?? clone $createdAt;
$entity = new FieldDefinition();
$entity->setFieldKey($validated['field_key']);
$entity->setLabel($validated['label']);
$entity->setType($validated['type']);
$entity->setEditPolicy($validated['edit_policy']);
$entity->setExposurePolicy($validated['exposure_policy']);
$entity->setSortOrder($validated['sort_order']);
$entity->setActive($validated['active']);
try {
$entity->setOptions(isset($validated['options']) ? json_encode($validated['options'], JSON_THROW_ON_ERROR) : null);
} catch (JsonException $e) {
throw new InvalidArgumentException($this->l10n->t('options could not be encoded: %s', [$e->getMessage()]), 0, $e);
}
$entity->setCreatedAt($createdAt);
$entity->setUpdatedAt($updatedAt);
return $this->fieldDefinitionMapper->insert($entity);
}
/**
* @param array<string, mixed> $definition
*/
public function update(FieldDefinition $existing, array $definition): FieldDefinition {
$validated = $this->validator->validate($definition + ['field_key' => $existing->getFieldKey()]);
if (($definition['field_key'] ?? $existing->getFieldKey()) !== $existing->getFieldKey()) {
throw new InvalidArgumentException($this->l10n->t('field_key cannot be changed'));
}
if ($validated['type'] !== $existing->getType() && $this->fieldValueMapper->hasValuesForFieldDefinitionId($existing->getId())) {
throw new InvalidArgumentException($this->l10n->t('type cannot be changed after values exist'));
}
$existing->setLabel($validated['label']);
$existing->setType($validated['type']);
$existing->setEditPolicy($validated['edit_policy']);
$existing->setExposurePolicy($validated['exposure_policy']);
$existing->setSortOrder($validated['sort_order']);
$existing->setActive($validated['active']);
try {
$existing->setOptions(isset($validated['options']) ? json_encode($validated['options'], JSON_THROW_ON_ERROR) : null);
} catch (JsonException $e) {
throw new InvalidArgumentException($this->l10n->t('options could not be encoded: %s', [$e->getMessage()]), 0, $e);
}
$existing->setUpdatedAt($this->parseImportedDate($definition['updated_at'] ?? null) ?? new DateTime());
return $this->fieldDefinitionMapper->update($existing);
}
private function parseImportedDate(mixed $value): ?DateTime {
if (!is_string($value) || $value === '') {
return null;
}
return new DateTime($value);
}
/**
* @return list<FieldDefinition>
*/
public function findAllOrdered(): array {
return $this->fieldDefinitionMapper->findAllOrdered();
}
public function findById(int $id): ?FieldDefinition {
return $this->fieldDefinitionMapper->findById($id);
}
public function findByFieldKey(string $fieldKey): ?FieldDefinition {
return $this->fieldDefinitionMapper->findByFieldKey($fieldKey);
}
public function delete(int $id): ?FieldDefinition {
$definition = $this->fieldDefinitionMapper->findById($id);
if ($definition === null) {
return null;
}
return $this->fieldDefinitionMapper->delete($definition);
}
/**
* @return list<FieldDefinition>
*/
public function findActiveOrdered(): array {
return $this->fieldDefinitionMapper->findActiveOrdered();
}
}