erwane / cakephp-contact
Cakephp 插件,用于操作联系信息(地址、电话等)
Requires
- php: >=7.2.0
- ext-json: *
- cakephp/cakephp: ^4.0
- giggsey/libphonenumber-for-php: ^8.12
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.0
- php-parallel-lint/php-parallel-lint: ^1.1
- phpro/grumphp: ^1.3
- phpunit/phpunit: ^9.5
README
===============================================
CakePHP-Contact旨在帮助您保存、测试和显示所有本地性和联系数据,如电话和地址。
它仅与CakePHP 4兼容。对于Cakephp3兼容性,请使用1.x版本
使用Composer安装
使用Composer安装它
composer require erwane/cakephp-contact
电话号码
实用工具
Contact\Utility\Phone::format(?string $text = null, array $options = [])
$text
是字符串或null,$options
是一个数组,包含以下可能的选项和输出
country
: 国家代码,例如FR
或UK
format
: *international
: +33 1 23 45 67 89 *national
: 01 23 45 67 89 *uri
: tel:+33-1-23-45-67-89 *short
: +33123456789
PhoneNumberType
电话号码数据库类型自动将请求数据格式化为E164电话号码(+33....)。它还可以将未格式化的数据库结果中的电话号码格式化。
如何使用PhoneNumberType
// in src/Application.php use Cake\Core\Exception\MissingPluginException; public function bootstrap(): void { // Load Contact plugin try { $this->addPlugin(\Contact\Plugin::class); } catch (MissingPluginException $e) { debug($e->getMessage()); } } // in table file use Cake\Database\Schema\TableSchemaInterface; use Cake\Orm\Table; class UsersTable extends Table /** * {@inheritDoc} */ protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface { $schema->setColumnType('phone_number', 'phonenumber') ->setColumnType('mobile_number', 'phonenumber'); return $schema; }
默认国家
表单中的电话号码设置为用户所在国家的格式,例如法国的 0123456789
。但是可能存在冲突,这取决于填写表单的用户所在国家。您可以设置 defaultCountry
以设置所有未设置为国际格式的电话号码。
// in config/bootstrap.php // or after loaded user preference or website country use Cake\Database\TypeFactory; use Contact\Database\Type\PhoneNumberType; $phoneNumberType = new PhoneNumberType(); $phoneNumberType->setDefaultCountry('BE'); TypeFactory::set('phonenumber', $phoneNumberType);
现在,所有非国际格式表单电话号码均以 +32 前缀进行格式化
电话号码验证
Contact插件提供简单的电话号码验证规则
// in validation method public function validationDefault(Validator $validator) { $validator->setProvider('contact', 'Contact\Validation\ContactValidation'); $validator->add('phone_number', [ 'number' => [ 'provider' => 'contact', 'rule' => ['phone'], ], ]); } // You can pass country argument $validator->add('phone_number', [ 'number' => [ 'provider' => 'contact', 'rule' => ['phone', 'ES'], ], ]);
电话帮助器
您可以以非常简单的方式格式化电话号码;
// In src/AppView.php public function initialize(): void { $this->loadHelper('Contact.Contact'); } // in template file echo $this->Contact->phone($entity->phone_number); // Can pass options (see Utility/Phone::format() help) echo $this->Contact->phone($entity->phone_number, [ 'country' => 'BE', 'format' => 'uri', ]);
地址特征
此特征必须附加到您的App中的 \Cake\ORM\Entity
。它从实体字段中获取地址数据并将其格式化为标准数组或字符串格式。
字段和格式可以在实体或方法中配置。
默认
地址字段匹配
默认情况下,地址将从以下字段提取
// ['key' => 'field name in database']
[
'organization' => 'organization',
'street1' => 'street1',
'street2' => 'street2',
'postalCode' => 'postalCode',
'locality' => 'locality',
'region' => 'Regions.title', // Pluralized table name
'country' => 'Countries.title', // Pluralized table name
]
数据可以位于关联模型中。使用点格式 Countries.title
进行设置。
地址文本格式
地址文本($entity->address_text
)使用 Cake\Utility\Text::insert()
方法来格式化字段。
默认地址文本格式是
":organization
:street1
:street2
:postalCode :locality
:country"
地址特征配置
您可以通过两种方式更改默认配置。
通过方法
// setAddressFields(array $fields, bool $merge = true) $entity->setAddressFields(['organization' => 'name']); // setAddressFormat(string $format) $entity->setAddressFormat(":organization\n:country");
在实体中
use Cake\ORM\Entity; use Contact\Model\Entity\AddressTrait; class Company extends Entity { use AddressTrait; protected $_addressFields = [ 'organization' => 'name', 'street1' => 'address1', 'street2' => 'address2', 'postalCode' => 'postal_code', 'locality' => 'city', 'region' => 'Regions.name', 'country' => 'Countries.name', ]; protected $_addressFormat = ":street1 :street2\n:locality :postalCode\n:region :country"; }