-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhoneNumberInput.php
129 lines (108 loc) · 3.78 KB
/
PhoneNumberInput.php
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
127
128
129
<?php
declare(strict_types = 1);
namespace Nepada\PhoneNumberInput;
use Brick\PhoneNumber\PhoneNumber;
use Brick\PhoneNumber\PhoneNumberFormat;
use Brick\PhoneNumber\PhoneNumberParseException;
use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;
use Nette\Forms\Validator as NetteFormsValidator;
use Nette\Utils\Html;
use Nette\Utils\Strings;
class PhoneNumberInput extends TextInput
{
public const VALID = Validator::class . '::validatePhoneNumber';
public const VALID_STRICT = Validator::class . '::validatePhoneNumberStrict';
public const REGION = Validator::class . '::validatePhoneNumberRegion';
private const PHONE_NUMBER_REGEX = '[\s\d()\[\]~/.+-]+';
private ?string $defaultRegionCode;
public function __construct(string|Html|null $label = null, ?string $defaultRegionCode = null)
{
parent::__construct($label);
$this->defaultRegionCode = $defaultRegionCode;
$this->setHtmlType('tel');
$this->setNullable();
$invalidValueErrorMessage = NetteFormsValidator::$messages[self::VALID] ?? 'Please enter a valid phone number.';
$this->addRule(Form::PATTERN, $invalidValueErrorMessage, self::PHONE_NUMBER_REGEX);
$this->addRule(self::VALID, $invalidValueErrorMessage);
}
public function getDefaultRegionCode(): ?string
{
return $this->defaultRegionCode;
}
public function setDefaultRegionCode(?string $defaultRegionCode): void
{
$this->defaultRegionCode = $defaultRegionCode;
}
public function getValue(): ?PhoneNumber
{
/** @var PhoneNumber|null $value */
$value = parent::getValue();
return $value;
}
/**
* @internal
* @return $this
*/
public function setValue(mixed $value): static
{
if ($value === null) {
$this->value = '';
$this->rawValue = '';
return $this;
}
if (is_string($value)) {
$value = PhoneNumber::parse($value, $this->defaultRegionCode);
} elseif (! $value instanceof PhoneNumber) {
throw new \InvalidArgumentException(
sprintf(
'Value must be null, PhoneNumber instance, or string with a valid phone number, %s given in field "%s".',
gettype($value),
$this->name,
),
);
}
$this->value = $value;
$this->rawValue = ($this->defaultRegionCode !== null && $this->defaultRegionCode === $value->getRegionCode())
? $value->format(PhoneNumberFormat::NATIONAL)
: $value->format(PhoneNumberFormat::INTERNATIONAL);
return $this;
}
/**
* @param PhoneNumber|string|null $value
* @return $this
*/
public function setDefaultValue(mixed $value): static
{
parent::setDefaultValue($value);
return $this;
}
public function loadHttpData(): void
{
$value = $this->getHttpData(Form::DATA_LINE);
if ($value === '' || $value === Strings::trim($this->translate($this->emptyValue))) {
$this->value = '';
$this->rawValue = $value;
return;
}
try {
$this->setValue($value);
$this->rawValue = $value;
} catch (PhoneNumberParseException $exception) {
$this->value = '';
$this->rawValue = $value;
}
}
public function isFilled(): bool
{
return $this->rawValue !== '' && $this->rawValue !== Strings::trim($this->translate($this->emptyValue));
}
public function getControl(): Html
{
$control = parent::getControl();
if ($this->defaultRegionCode !== null) {
$control->data('default-region-code', $this->defaultRegionCode);
}
return $control;
}
}