websoft/phone-number-module

将libphonenumber集成到您的Zend Framework 2应用程序中

安装次数: 1,747

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

类型:zf2-module

0.0.6 2015-11-04 10:20 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:53:09 UTC


README

将libphonenumber集成到您的Zend Framework 2应用程序中。目前只实现了ZF2表单过滤器验证器。

使用表单过滤器和验证器

  • 在应用程序配置中激活PhoneNumber模块(config/application.config.php
<?php
	return [
    	// This should be an array of module namespaces used in the application.
    	'modules' => [
        	'MyApp',
       		'PhoneNumber',
    	],
    ];

  • 如有必要,覆盖模块配置中的默认区域(config/autoload/config.local.php)。默认设置是"CH"(瑞士)。参见\libphonenumber\RegionCode类获取有效的区域代码。
<?php
	return [
    	'phone_number' => [
        	'default_region' => 'CH',
    	],
    ];

注意:区域也可以在过滤器或验证器选项中覆盖(使用关键字"region")。

  • 将过滤器和验证器添加到表单中。
<?php

namespace Contact;

use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;

class ContactForm extends Form implements InputFilterProviderInterface
{
    public function init()
    {
        $this->add([
            'name' => 'mobile',
            'type' => 'Text',
            'options' => [
                'label' => 'Mobile *'
            ]
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            [
                'name' => 'mobile',
                'required' => true,
                'filters' => [
                    [
                    	'name' => 'PhoneNumber\Filter\PhoneNumberFilter',
                    	'options' => [
                    		 // override the default region
                            'region' => 'GB',
                        ],
                    ],
                ],
                'validators' => [
                    [
                        'name' => 'PhoneNumber\Validator\PhoneNumberValidator',
                        'options' => [
                        	 // override the default region
                            'region' => 'GB',
                            // override the error messages
                            'messageTemplates' => [
                                \PhoneNumber\Validator\PhoneNumberValidator::INVALID 
                                	=> _("'%value%' is not a valid phone number")
                            ]
                        ],
                    ],
                ],
            ],
        ];
    }
}

在本地运行单元测试

您需要在本地环境中安装PhpUnit以运行单元测试。

MacOSX的PhpUnit安装指令

curl https://phar.phpunit.de/phpunit.phar -o phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit

安装composer.phar和供应商模块

$:> curl -sS https://getcomposer.org/installer | php
$:> chmod +x composer.phar
$:> ./composer.phar install

运行所有单元测试

$:> cd test
$:> phpunit