Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SearchNormalizer
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalize
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 tryNormalizePhoneNumber
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\Identify;
10
11use OCA\Libresign\Service\IdentifyMethodService;
12use OCP\IConfig;
13use OCP\IPhoneNumberUtil;
14
15class SearchNormalizer {
16    public function __construct(
17        private IConfig $config,
18        private IPhoneNumberUtil $phoneNumberUtil,
19    ) {
20    }
21
22    public function normalize(string $search, string $method): string {
23        if (!in_array($method, IdentifyMethodService::IDENTIFY_PHONE_METHODS, true)) {
24            return $search;
25        }
26
27        if (str_starts_with($search, '+')) {
28            return $search;
29        }
30
31        $defaultRegion = $this->config->getSystemValueString('default_phone_region', '');
32        if ($defaultRegion === '') {
33            return $search;
34        }
35
36        $standardFormat = $this->phoneNumberUtil->convertToStandardFormat($search, $defaultRegion);
37
38        return $standardFormat ?? $search;
39    }
40
41    public function tryNormalizePhoneNumber(string $phoneNumber, string $method): ?string {
42        if (!in_array($method, IdentifyMethodService::IDENTIFY_PHONE_METHODS, true)) {
43            return null;
44        }
45
46        $phoneNumber = trim($phoneNumber);
47        if ($phoneNumber === '') {
48            return null;
49        }
50
51        if (str_starts_with($phoneNumber, '+')) {
52            return $phoneNumber;
53        }
54
55        $defaultRegion = $this->config->getSystemValueString('default_phone_region', '');
56        if ($defaultRegion === '') {
57            return null;
58        }
59
60        return $this->phoneNumberUtil->convertToStandardFormat($phoneNumber, $defaultRegion);
61    }
62}