• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / phone-number-input / 6343574457

28 Sep 2023 07:48PM UTC coverage: 96.429% (+0.09%) from 96.341%
6343574457

push

github

xificurk
Drop useless dev dependency

81 of 84 relevant lines covered (96.43%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/src/PhoneNumberInput/Validator.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\PhoneNumberInput;
5

6
use Brick\PhoneNumber\PhoneNumber;
7
use Brick\PhoneNumber\PhoneNumberParseException;
8
use Nette;
9
use Nette\Forms\Control;
10
use function is_array;
11

12
final class Validator
13
{
14

15
    use Nette\StaticClass;
16

17
    /**
18
     * Does the control value look like a phone number?
19
     * This performs only basic checks (e.g. of the length of the number). For a more strict validation use `validatePhoneNumberStrict()`.
20
     */
21
    public static function validatePhoneNumber(Control $control): bool
1✔
22
    {
23
        $value = self::castToPhoneNumber($control->getValue());
1✔
24
        return $value instanceof PhoneNumber && $value->isPossibleNumber();
1✔
25
    }
26

27
    /**
28
     * Does the control value match a valid phone number pattern?
29
     * This relies on up-to-date metadata in `giggsey/libphonenumber-for-php`.
30
     * This doesn't verify the number is actually in use, which is impossible to tell by just looking at a number itself.
31
     */
32
    public static function validatePhoneNumberStrict(Control $control): bool
1✔
33
    {
34
        $value = self::castToPhoneNumber($control->getValue());
1✔
35
        return $value instanceof PhoneNumber && $value->isValidNumber();
1✔
36
    }
37

38
    /**
39
     * Does the control value contain a phone number from one of the specified regions?
40
     *
41
     * @param string|string[] $allowedRegionCodes
42
     */
43
    public static function validatePhoneNumberRegion(Control $control, string|array $allowedRegionCodes): bool
1✔
44
    {
45
        $allowedRegionCodes = is_array($allowedRegionCodes) ? $allowedRegionCodes : [$allowedRegionCodes];
1✔
46
        $value = self::castToPhoneNumber($control->getValue());
1✔
47
        return $value instanceof PhoneNumber && in_array($value->getRegionCode(), $allowedRegionCodes, true);
1✔
48
    }
49

50
    private static function castToPhoneNumber(mixed $value): ?PhoneNumber
1✔
51
    {
52
        if ($value instanceof PhoneNumber || $value === null) {
1✔
53
            return $value;
1✔
54
        }
55

56
        if (is_int($value)) {
1✔
57
            $value = (string) $value;
×
58
        } elseif (is_object($value) && method_exists($value, '__toString')) {
1✔
59
            $value = $value->__toString();
1✔
60
        }
61

62
        if (! is_string($value)) {
1✔
63
            return null;
1✔
64
        }
65

66
        try {
67
            return PhoneNumber::parse($value);
1✔
68
        } catch (PhoneNumberParseException $exception) {
1✔
69
            return null;
1✔
70
        }
71
    }
72

73
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc