arraypress/wp-rate-format
Composer 安装命令:
composer require arraypress/wp-rate-format
包简介
A WordPress library for formatting, rendering, sanitizing, and validating rates and percentages
关键字:
README 文档
README
A WordPress library for formatting, rendering, sanitizing, and validating rates and percentages. Handles values that can be either a percentage or a flat currency amount, with automatic type resolution from data objects.
Installation
composer require arraypress/wp-rate-format
Features
- Type Resolution — Automatically detects whether a rate is a percentage or flat amount from object properties
- Percentage Formatting — Format and render percentage values with i18n support
- Rate Formatting — Smart formatting that dispatches to percentage or currency based on type
- HTML Rendering — Ready-to-use HTML output for admin tables and displays
- Sanitization — Clamp and sanitize percentage and rate values for safe storage
- Validation — Validate rates and percentages with configurable bounds
Usage
Percentage Formatting
use ArrayPress\RateFormat\Rate; // Plain string: "15%" Rate::format_percentage( 15 ); // With decimals: "15.50%" Rate::format_percentage( 15.5, 2 ); // HTML: <span class="percentage">15%</span> Rate::render_percentage( 15 );
Rate Formatting (Auto-Detection)
The library resolves the rate type from your data object by checking for {column}_type or type properties:
// Object with rate_type = 'percentage' $item = (object) [ 'discount' => 15, 'discount_type' => 'percentage', ]; // Returns: "15%" Rate::format( $item->discount, $item, 'discount' ); // Object with rate_type = 'flat' and currency $item = (object) [ 'discount' => 1500, 'discount_type' => 'flat', 'currency' => 'USD', ]; // Returns: "$15.00" Rate::format( $item->discount, $item, 'discount' );
HTML Rendering
// Renders as percentage or currency HTML based on type Rate::render( $item->rate, $item, 'rate' ); // Returns null for invalid values (pair with your own empty state) Rate::render( $value, $item ) ?? '<span>—</span>';
Type Resolution
// Resolve type from an object $type = Rate::resolve_type( $item, 'discount' ); // 'percentage', 'flat', etc. // Check type categories Rate::is_percentage_type( 'percent' ); // true Rate::is_percentage_type( '%' ); // true Rate::is_flat_type( 'fixed' ); // true Rate::is_flat_type( 'amount' ); // true // Determine the effective format Rate::determine_format( 15, $item, 'discount' ); // 'percentage' or 'currency'
Sanitization
// Clamp percentage between 0-100, round to 2 decimals Rate::sanitize_percentage( 150 ); // 100.0 Rate::sanitize_percentage( -5 ); // 0.0 Rate::sanitize_percentage( 15.555, 0, 100, 1 ); // 15.6 // Custom bounds Rate::sanitize_percentage( 50, 10, 90 ); // 50.0 // Sanitize rate based on resolved type Rate::sanitize_rate( $value, $item, 'discount' );
Validation
// Validate percentage Rate::is_valid_percentage( 50 ); // true Rate::is_valid_percentage( 150 ); // false Rate::is_valid_percentage( 'abc' ); // false // Custom bounds Rate::is_valid_percentage( 50, 10, 90 ); // true // Validate rate based on resolved type Rate::is_valid_rate( $value, $item, 'discount' );
Integration with Admin Tables
use ArrayPress\RateFormat\Rate; // In your column formatter 'rate' => Rate::render( $value, $item, $column_name ) ?? self::render_empty(), 'percentage' => Rate::render_percentage( $value ) ?? self::render_empty(),
Type Resolution Order
When resolving the rate type from an item object, the library checks in this order:
$item->get_{column}_type()method (e.g.,get_discount_type())$item->{column}_typeproperty (e.g.,discount_type)$item->get_type()method$item->typeproperty- Falls back to guessing: values 0–100 are treated as percentages, others as currency
Supported Type Identifiers
| Category | Identifiers |
|---|---|
| Percentage | percent, percentage, % |
| Flat | flat, fixed, amount |
Requirements
- PHP 7.4 or later
- WordPress 6.2 or later
- arraypress/wp-currencies
License
GPL-2.0-or-later
arraypress/wp-rate-format 适用场景与选型建议
arraypress/wp-rate-format 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「validation」 「currency」 「stripe」 「formatting」 「sanitization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/wp-rate-format 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/wp-rate-format 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/wp-rate-format 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fixer.io data provider for Peso
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
TCMB Currenct Converter
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 44
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2026-02-04