Giter Site home page Giter Site logo

laravel-formatters's Introduction

Laravel Formatters

Laravel Formatters

Latest Version on Packagist Total Downloads Code Quality Code Coverage GitHub Tests Action Status PHPStan

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

SWUbanner

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

laravel-formatters's People

Contributors

dependabot[bot] avatar khalyomede avatar michael-rubel avatar olsza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-formatters's Issues

Support PHP 7.x

Could you please downgrade to PHP 7.x ?
Maybe PHP 7.4...

Thank you for shared laravel-formatters. This will very very useful for Laravel community.

Add .editorconfig file

To help having the same conventions across differents IDEs (mostly useful for indentation style/size)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.