simonschaufi / typo3-phone
基于Google的libphonenumber API,为TYPO3添加电话号码功能。
Requires
- php: ^8.1
- ext-json: *
- giggsey/libphonenumber-for-php: ^8.13
- simonschaufi/typo3-support: ^2.0 || ^3.0
- typo3/cms-core: ^12.4
Requires (Dev)
- ergebnis/composer-normalize: ^2.42.0
- friendsofphp/php-cs-fixer: ^3.54.0
- phpunit/phpunit: ^10.5
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.9
- symfony/console: ^5.4 || ^6.4
- symfony/translation: ^5.4 || ^6.4 || ^7.0
- symfony/yaml: ^5.3.6 || ^6.4.0
- typo3/coding-standards: ^0.7.1
- typo3/testing-framework: ^8.0.9
- dev-main
- 11.x-dev
- v3.1.1
- v3.1.0
- v3.0.0
- v2.1.3
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.1
- v1.0.0
- dev-dependabot/composer/phpunit/phpunit-tw-10.5.35
- dev-dependabot/composer/phpstan/phpstan-tw-1.12.4
- dev-dependabot/composer/squizlabs/php_codesniffer-tw-3.10.3
- dev-dependabot/github_actions/dependabot/fetch-metadata-2.2.0
This package is auto-updated.
Last update: 2024-09-23 07:07:32 UTC
README
基于PHP端口的Google的libphonenumber,为TYPO3添加电话号码功能。
安装
运行以下命令安装包的最新适用版本
composer require simonschaufi/typo3-phone
验证
对于每个动作(这里为updateAction
),你想要验证你的对象(在这种情况下为一个具有“电话”和“传真”两个属性的地址),在你的控制器中添加以下代码
use SimonSchaufi\TYPO3Phone\Validation\Validator\PhoneValidator; public function initializeUpdateAction(): void { if ($this->request->hasArgument('address') && $this->request->getArgument('address')) { $addressValidator = $this->validatorResolver->getBaseValidatorConjunction(Address::class); $validators = $addressValidator->getValidators(); $validators->rewind(); $validator = $validators->current(); /** @var PhoneValidator $phoneValidator */ $phoneValidator = $this->validatorResolver->createValidator(PhoneValidator::class, [ // If the user enters a number prefixed with "+" then the country can be guessed. // If not, the following countries listed in the array will be checked against 'countries' => ['DE'], 'international' => true, ]); $validator->addPropertyValidator('phone', $phoneValidator); $validator->addPropertyValidator('fax', $phoneValidator); } }
或者你可以在代码的任何地方实例化验证器,如下所示
use SimonSchaufi\TYPO3Phone\Validation\Validator\PhoneValidator; $phoneValidator = GeneralUtility::makeInstance(PhoneValidator::class); $phoneValidator->setOptions([ // If the user enters a number prefixed with "+" then the country can be guessed. // If not, the following countries listed in the array will be checked against 'countries' => ['DE'], 'types' => ['mobile'], 'international' => true, ]); $result = $phoneValidator->validate('+3212345678'); if ($result->hasErrors()) { // Error handling }
注意:国家代码应遵守ISO 3166-1 alpha-2规范。
要支持除白名单国家以外的任何有效的国际格式电话号码,请使用international
选项。这在期望从特定国家获得本地格式号码的同时,也想要接受任何其他正确输入的外国号码时非常有用。
$phoneValidator->setOptions([ 'international' => true, ]);
验证器将尝试从号码本身提取国家,然后检查该国家的号码是否有效。如果无法猜测国家,它将使用提供的后备国家进行验证。请注意,只有在电话号码以国际格式(以加号+
开头,例如 +32 ...)输入时,国家猜测才能正常工作。前导双零将不会正确解析,因为这不是一个已建立的规则。
要指定号码类型的约束,设置允许的类型,例如
$phoneValidator->setOptions([ 'types' => ['mobile'], ]);
最常见的类型是mobile
和fixed_line
,但您可以使用定义在这里的任何类型。
您还可以通过使用lenient
选项启用宽松验证。启用宽松验证时,只检查号码的长度,而不是实际的运营商模式。
$phoneValidator->setOptions([ 'lenient' => true, ]);
Extbase之外的验证
如果您不想使用Extbase验证器,而是采用更底层的方案,请使用以下代码
信息:在这种情况下,地址对象有一个类型为\SJBR\StaticInfoTables\Domain\Model\Country
的“国家”属性
use SimonSchaufi\TYPO3Phone\Exceptions\NumberParseException; use SimonSchaufi\TYPO3Phone\PhoneNumber; if (!empty($address->getPhone())) { try { $phoneNumber = (new PhoneNumber($address->getPhone(), [$address->getCountry()->getIsoCodeA2()]))->formatInternational(); $address->setPhone($phoneNumber); } catch (NumberParseException $exception) { // Error handling } }
电话号码工具类
可以将电话号码包裹在SimonSchaufi\TYPO3Phone\PhoneNumber
类中,以增强其功能。可以直接在视图中引用这些对象或在数据库中保存时使用这些对象,因为它们将优雅地降级到E.164格式。
use SimonSchaufi\TYPO3Phone\PhoneNumber; (string) new PhoneNumber('+3212/34.56.78'); // +3212345678 (string) new PhoneNumber('012 34 56 78', 'BE'); // +3212345678
格式化
电话号码可以以多种方式格式化
use SimonSchaufi\TYPO3Phone\PhoneNumber; $phone = new PhoneNumber('012/34.56.78', 'BE'); $phone->format($format); // See libphonenumber\PhoneNumberFormat $phone->formatE164(); // +3212345678 $phone->formatInternational(); // +32 12 34 56 78 $phone->formatRFC3966(); // +32-12-34-56-78 $phone->formatNational(); // 012 34 56 78 // Formats so the number can be called straight from the provided country. $phone->formatForCountry('BE'); // 012 34 56 78 $phone->formatForCountry('NL'); // 00 32 12 34 56 78 $phone->formatForCountry('US'); // 011 32 12 34 56 78 // Formats so the number can be clicked on and called straight from the provided country using a cellphone. $phone->formatForMobileDialingInCountry('BE'); // 012345678 $phone->formatForMobileDialingInCountry('NL'); // +3212345678 $phone->formatForMobileDialingInCountry('US'); // +3212345678
号码信息
获取有关电话号码的一些信息
use SimonSchaufi\TYPO3Phone\PhoneNumber; $phone = new PhoneNumber('012 34 56 78', 'BE'); $phone->getType(); // 'fixed_line' $phone->isOfType('fixed_line'); // true $phone->getCountry(); // 'BE' $phone->isOfCountry('BE'); // true
相等比较
检查给定的电话号码是否等于另一个电话号码
use SimonSchaufi\TYPO3Phone\PhoneNumber; $phone = new PhoneNumber('012 34 56 78', 'BE'); $phone->equals('012/34.56.78', 'BE') // true $phone->equals('+32 12 34 56 78') // true $phone->equals($anotherPhoneObject) // true/false $phone->notEquals('045 67 89 10', 'BE') // true $phone->notEquals('+32 45 67 89 10') // true $phone->notEquals($anotherPhoneObject) // true/false
数据库考虑事项
免责声明:不同应用中对电话号码的处理方式有很大差异。以下提到的主题因此被视为一系列思考起点;将不会提供支持。
在数据库中存储电话号码一直是一个有争议的话题,而且根本没有一劳永逸的解决方案。这完全取决于您的应用程序需求。以下是一些需要考虑的事项,以及一些实现建议。您理想的数据库配置可能将是以下详细说明的一些指南的组合。
唯一性
E.164格式在全球范围内唯一地识别电话号码,并隐含地指明特定的国家。它可以直接提供给phone()
辅助函数。
您需要
- 一个列来存储电话号码
- 在持久化之前将电话号码格式化为E.164
示例
- 用户输入 =
012/45.65.78
- 数据库列
phone
(varchar) =+3212456578
以用户输入的方式呈现电话号码
如果您存储了格式化电话号码,原始用户输入将永远无法恢复。可能有益的是向用户提供他们自己的输入电话号码,例如改善用户体验。
您需要
- 两列来存储原始输入和相关的国家
示例
- 用户输入 =
012/34.56.78
- 数据库列
phone
(varchar) =012/34.56.78
phone_country
(varchar) =BE
支持搜索
通过电话号码进行搜索可能会迅速变得非常复杂,并且始终需要深入理解您应用程序的上下文和范围。以下是一个可能的方法,涵盖了相当多的“自然”用例。
您需要
- 三个额外的列来存储电话号码的可搜索变体
- 标准化输入(去除所有非字母字符的原始输入)
- 国家格式电话号码(去除所有非字母字符)
- E.164格式电话号码
- 利用可搜索变体的广泛搜索查询
示例
- 用户输入 =
12/34.56.78
- 数据库列
phone_normalized
(varchar) =12345678
phone_national
(varchar) =012345678
phone_e164
(varchar) =+3212345678
需要帮助将此扩展集成到您的网站中吗?
请通过我的网站联系我:[https://www.simonschaufelberger.de/de/kontakt.html](https://www.simonschaufelberger.de/de/kontakt.html) 并我会帮助您!
表示感谢
此扩展深受https://github.com/Propaganistas/Laravel-Phone的启发。谢谢!