This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTelephone.php
175 lines (147 loc) · 5.27 KB
/
Telephone.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace newism\fields\fields;
use CommerceGuys\Addressing\Country\CountryRepository;
use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\helpers\Html;
use libphonenumber\PhoneNumberUtil;
use newism\fields\models\TelephoneModel;
/**
* @property-read array $countryOptions
* @property-read array $elementValidationRules
* @property-read null|string $settingsHtml
*/
class Telephone extends Field implements PreviewableFieldInterface
{
protected PhoneNumberUtil $phoneNumberUtil;
public string $defaultCountryCode = 'US';
public static function displayName(): string
{
return Craft::t('nsm-fields', 'Telephone (Newism)');
}
public function init(): void
{
parent::init();
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
public function rules(): array
{
$rules = parent::rules();
$rules = array_merge(
$rules,
[
[['defaultCountryCode'], 'string'],
[['defaultCountryCode'], 'default', 'value' => 'US'],
]
);
return $rules;
}
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed
{
// Just return value if it's already an TelephoneModel.
if ($value instanceof TelephoneModel) {
return $value;
}
// Serialised value from the DB
if (is_string($value)) {
$value = json_decode($value, true);
}
// Array value from post or unserialized array
if (is_array($value) && !empty(array_filter($value))) {
return new TelephoneModel(
strlen($value['countryCode']) ? $value['countryCode'] : $this->defaultCountryCode,
$value['rawInput']
);
}
return null;
}
public function serializeValue(mixed $value, ?ElementInterface $element = null): mixed
{
if ($value instanceof TelephoneModel && !$value->phoneNumber) {
return null;
}
return parent::serializeValue($value, $element);
}
public function getSettingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate(
'nsm-fields/_components/fieldtypes/Telephone/settings',
[
'field' => $this,
'countryOptions' => $this->getCountryOptions(),
]
);
}
public function getInputHtml(mixed $value, ?ElementInterface $element = null): string {
// Get our id and namespace
$id = Html::id($this->handle);
$namespace = Craft::$app->getView()->getNamespace();
$namespacedId = Craft::$app->getView()->namespaceInputId($id);
return Craft::$app->getView()->renderTemplate(
'nsm-fields/_components/fieldtypes/Telephone/input',
[
'name' => $this->handle,
'viewData' => $value,
'field' => $this,
'id' => $id,
'namespace' => $namespace,
'namespacedId' => $namespacedId,
'countryOptions' => $this->getCountryOptions(),
'settings' => $this->getSettings(),
]
);
}
private function getCountryOptions(): array
{
// Removing the null default value as this causes more problems
// We already have a default country code defined in the settings and trying to manage a field that can have
// the first Telephone object as an optional parameter is difficult.
// $countries = [['value' => '', 'label' => '']];
$countryRepository = new CountryRepository();
$countryData = $countryRepository->getList();
$countries = [];
foreach ($countryData as $key => $option) {
$regionCode = $this->phoneNumberUtil->getCountryCodeForRegion(
$key
);
$countries[] = [
'value' => $key,
'label' => $option.($regionCode ? ' +'.$regionCode : ''),
];
}
return $countries;
}
public function getElementValidationRules(): array
{
return array_merge(parent::getElementValidationRules(), [
'validatePhoneNumber'
]);
}
public function isValueEmpty(mixed $value, ElementInterface $element = null): bool
{
return ($value instanceof TelephoneModel)
? $value->isEmpty()
: parent::isValueEmpty($value, $element);
}
public function validatePhoneNumber( ElementInterface $element, array $params = null ) {
$value = $element->getFieldValue($this->handle);
$valid = $value->isValid();
if (!$valid) {
$element->addError(
$this->handle,
Craft::t(
'nsm-fields',
'The string supplied did not seem to be a phone number or didn\'t match the expected format for the country.'
)
);
}
}
public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
{
return ($value instanceof TelephoneModel && !$value->isEmpty())
? (string) $value->format("E164")
: '';
}
}