upward/formatters 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

upward/formatters

Composer 安装命令:

composer require upward/formatters

包简介

Formatters utils

README 文档

README

Upward Formatters is a PHP package that provides utilities for formatting, validating, and anonymizing documents such as CPF, CNPJ, numbers, and dates. It implements PSR-4 for autoloading and can be easily integrated into any PHP project.

Installation

To install the package, use Composer:

composer require upward/formatters

Document usage

Examples

Working with CPF Documents

The CPF (Cadastro de Pessoas Físicas) is the Brazilian individual taxpayer registry identification. Below are examples of how to validate, format, and anonymize CPF numbers:

// Creating and validating a CPF Document
use Upward\Formatters\Documents\CpfDocument;
use Upward\Formatters\Document;

// Initialize with a CPF number
$cpf = new CpfDocument(value: '12345678909');
$document = new Document($cpf);

// Validate the CPF (throws exception if invalid)
try {
    $document->validate();
    echo "CPF is valid!";
} catch (\Exception) {
    //
}

// Format the CPF with standard mask
echo $document->format();    // Outputs: 123.456.789-09

// Create an anonymized version for privacy
echo $document->anonymize(); // Outputs: 123.***.***-09

Working with CNPJ Documents

The CNPJ (Cadastro Nacional da Pessoa Jurídica) is the Brazilian registry for legal entities. Here's how to work with CNPJ documents:

// Creating and working with CNPJ Documents
use Upward\Formatters\Documents\CnpjDocument;
use Upward\Formatters\Document;

// Initialize with a CNPJ number
$cnpj = new CnpjDocument(value: '12345678000195');
$document = new Document($cnpj);

// Validate the CNPJ
try {
    $document->validate();
    echo "CNPJ is valid and can be used!";
} catch (\Exception) {
    //
}

// Apply standard CNPJ formatting
echo $document->format();    // Outputs: 12.345.678/0001-95

// Generate privacy-safe version for displaying
echo $document->anonymize(); // Outputs: 12.***.***/0001-95

Managing Documents with Folder

The Folder class provides a convenient way to manage collections of documents:

// Working with multiple documents
use Upward\Formatters\Folder;
use Upward\Formatters\Documents\CpfDocument;
use Upward\Formatters\Documents\CnpjDocument;
use Upward\Formatters\Document;
use Upward\Formatters\Exceptions\Documents\InvalidDocumentException;

// Create a document collection
$folder = new Folder();

// Add different document types
$folder->push(new Document(new CpfDocument('12345678909')));
$folder->push(new Document(new CnpjDocument('12345678000195')));

// Count documents in collection
echo "Total documents: " . count($folder); // Outputs: 2

// Filter for only valid documents
$validDocs = $folder->valid();

// Process each document in the collection
$folder->each(callback: static function (Document $document, string | int $key): void {
    // Perform operations on each document
});

// Validate all documents at once (will throw exception on first invalid document)
try {
    $folder->validate();
    echo "All documents are valid!";
} catch (InvalidDocumentException) {
    //
}

For more detailed examples and advanced usage, please refer to the documentation for each document type.

upward/formatters 适用场景与选型建议

upward/formatters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 157 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 upward/formatters 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 157
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-07