michael-rubel / laravel-formatters
This package is a collection of classes you can use to standardize data formats in your Laravel application. It uses the Service Container to easily extend or override the formatter classes.
Fund package maintenance!
paypal.com/donate/?hosted_button_id=KHLEL8PFS4AXJ
Installs: 47 290
Dependents: 5
Suggesters: 0
Security: 0
Stars: 91
Watchers: 1
Forks: 5
Open Issues: 1
Requires
- php: ^8.1
- ext-intl: *
- illuminate/contracts: ^10.0|^11.0
- michael-rubel/laravel-enhanced-container: *
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- brianium/paratest: ^6.3|^7.4
- infection/infection: ^0.27.3
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0|^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^9.5.4|^10.5
- dev-main
- 8.0.2
- 8.0.1
- 8.0.0
- 7.1.1
- 7.1.0
- 7.0.7
- 7.0.6
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.0.0
- 4.2.0
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.2
- 3.2.0
- 3.1.0
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- dev-dependabot/composer/league/commonmark-2.6.0
- dev-php84
- dev-dependencies/laravel-11-21
This package is auto-updated.
Last update: 2024-12-10 01:14:37 UTC
README
Laravel Formatters
This package introduces the Formatter
pattern you can use to standardize data formats in your Laravel application. You can write your own formatters and put them in app/Formatters
folder, then apply them everywhere in your application through format
helper. The package uses the Service Container under the hood to easily extend or override the formatter classes.
The package requires PHP 8.1
or higher and Laravel 10
or higher.
If you're looking for older versions, check release history.
#StandWithUkraine
Installation
Install the package via composer:
composer require michael-rubel/laravel-formatters
php artisan vendor:publish --tag="formatters-config"
Usage
format(DateTimeFormatter::class, now());
You can use a shorter version of the string as an alternative:
format('date-time', now());
Available built-in formatters
Artisan command
To make the programmer's life easier, we also added the Artisan command. You can use make:formatter
command to generate formatter classes. It will put the class with the given name into app/Formatters
folder and auto-inject the stub.
Extending formatters
Since the formatters are resolved through the Service Container they can be easily overridden by extending bindings.
For example in your Service Provider:
$this->app->extend(DateTimeFormatter::class, function ($formatter) { $formatter->datetime_format = 'Y.m.d H:i'; return $formatter; });
Adding custom/overriding package formatters
To add a custom formatter you should create the class that implements the MichaelRubel\Formatters\Formatter
interface and put it to the app/Formatters
folder.
You can put formatter with the same name as the package's to override the formatter from the package. You can customize the folder in the config file.
Examples
You can discover examples of the usage here.
Contributing
If you have written your own formatter and want to add it to this package, PRs are welcomed. But take care of the extendability of the formatter you want to make as built-in and remember to write tests for your use cases.
Testing
composer test