PHPackages                             arraypress/wp-rate-format - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. arraypress/wp-rate-format

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

arraypress/wp-rate-format
=========================

A WordPress library for formatting, rendering, sanitizing, and validating rates and percentages

05PHP

Since Feb 4Pushed 3mo agoCompare

[ Source](https://github.com/arraypress/wp-rate-format)[ Packagist](https://packagist.org/packages/arraypress/wp-rate-format)[ RSS](/packages/arraypress-wp-rate-format/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

WordPress Rate Format
=====================

[](#wordpress-rate-format)

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
------------

[](#installation)

```
composer require arraypress/wp-rate-format
```

Features
--------

[](#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
-----

[](#usage)

### Percentage Formatting

[](#percentage-formatting)

```
use ArrayPress\RateFormat\Rate;

// Plain string: "15%"
Rate::format_percentage( 15 );

// With decimals: "15.50%"
Rate::format_percentage( 15.5, 2 );

// HTML: 15%
Rate::render_percentage( 15 );
```

### Rate Formatting (Auto-Detection)

[](#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

[](#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 ) ?? '—';
```

### Type Resolution

[](#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

[](#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

[](#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

[](#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
---------------------

[](#type-resolution-order)

When resolving the rate type from an item object, the library checks in this order:

1. `$item->get_{column}_type()` method (e.g., `get_discount_type()`)
2. `$item->{column}_type` property (e.g., `discount_type`)
3. `$item->get_type()` method
4. `$item->type` property
5. Falls back to guessing: values 0–100 are treated as percentages, others as currency

Supported Type Identifiers
--------------------------

[](#supported-type-identifiers)

CategoryIdentifiersPercentage`percent`, `percentage`, `%`Flat`flat`, `fixed`, `amount`Requirements
------------

[](#requirements)

- PHP 7.4 or later
- WordPress 6.2 or later
- [arraypress/wp-currencies](https://github.com/arraypress/wp-currencies)

License
-------

[](#license)

GPL-2.0-or-later

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance54

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd6eb8aff0903d87eb674d1ba3c5f3653899c0d7661504eb0deb7798ed86b643?d=identicon)[arraypress](/maintainers/arraypress)

---

Top Contributors

[![arraypress](https://avatars.githubusercontent.com/u/22668877?v=4)](https://github.com/arraypress "arraypress (1 commits)")

### Embed Badge

![Health badge](/badges/arraypress-wp-rate-format/health.svg)

```
[![Health](https://phpackages.com/badges/arraypress-wp-rate-format/health.svg)](https://phpackages.com/packages/arraypress-wp-rate-format)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
