Collection of classes you can use to standardize data formats in your Laravel application.

Overview

Laravel Formatters

Laravel Formatters

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

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.

The package requires PHP 8.0 and Laravel 8.x. Future versions of PHP & Laravel will be supported.

Available formatters

... Will be more formatters soon. 😉

Contributing

If you have written your own formatter and want to add it to this package, PRs are welcomed.

  • Note: PRs won't be accepted without tests!

Installation

You can install the package via composer:

composer require michael-rubel/laravel-formatters

Usage

format(DateTimeFormatter::class, now()->addMonth()) // as `Carbon` instance

format(DateTimeFormatter::class, '2022-12-25 00:00:00') // as string timestamp

Use can pass the string instead of ::class:

format('date_time', '2022-12-25 00:00:00') // 2022-12-25 00:00

format('table_column', 'created_at') // Created at

You can configure the string case (snake/kebab) in the config file.

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 by publishing the config:

php artisan vendor:publish --tag="formatters-config"

Then use custom formatter:

format(YourCustomFormatter::class, [
    'to_format' => 'something',
    'additional_data' => true,
])

Or by passing the string as an alternative:

format('your_custom', 'Something to format.')

Testing

composer test
Comments
  • Code change you can make to align with PHP 7.4

    Code change you can make to align with PHP 7.4

    These are the changes to align with PHP 7.4 that I see.

    Just one test fails: • MichaelRubel\Formatters\Tests\FormatterBindingsTest > string bindings with camel case is forbidden Failed asserting that exception of type "Illuminate\Contracts\Container\BindingResolutionException" matches expected exception "MichaelRubel\Formatters\Exceptions\ShouldNotUseCamelCaseException". Message was: "Unresolvable dependency resolving [Parameter #0 [ $time ]] in class DateTime" at V:\sys\laragon\www\laravel-formatters\vendor\laravel\framework\src\Illuminate\Container\Container.php:1108

    opened by dany-eudes 4
  • Update `FullName` formatting behavior to handle names like `McKenzie`

    Update `FullName` formatting behavior to handle names like `McKenzie`

    About

    Updates FullName to properly format names like McKenzie.

    The underlying implementation splits the string into multiple words by spaces, then converts each word to uppercase format instead of converting the string to title-case.


    opened by michael-rubel 0
  • Update fullname formatter

    Update fullname formatter

    About

    With the current implementation, this is impossible to format names like Anna Nowak-Kowalska, because it will split the last name like "Anna Nowak Kowalska".

    Swapping the headline with title stringable method resolves the problem, but at the same time removes support for different separators, like the snake_case.

    enhancement 
    opened by michael-rubel 0
  • Full-name formatter

    Full-name formatter

    About

    Formatter to clean up the regular full name.

    Examples:

    • michael becomes Michael
    • michaeL rubéL becomes Michael Rubél
    • MichaelRubél becomes Michael Rubél
    • ⠀MichaelRubél⠀ becomes Michael Rubél
    • michael_rubél becomes Michael Rubél
    enhancement 
    opened by michael-rubel 0
  • Drop Laravel 8 support

    Drop Laravel 8 support

    About

    Laravel 8 already stopped receiving bug fixes, and the security fixes will stop in three months. It's time to move forward and drop support of Laravel 8 in a new major version of the package.

    Larastan code analysis tool also updated from ^1.0 to ^2.2


    enhancement 
    opened by michael-rubel 0
  • Cleanup in package directory

    Cleanup in package directory

    About

    This PR removes ExampleFormatter from the main folder of the package and places it in the tests folder. Configuration test updated appropriately.

    enhancement 
    opened by michael-rubel 0
  • Change behavior of date formatters

    Change behavior of date formatters

    About

    When we're working with date/datetime formatters, we need to make sure the date is not null. Otherwise, the format helper returns the current date. It is considered wrong behavior because most times current date is unexpected by the developer.

    This PR changes the date formatters, so all null and empty values are now handled by returning null by the format helper.

    Before:

    ! empty($datetime) ? format('date', $datetime) : __('No data')
    

    After:

    format('date', $datetime) ?? __('No data')
    

    This change is breaking, so it'll be published at the v5 of the package.

    enhancement 
    opened by michael-rubel 0
  • Localization in date formatters

    Localization in date formatters

    About

    This pull request adds the possibility to generate localized texts when using date formatters. Additionally, it adds nullability checks for format in these classes.

    enhancement 
    opened by michael-rubel 0
  • Use `Laravel Pint` instead of `PHP_CodeSniffer`

    Use `Laravel Pint` instead of `PHP_CodeSniffer`

    Logo Laravel Pint

    Introduction

    Laravel Pint is an opinionated PHP code style fixer for minimalists. Pint is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent.

    enhancement 
    opened by michael-rubel 0
