Skip to content

Commit 009f128

Browse files
committed
refactor(php): inject IL10N into FieldValueService and translate throws with literal strings
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 4ef29c1 commit 009f128

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

lib/Service/FieldValueService.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OCA\ProfileFields\Workflow\Event\ProfileFieldVisibilityUpdatedEvent;
2525
use OCA\ProfileFields\Workflow\ProfileFieldValueWorkflowSubject;
2626
use OCP\EventDispatcher\IEventDispatcher;
27+
use OCP\IL10N;
2728

2829
class FieldValueService {
2930
private const SEARCH_OPERATOR_EQ = 'eq';
@@ -33,6 +34,7 @@ class FieldValueService {
3334
public function __construct(
3435
private FieldValueMapper $fieldValueMapper,
3536
private IEventDispatcher $eventDispatcher,
37+
private IL10N $l10n,
3638
) {
3739
}
3840

@@ -51,7 +53,7 @@ public function upsert(
5153
$valueJson = $this->encodeValue($normalizedValue);
5254
$visibility = $currentVisibility ?? FieldExposurePolicy::from($definition->getExposurePolicy())->initialVisibility()->value;
5355
if (!FieldVisibility::isValid($visibility)) {
54-
throw new InvalidArgumentException('current_visibility is not supported');
56+
throw new InvalidArgumentException($this->l10n->t('current_visibility is not supported'));
5557
}
5658

5759
$entity = $this->fieldValueMapper->findByFieldDefinitionIdAndUserUid($definition->getId(), $userUid) ?? new FieldValue();
@@ -143,16 +145,16 @@ public function searchByDefinition(
143145
int $offset,
144146
): array {
145147
if ($limit < 1 || $limit > self::SEARCH_MAX_LIMIT) {
146-
throw new InvalidArgumentException(sprintf('limit must be between 1 and %d', self::SEARCH_MAX_LIMIT));
148+
throw new InvalidArgumentException($this->l10n->t('limit must be between 1 and %d', [self::SEARCH_MAX_LIMIT]));
147149
}
148150

149151
if ($offset < 0) {
150-
throw new InvalidArgumentException('offset must be greater than or equal to 0');
152+
throw new InvalidArgumentException($this->l10n->t('offset must be greater than or equal to 0'));
151153
}
152154

153155
$normalizedOperator = strtolower(trim($operator));
154156
if (!in_array($normalizedOperator, [self::SEARCH_OPERATOR_EQ, self::SEARCH_OPERATOR_CONTAINS], true)) {
155-
throw new InvalidArgumentException('search operator is not supported');
157+
throw new InvalidArgumentException($this->l10n->t('search operator is not supported'));
156158
}
157159

158160
$searchValue = $this->normalizeSearchValue($definition, $normalizedOperator, $rawValue);
@@ -175,12 +177,12 @@ public function searchByDefinition(
175177

176178
public function updateVisibility(FieldDefinition $definition, string $userUid, string $updatedByUid, string $currentVisibility): FieldValue {
177179
if (!FieldVisibility::isValid($currentVisibility)) {
178-
throw new InvalidArgumentException('current_visibility is not supported');
180+
throw new InvalidArgumentException($this->l10n->t('current_visibility is not supported'));
179181
}
180182

181183
$entity = $this->fieldValueMapper->findByFieldDefinitionIdAndUserUid($definition->getId(), $userUid);
182184
if ($entity === null) {
183-
throw new InvalidArgumentException('field value not found');
185+
throw new InvalidArgumentException($this->l10n->t('field value not found'));
184186
}
185187

186188
$previousValue = $this->extractScalarValue($entity->getValueJson());
@@ -227,7 +229,7 @@ public function serializeForResponse(FieldValue $value): array {
227229
*/
228230
private function normalizeTextValue(array|string|int|float|bool $rawValue): array {
229231
if (is_array($rawValue)) {
230-
throw new InvalidArgumentException('text fields expect a scalar value');
232+
throw new InvalidArgumentException($this->l10n->t('text fields expect a scalar value'));
231233
}
232234

233235
return ['value' => trim((string)$rawValue)];
@@ -239,13 +241,13 @@ private function normalizeTextValue(array|string|int|float|bool $rawValue): arra
239241
*/
240242
private function normalizeSelectValue(array|string|int|float|bool $rawValue, FieldDefinition $definition): array {
241243
if (!is_string($rawValue)) {
242-
throw new InvalidArgumentException('select fields expect a string value');
244+
throw new InvalidArgumentException($this->l10n->t('select fields expect a string value'));
243245
}
244246

245247
$value = trim($rawValue);
246248
$options = json_decode($definition->getOptions() ?? '[]', true);
247249
if (!in_array($value, $options, true)) {
248-
throw new InvalidArgumentException(sprintf('"%s" is not a valid option for this field', $value));
250+
throw new InvalidArgumentException($this->l10n->t('"%s" is not a valid option for this field', [$value]));
249251
}
250252

251253
return ['value' => $value];
@@ -257,7 +259,7 @@ private function normalizeSelectValue(array|string|int|float|bool $rawValue, Fie
257259
*/
258260
private function normalizeNumberValue(array|string|int|float|bool $rawValue): array {
259261
if (is_array($rawValue) || is_bool($rawValue) || !is_numeric($rawValue)) {
260-
throw new InvalidArgumentException('number fields expect a numeric value');
262+
throw new InvalidArgumentException($this->l10n->t('number fields expect a numeric value'));
261263
}
262264

263265
return ['value' => str_contains((string)$rawValue, '.') ? (float)$rawValue : (int)$rawValue];
@@ -270,7 +272,7 @@ private function encodeValue(array $value): string {
270272
try {
271273
return json_encode($value, JSON_THROW_ON_ERROR);
272274
} catch (JsonException $exception) {
273-
throw new InvalidArgumentException('value_json could not be encoded', 0, $exception);
275+
throw new InvalidArgumentException($this->l10n->t('value_json could not be encoded'), 0, $exception);
274276
}
275277
}
276278

@@ -281,11 +283,11 @@ private function decodeValue(string $valueJson): array {
281283
try {
282284
$decoded = json_decode($valueJson, true, 512, JSON_THROW_ON_ERROR);
283285
} catch (JsonException $exception) {
284-
throw new InvalidArgumentException('value_json could not be decoded', 0, $exception);
286+
throw new InvalidArgumentException($this->l10n->t('value_json could not be decoded'), 0, $exception);
285287
}
286288

287289
if (!is_array($decoded)) {
288-
throw new InvalidArgumentException('value_json must decode to an object payload');
290+
throw new InvalidArgumentException($this->l10n->t('value_json must decode to an object payload'));
289291
}
290292

291293
return $decoded;
@@ -326,13 +328,13 @@ private function normalizeSearchValue(FieldDefinition $definition, string $opera
326328
}
327329

328330
if (FieldType::from($definition->getType()) !== FieldType::TEXT) {
329-
throw new InvalidArgumentException('contains operator is only supported for text fields');
331+
throw new InvalidArgumentException($this->l10n->t('contains operator is only supported for text fields'));
330332
}
331333

332334
$normalized = $this->normalizeValue($definition, $rawValue);
333335
$value = $normalized['value'] ?? null;
334336
if (!is_string($value) || $value === '') {
335-
throw new InvalidArgumentException('contains operator requires a non-empty text value');
337+
throw new InvalidArgumentException($this->l10n->t('contains operator requires a non-empty text value'));
336338
}
337339

338340
return ['value' => $value];

0 commit comments

Comments
 (0)