• 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

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

4
namespace Nepada\PhoneNumberInput;
5

6
use Brick\PhoneNumber\PhoneNumber;
7
use Brick\PhoneNumber\PhoneNumberFormat;
8
use Brick\PhoneNumber\PhoneNumberParseException;
9
use Nette\Forms\Controls\TextInput;
10
use Nette\Forms\Form;
11
use Nette\Forms\Validator as NetteFormsValidator;
12
use Nette\Utils\Html;
13
use Nette\Utils\Strings;
14

15
class PhoneNumberInput extends TextInput
16
{
17

18
    public const VALID = Validator::class . '::validatePhoneNumber';
19
    public const VALID_STRICT = Validator::class . '::validatePhoneNumberStrict';
20
    public const REGION = Validator::class . '::validatePhoneNumberRegion';
21

22
    private const PHONE_NUMBER_REGEX = '[\s\d()\[\]~/.+-]+';
23

24
    private ?string $defaultRegionCode;
25

26
    public function __construct(string|Html|null $label = null, ?string $defaultRegionCode = null)
1✔
27
    {
28
        parent::__construct($label);
1✔
29
        $this->defaultRegionCode = $defaultRegionCode;
1✔
30
        $this->setHtmlType('tel');
1✔
31
        $this->setNullable();
1✔
32
        $invalidValueErrorMessage = NetteFormsValidator::$messages[self::VALID] ?? 'Please enter a valid phone number.';
1✔
33
        $this->addRule(Form::PATTERN, $invalidValueErrorMessage, self::PHONE_NUMBER_REGEX);
1✔
34
        $this->addRule(self::VALID, $invalidValueErrorMessage);
1✔
35
    }
1✔
36

37
    public function getDefaultRegionCode(): ?string
38
    {
39
        return $this->defaultRegionCode;
1✔
40
    }
41

42
    public function setDefaultRegionCode(?string $defaultRegionCode): void
1✔
43
    {
44
        $this->defaultRegionCode = $defaultRegionCode;
1✔
45
    }
1✔
46

47
    public function getValue(): ?PhoneNumber
48
    {
49
        /** @var PhoneNumber|null $value */
50
        $value = parent::getValue();
1✔
51
        return $value;
1✔
52
    }
53

54
    /**
55
     * @internal
56
     * @return $this
57
     */
58
    public function setValue(mixed $value): static
1✔
59
    {
60
        if ($value === null) {
1✔
61
            $this->value = '';
1✔
62
            $this->rawValue = '';
1✔
63
            return $this;
1✔
64
        }
65

66
        if (is_string($value)) {
1✔
67
            $value = PhoneNumber::parse($value, $this->defaultRegionCode);
1✔
68
        } elseif (! $value instanceof PhoneNumber) {
1✔
69
            throw new \InvalidArgumentException(
1✔
70
                sprintf(
1✔
71
                    'Value must be null, PhoneNumber instance, or string with a valid phone number, %s given in field "%s".',
1✔
72
                    gettype($value),
1✔
73
                    $this->name,
1✔
74
                ),
75
            );
76
        }
77

78
        $this->value = $value;
1✔
79
        $this->rawValue = ($this->defaultRegionCode !== null && $this->defaultRegionCode === $value->getRegionCode())
1✔
80
            ? $value->format(PhoneNumberFormat::NATIONAL)
1✔
81
            : $value->format(PhoneNumberFormat::INTERNATIONAL);
1✔
82
        return $this;
1✔
83
    }
84

85
    /**
86
     * @param PhoneNumber|string|null $value
87
     * @return $this
88
     */
89
    public function setDefaultValue(mixed $value): static
90
    {
91
        parent::setDefaultValue($value);
×
92
        return $this;
×
93
    }
94

95
    public function loadHttpData(): void
96
    {
97
        $value = $this->getHttpData(Form::DATA_LINE);
1✔
98

99
        if ($value === '' || $value === Strings::trim($this->translate($this->emptyValue))) {
1✔
100
            $this->value = '';
1✔
101
            $this->rawValue = $value;
1✔
102
            return;
1✔
103
        }
104

105
        try {
106
            $this->setValue($value);
1✔
107
            $this->rawValue = $value;
1✔
108
        } catch (PhoneNumberParseException $exception) {
1✔
109
            $this->value = '';
1✔
110
            $this->rawValue = $value;
1✔
111
        }
112
    }
1✔
113

114
    public function isFilled(): bool
115
    {
116
        return $this->rawValue !== '' && $this->rawValue !== Strings::trim($this->translate($this->emptyValue));
1✔
117
    }
118

119
    public function getControl(): Html
120
    {
121
        $control = parent::getControl();
1✔
122
        if ($this->defaultRegionCode !== null) {
1✔
123
            $control->data('default-region-code', $this->defaultRegionCode);
1✔
124
        }
125

126
        return $control;
1✔
127
    }
128

129
}
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