Releases(7.0.5)
  • 7.0.5(Dec 10, 2022)

    What's Changed

    • PHP 8.2 build 🐘 by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/27

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/7.0.4...7.0.5

    Source code(tar.gz)
    Source code(zip)
  • 7.0.4(Dec 2, 2022)

    What's Changed

    • Update FullName formatting behavior to handle names like McKenzie by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/26

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/7.0.3...7.0.4

    Source code(tar.gz)
    Source code(zip)
  • 7.0.3(Nov 22, 2022)

    What's Changed

    • Bump LEC version 🏷 by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/25

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/7.0.2...7.0.3

    Source code(tar.gz)
    Source code(zip)
  • 7.0.2(Nov 14, 2022)

    What's Changed

    • Improve package service provider by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/24

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/7.0.1...7.0.2

    Source code(tar.gz)
    Source code(zip)
  • 7.0.1(Nov 14, 2022)

    What's Changed

    • Fix binding folder by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/23

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/7.0.0...7.0.1

    Source code(tar.gz)
    Source code(zip)
  • 7.0.0(Nov 14, 2022)

    What's Changed

    • Introduce mutation testing | Improve code quality & tests 🧪 by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/22

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/6.3.0...7.0.0

    Source code(tar.gz)
    Source code(zip)
  • 6.3.0(Nov 6, 2022)

    What's Changed

    • Add Name formatter by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/21

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/6.2.0...6.3.0

    Source code(tar.gz)
    Source code(zip)
  • 6.2.0(Oct 6, 2022)

    What's Changed

    • Sanitize invisible characters in FullNameFormatter by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/20

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/6.1.0...6.2.0

    Source code(tar.gz)
    Source code(zip)
  • 6.1.0(Sep 26, 2022)

    What's Changed

    • Update fullname formatter by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/19

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/6.0.0...6.1.0

    Source code(tar.gz)
    Source code(zip)
  • 6.0.0(Sep 20, 2022)

    What's Changed

    • Cleanup in package directory by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/16
    • Drop Laravel 8 support by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/17
    • Full-name formatter by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/18

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/5.0.0...6.0.0

    Source code(tar.gz)
    Source code(zip)
  • 5.0.0(Sep 6, 2022)

    What's Changed

    • Change behavior of date formatters by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/15

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/4.2.0...5.0.0

    Source code(tar.gz)
    Source code(zip)
  • 4.2.0(Aug 30, 2022)

    What's Changed

    • Add tests for dashes by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/13
    • Localization in date formatters by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/14

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/4.1.3...4.2.0

    Source code(tar.gz)
    Source code(zip)
  • 4.1.3(Aug 1, 2022)

    What's Changed

    • Update Laravel Pint & Remove redundant now assignments by @michael-rubel in https://github.com/michael-rubel/laravel-formatters/pull/12

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/4.1.2...4.1.3

    Source code(tar.gz)
    Source code(zip)
  • 4.1.2(Jul 8, 2022)

    What's Changed

    • Use Laravel Pint instead of PHP_CodeSniffer
    • Bump allowed version of LEC

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/4.1.1...4.1.2

    Source code(tar.gz)
    Source code(zip)
  • 4.1.1(Jun 23, 2022)

    Maintenance release

    • Avoiding further problems with passing null to native string functions like mb_strtoupper

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/4.1.0...4.1.1

    Source code(tar.gz)
    Source code(zip)
  • 4.1.0(Jun 9, 2022)

  • 4.0.1(May 23, 2022)

    What's changed

    • Removed the LEC provider registering (no longer necessary).

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/4.0.0...4.0.1

    Source code(tar.gz)
    Source code(zip)
  • 4.0.0(May 18, 2022)

    What's changed

    • The package was refactored to make it maintainable.
    • format helper now supports variadic parameters:
    format(DateTimeFormatter::class, now(), 'UTC', 'Y-m-d H:i:s');
    

    Full Changelog: https://github.com/michael-rubel/laravel-formatters/compare/3.3.3...4.0.0

    Source code(tar.gz)
    Source code(zip)
  • 3.3.3(Apr 1, 2022)

  • 3.3.2(Mar 25, 2022)

  • 3.3.1(Mar 24, 2022)

  • 3.3.0(Mar 24, 2022)

    Added

    • Variadic parameters in format helper.
    • Added possibility to set timezone in Date/DateTime formatters.

    Changed

    • Date/DateTime formatters now contain class properties for datetime and timezone parameters.
    • TaxNumber/LocaleNumber formatters are slightly modified to handle variadic parameters from collection.
    Source code(tar.gz)
    Source code(zip)
  • 3.2.2(Mar 7, 2022)

  • 3.1.0(Feb 9, 2022)

  • 3.0.0(Jan 14, 2022)

  • 2.1.1(Dec 10, 2021)

  • 2.1.0(Dec 7, 2021)

  • 2.0.0(Nov 25, 2021)

    Added

    • PHP 8.1 support.

    The version is considered major since composer.json now requires Laravel ^8.67. If someone's still on the lower version and cannot upgrade to the new one, you can stick with 1.x version until possible to upgrade.

    Source code(tar.gz)
    Source code(zip)
  • 1.2.7(Nov 12, 2021)

    Changed

    • Applied code quality recommendations from Larastan ^1.0
    • Added .editorconfig to standardize spacing across IDEs (#5)

    Contributors

    • @khalyomede
    Source code(tar.gz)
    Source code(zip)
Owner
Michael Rubel
Certified Laravel Developer
Michael Rubel
Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

Egor Shitikov 87 Nov 26, 2022
A collection of classes to be extended/used in laravel apps for quick development

laraquick A collection of classes to be extended/used in laravel applications for quick development. Introduction The library contains traits with wel

Ezra Obiwale 35 Dec 13, 2022
PHP components - collection of cross-project PHP classes

PHP components Collection of cross-project PHP classes. Install: $ composer require ansas/php-component Ansas\Component\Convert\ConvertPrice Convert "

null 1 Jan 5, 2022
A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDowell

PHPAlgorithms A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDow

Doğan Can Uçar 921 Dec 18, 2022
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

Tinashe Musonza 931 Dec 24, 2022
The list of all Algerian provinces and cities according to the official division in different formats: csv, xlsx, php, json, etc.

algeria-cities This repository contains the list of all the administrative provinces and cities in Algeria. The data is up-to-date according to the of

Ramtani Othmane 393 Jan 2, 2023
If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

hirwa-products-plugin If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

NIYIBIZI HIRWA 1 Aug 23, 2022
you can use this package with your develop operation....

Laravel Debugbar This is a package to integrate PHP Debug Bar with Laravel. It includes a ServiceProvider to register the debugbar and attach it to th

Eng Hasan Hajjar 2 Sep 30, 2022
🏭This package lets you create factory classes for your Laravel project.

Laravel Factories Reloaded ?? This package generates class-based model factories, which you can use instead of the ones provided by Laravel. Laravel 8

Christoph Rumpel 372 Dec 27, 2022
Aliyun oss filesystem storage adapter for laravel 5. You can use Aliyun OSS just like laravel Storage as usual

Aliyun oss filesystem storage adapter for laravel 5. You can use Aliyun OSS just like laravel Storage as usual

jacob 517 Dec 29, 2022
Use Laravel's built-in ORM classes to query cloud resources with Steampipe.

Laravel Steampipe Use Laravel's built-in ORM classes to query cloud resources with Steampipe, an open source CLI to instantly query cloud APIs using S

Renoki Co. 13 Nov 8, 2022
A query database collection for use with Laravel Pipeline

A query database collection for use with Laravel Pipeline This package contains a collection of class that can be used with Laravel Pipeline. Let's se

Dương Gia Bảo 188 Dec 24, 2022
Smeify is a Stable Automated Solution for Airtime and Data businesses in Nigeria, this package helps you integrate smeify easily into your laravel application.

Smeify is a Stable Automated Solution for Airtime and Data businesses in Nigeria, this package helps you integrate smeify easily into your laravel application.

Ogundiran Adewale Charles 2 Jul 27, 2022
A collection of helper functions that I use across my projects.

A collection of helper functions that I use across my projects. This package includes some of the helper functions that I tend to use in all of my pro

Ryan Chandler 33 Oct 18, 2022
Collection of agnostic PHP Functions and helpers with zero dependencies to use as foundation in packages and other project

Collection of agnostic PHP Functions and helpers This package provides a lot of very usefull agnostic helpers to use as foundation in packages and oth

padosoft 54 Sep 21, 2022
A collection of easy-to-use filters with clause conditions to Filament

Filament Advanced Filter A collection of easy-to-use filters with clause conditions to Filament Installation Install the package via composer (require

Webbing Brasil 45 Jan 2, 2023
🐍 Web application made in PHP with Laravel where you can interact via API with my Snake game which is made in Python

Snake web application Project of the web application where you can interact via API with Snake game which is available to download on it. Application

Maciek Iwaniuk 1 Nov 26, 2022
States allows you to create PHP classes following the State Pattern in PHP.

States allows you to create PHP classes following the State Pattern in PHP. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and this improve maintainability and workflows writing.

Teknoo Software 10 Nov 20, 2022
A Laravel package that allows you to use multiple ".env" files in a precedent manner. Use ".env" files per domain (multi-tentant)!

Laravel Multi ENVs Use multiple .envs files and have a chain of precedence for the environment variables in these different .envs files. Use the .env

Allyson Silva 48 Dec 29, 2022