rocketfellows/specific-country-vat-number-format-validators-config

v1.0.0 2023-04-26 17:53 UTC

This package is auto-updated.

Last update: 2024-09-26 21:05:45 UTC


README

Code Coverage Badge

本包提供了一个配置特定国家验证器列表的抽象类。它是CountryVatNumberFormatValidatorsConfigInterface接口的实现之一。

安装

composer require rocketfellows/specific-country-vat-number-format-validators-config

依赖

包组件列表

  • SpecificCountryVatNumberFormatValidatorsConfig - 实现特定国家验证器元组的抽象类,用于配置CountryVatNumberFormatValidatorsConfigInterface接口;

SpecificCountryVatNumberFormatValidatorsConfig 描述

SpecificCountryVatNumberFormatValidatorsConfig 属性

  • $validators - 存储特定国家验证器元组的属性;

SpecificCountryVatNumberFormatValidatorsConfig 函数

  • abstract public function getCountry(): Country - 抽象公共函数,必须在子类中实现,它定义了具体配置的特定国家;
  • abstract protected function getDefaultValidator(): CountryVatFormatValidatorInterface - 抽象受保护的函数,必须在子类中实现,它定义了国家默认增值税号格式验证器,返回CountryVatFormatValidatorInterface实例;

类构造函数接受两个可选参数

  • $defaultValidator - CountryVatFormatValidatorInterface实例或null,如果传递此参数,则替换子类中从getDefaultValidator函数返回的验证器;
  • $additionalValidators - CountryVatFormatValidators实例或null,特定国家的附加验证器元组;

扩展SpecificCountryVatNumberFormatValidatorsConfig的子类必须实现以下函数

  • public function getCountry(): Country - 实现CountryVatNumberFormatValidatorsConfigInterface接口的函数,返回arslanimamutdinov\ISOStandard3166\Country实例,从而子类定义了vat号格式验证器配置的国家;
  • protected function getDefaultValidator(): CountryVatFormatValidatorInterface - 实现SpecificCountryVatNumberFormatValidatorsConfig类的抽象函数,返回CountryVatFormatValidatorInterface类型的对象 - 特定国家的默认增值税号格式验证器;

根据传递给子类构造函数的参数,在CountryVatFormatValidators元组中形成$validators元组,其中的验证器根据以下规则进行类型化

  • 如果$defaultValidator参数为null且$additionalValidators参数为null,则$validators属性仅存储从实现getDefaultValidator函数返回的一个验证器;
  • 如果$defaultValidator参数为null且$additionalValidators参数不为null,则$validators属性存储从实现getDefaultValidator函数返回的验证器和传递给构造函数的$additionalValidators;
  • 如果 $defaultValidator 参数不为空且 $additionalValidators 参数为空,则将 $validators 属性中通过的 $defaultValidator 存储到构造函数中;
  • 如果 $defaultValidator 参数不为空且 $additionalValidators 参数不为空,则将 $validators 属性中通过的 $defaultValidator$additionalValidators 存储到构造函数中;

使用示例

假设我们需要实现德国增值税号码格式的配置。为此,您需要创建一个类(例如,DEVatNumberFormatValidatorsConfig),并从 SpecificCountryVatNumberFormatValidatorsConfig 继承。德国的默认增值税号码格式验证器可以从包 https://packagist.org.cn/packages/rocketfellows/de-vat-format-validator 中使用 - 已经有一个实现 CountryVatFormatValidatorInterface 接口的德国验证器类。对于德国的默认增值税号码格式验证器,我们将使用 DEVatFormatValidator。对于 getCountry 函数,我们可以使用已定义的 arslanimamutdinov\ISOStandard3166\ISO3166 实用工具 Country getter 来获取德国。

class DEVatNumberFormatValidatorsConfig extends SpecificCountryVatNumberFormatValidatorsConfig
{
    public function getCountry(): Country
    {
        return ISO3166::DE();
    }

    protected function getDefaultValidator(): CountryVatFormatValidatorInterface
    {
        return new DEVatFormatValidator();
    }
}

这样,我们就为德国创建了默认的预配置的增值税号码格式验证器。

$config = new DEVatNumberFormatValidatorsConfig();

// will return Country instance for Germany (according to standard ISO 3166)
$config->getCountry();

// will return CountryVatFormatValidators tuple with one item, which is DEVatFormatValidator instance
$config->getValidators();

假设我们想在当前配置中替换德国增值税号码格式验证器的实现。为此,您需要创建一个新的验证器类,该类必须实现 CountryVatFormatValidatorInterface 接口。

class AnotherDEVatFormatValidator implements CountryVatFormatValidatorInterface
{
    public function isValid(string $vatNumber): bool
    {
        // add some implementation
    }
}

进一步,在将 DEVatNumberFormatValidatorsConfig 实例化为构造函数时,作为第一个参数,您需要传递新验证器的实例,这将替换配置中的默认验证器。

$config = new DEVatNumberFormatValidatorsConfig(new AnotherDEVatFormatValidator());

// will return Country instance for Germany (according to standard ISO 3166)
$config->getCountry();

// will return CountryVatFormatValidators tuple with one item, which is AnotherDEVatFormatValidator instance
$config->getValidators();

假设我们不想替换配置中的默认验证器,而是想添加 AnotherDEVatFormatValidator 作为附加的一个。例如,默认验证器仅检查个人增值税号码格式,而我们希望配置中有一个检查法人增值税号码格式的验证器。为此,在实例化 DEVatNumberFormatValidatorsConfig 时,将第一个参数传递为 null,并将包含一个元素的 CountryVatFormatValidators 元组作为第二个参数传递。

$config = new DEVatNumberFormatValidatorsConfig(
    null,
    (
        new CountryVatFormatValidators(
            new AnotherDEVatFormatValidator()
        )
    )
);

// will return Country instance for Germany (according to standard ISO 3166)
$config->getCountry();

// will return CountryVatFormatValidators tuple with two items:
// object instance of DEVatFormatValidator
// and object instance of AnotherDEVatFormatValidator
$config->getValidators();

如果您想完全更改 DEVatNumberFormatValidatorsConfig 的配置,那么在实例化配置时,需要传递自己的作为默认验证器,以及作为附加验证器。

更多使用案例示例可以在包的单元测试中找到:rocketfellows\SpecificCountryVatNumberFormatValidatorsConfig\tests\unit\SpecificCountryVatNumberFormatValidatorsConfigTest

贡献

欢迎提交拉取请求。如果有重大更改,请先为讨论打开一个问题。

请确保根据需要更新测试。