承接 kreyu/nip-validator-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kreyu/nip-validator-bundle

Composer 安装命令:

composer require kreyu/nip-validator-bundle

包简介

Validate the NIP/TIN numbers with configurable validation constraint

README 文档

README

Latest Stable Version Build Status

Validate the tax identification numbers with configurable validation constraint.

Requirements

  • Symfony validator >=6.1
  • PHP >=8.1

For PHP 7.0 & Symfony 3.4, look at v1.x version.

Installation

To download the bundle, require it using the Composer:

$ composer require kreyu/nip-validator-bundle

Usage

Like with all Symfony validation constraints, you can apply it by using annotations:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip()
     */
    private $nip;
}

or by using attributes:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    #[Assert\Nip]
    private $nip;
}

or by using YAML:

App\Entity\Company:
    properties:
        nip:
            - Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip: ~

or by using XML:

<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

    <class name="App\Entity\Company">
        <property name="nip">
            <constraint name="Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip"/>
        </property>
    </class>
</constraint-mapping>

or by using PHP:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Company
{
    private $nip;
    
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('nip', new Nip());
    }
}

Pattern validation

By default, the only accepted format is a string of characters without the prefix nor dashes.

Usage of dashes

If you wish to allow or require the usage of the dashes, use the allowDashes and requireDashes options:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     allowDashes=true,
     *     requireDashes=true
     * )
     */
    private $nip;
}

Both options are set to false by default.
Setting the requireDashes option to true ignores the value of the allowDashes, as the number without dashes is no longer valid.
The allowed formats with dashes are following:

  • XX-XXX-XXX-XX, e.g. 34-208-769-99
  • XXX-XXX-XX-XX, e.g. 342-087-69-99

Usage of prefix

If you wish to allow or require the usage of the prefix, use the allowPrefix and requirePrefix options.
Additionally, it is possible to modify the length of the prefix, using the prefixLength option.

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     allowPrefix=true,
     *     requirePrefix=true,
     *     prefixLength=2     
     * )
     */
    private $nip;
}

Both allowPrefix and requirePrefix options are set to false by default, and prefixLength defaults to 2.
Prefixes are not followed with the space:

  • PPXXXXXXXXXX, e.g. PL3420876999

Customization of the pattern violation message

It is possible to customize the pattern violation message using the patternMessage option:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     patternMessage="This is not a valid NIP number."
     * )
     */
    private $nip;
}

You can use the following parameters in this message:

Parameter Description
{{ value }} The current (invalid value)
{{ pattern }} The regular expression pattern used in the validation

Usage of a custom regular expression

If the default functionality does not meet your needs, consider using the pattern option:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     pattern="/^(\d{2}.\d{3}.\d{3}.\d{2})$/"
     * )
     */
    private $nip;
}

By default this option is equals null, and setting it to any other value ignores the allowDashes, requireDashes, allowPrefix and requirePrefix options.

Checksum validation

By default, the checksum is being validated. If you wish to disable this feature, set the checksum option to false:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     checksum=false
     * )
     */
    private $nip;
}

Customization of the checksum violation message

It is possible to customize the checksum violation message using the checksumMessage option:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     checksumMessage="This is not a valid NIP number."
     * )
     */
    private $nip;
}

You can use the following parameters in this message:

Parameter Description
{{ value }} The current (invalid value)

Usage of a callable normalizer

It is possible to define the PHP callable to apply on the value before the validation, using the normalizer option:

<?php

namespace App\Entity;

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints as Assert;

class Company
{
    /**
     * @Assert\Nip(
     *     normalizer="trim"
     * )
     */
    private $nip;
}

License

The MIT License (MIT). Please see license file for more information.

kreyu/nip-validator-bundle 适用场景与选型建议

kreyu/nip-validator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 257.72k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validation」 「nip」 「constraint」 「tin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kreyu/nip-validator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kreyu/nip-validator-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 kreyu/nip-validator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 257.72k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-24