t-labs-co/formatter

The Laravel Formatter package

1.0.2 2025-03-22 05:06 UTC

This package is not auto-updated.

Last update: 2025-03-22 05:15:16 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package helps you format your PHP strings. You can use it for one string or many, and it's as simple as using Laravel validation.

Work with us

We're PHP and Laravel whizzes, and we'd love to work with you! We can:

  • Design the perfect fit solution for your app.
  • Make your code cleaner and faster.
  • Refactoring and Optimize performance.
  • Ensure Laravel best practices are followed.
  • Provide expert Laravel support.
  • Review code and Quality Assurance.
  • Offer team and project leadership.
  • Delivery Manager

Features

  • Simple and intuitive syntax for defining formatting rules.
  • Support for multiple built-in formatting rules (e.g., trim, replace, limit, date_format).
  • Ability to create custom formatting rules.
  • Integration with Laravel models and validation.
  • Comprehensive unit tests for ensuring reliability.

PHP and Laravel Version Support

This package supports the following versions of PHP:

  • PHP: ^8.2

Installation

You can install the package via composer:

composer require t-labs-co/formatter

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'
];
*/

Use custom format rule

use TLabsCo\Formatter\Rules\FormatterRule;

class Max100FormatRule extends FormatterRule
{
    public function format($attribute, $value)
    {
        if (strlen($value) > 100) {
            return substr($value, 0, 100);
        }

        return $value;
    }
}
// Using with custom rule
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',
    'short_description' => ['trim', new Max100FormatRule()],
];

$data = [
    'title' => '  How to resolve missing Local Composer Dependencies on CentOS 8?  ',
    'publish_date' => '2024/05/02 13:00',
    'short_description' => 'Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description Very long description '
];

$formatted = Formatter::make($data, $formatterRules)->format()->formatted();

/*
Output:
$formatted = [
    'title' => 'How to resolve missing [Composer Dependencies] on CentOS 8?',
    'publish_date' => '2024-05-02',
    'short_description' => 'Very long description Very long description Very long description Very long description Very long de'
];
*/

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

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.