t-labs-co / formatter
A powerful data formatting package for Laravel — apply chainable format rules to transform, clean, and normalize data values.
Fund package maintenance!
v2.0.0
2026-10-28 05:08 UTC
Requires
- php: ^8.4
- illuminate/contracts: ^12|^13
- illuminate/support: ^12|^13
Requires (Dev)
- larastan/larastan: ^3
- laravel/pint: ^1
- orchestra/testbench: ^10|^11
- pestphp/pest: ^4|^5
- pestphp/pest-plugin-laravel: ^4|^5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A powerful data formatting package for Laravel — apply chainable format rules to transform, clean, and normalize data values.
Features
- Simple and intuitive syntax for defining formatting rules
- 24+ built-in formatting rules (trim, replace, limit, date_format, etc.)
- Chainable rules using pipe
|separator - Custom formatting rules via
FormatterRuleabstract class - Facade support for convenient usage
- Full strict typing (PHPStan Level 8)
- Comprehensive test coverage
PHP and Laravel Version Support
| Package Version | PHP | Laravel |
|---|---|---|
| ^2.0 | ^8.4 | ^12 | ^13 |
| ^1.0 | ^8.2 | ^11 | ^12 |
Installation
composer require t-labs-co/formatter
Upgrading from v1.x? Read the Migration Guide for a step-by-step upgrade path.
Usage
Basic Usage
use TLabsCo\Formatter\Formatter; $formatterRules = [ 'title' => 'trim|replace:Local Composer Dependencies,[Local Composer Dependencies]|replace:[Local Composer Dependencies],[Composer Dependencies]|limit:150', 'publish_date' => 'date_format:Y-m-d', ]; $data = [ 'title' => ' How to resolve missing Local Composer Dependencies on CentOS 8? ', 'publish_date' => '2024/05/02 13:00', ]; $formatted = Formatter::make($data, $formatterRules)->format()->formatted(); /* Output: $formatted = [ 'title' => 'How to resolve missing [Composer Dependencies] on CentOS 8?', 'publish_date' => '2024-05-02' ]; */
Using the Facade
use TLabsCo\Formatter\Facades\Formatter; $result = Formatter::make($data, $rules)->format()->formatted();
Single Value Formatting
use TLabsCo\Formatter\Formatter; $result = Formatter::singleFormat(' hello world ', 'trim|upper'); // 'HELLO WORLD'
Custom Format Rules
use TLabsCo\Formatter\Rules\FormatterRule; class Max100FormatRule extends FormatterRule { public function format(string $attribute, mixed $value): mixed { if (strlen((string) $value) > 100) { return substr((string) $value, 0, 100); } return $value; } } // Using with custom rule $formatterRules = [ 'title' => 'trim|limit:150', 'short_description' => ['trim', new Max100FormatRule()], ];
Format Rules Reference
| Rule | Description | Example |
|---|---|---|
boolean |
Convert to boolean (true/false) | boolean or boolean:0 for 1/0 |
integer |
Convert to integer | integer |
float |
Convert to float | float |
pricing |
Format as pricing number | pricing:2 |
array |
Convert to array | array |
substring |
Extract substring | substring:0,5 or substring:from,to |
trim |
Trim whitespace | trim or trim:.,! |
trimstart |
Trim leading whitespace | trimstart |
trimend |
Trim trailing whitespace | trimend |
trimcustom |
Custom trim | trimcustom |
replace |
Case-insensitive string replace | replace:old,new |
replacefirst |
Replace first occurrence | replacefirst:old,new |
replacelast |
Replace last occurrence | replacelast:old,new |
studly |
Convert to StudlyCase | studly |
title |
Convert to Title Case | title |
upper |
Convert to UPPER CASE | upper |
lower |
Convert to lower case | lower |
md5 |
Generate MD5 hash | md5 |
limit |
Limit string length | limit:50 or limit:50,-- |
pad |
Pad both sides | pad:10,* |
padleft |
Pad left side | padleft:10,* |
padright |
Pad right side | padright:10,* |
date |
Validate and format date | date |
date_format |
Format date to specific format | date_format:Y-m-d |
timestamp |
Convert to Unix timestamp | timestamp |
url_param |
Extract URL query parameter | url_param:page |
Quality & Development Practices
- PHPStan Level 8 — Static analysis with maximum strictness
- Laravel Pint — Code style following Laravel conventions
- Pest PHP — Modern, elegant testing framework
- Strict Types — All source files use
declare(strict_types=1)
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Upgrade Guide
Please see MIGRATION for instructions on upgrading from v1.x to v2.0.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.