anekdotes/formatter

Utility to format whole Input using provided rules

3.0.0 2021-11-24 16:30 UTC

This package is auto-updated.

Last update: 2024-10-24 22:33:54 UTC


README

Latest Stable Version Build Status codecov.io StyleCI License Total Downloads Codacy Badge

Utility to format whole Input using provided rules

Installation

Install via composer into your project:

composer require anekdotes/formatter

Usage

To use the formatter, include its namespace through Composer, then call its namespace and provide it with an data input and a rule input.

Exemple :

    use Anekdotes\Formatter\Formatter;
    $input = ["formPhoneNumber" => "1234567890"];
    $rules = ["formPhoneNumber" => ["phoneNumber"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["formPhoneNumber" => "(123) 456-7890"]

Rules

Here's a list of the usable rules :

postalCode

Convert into a postal code following this format : A1A 1A1

    use Anekdotes\Formatter\Formatter;
    $input = ["form" => "j6z1b2"];
    $rules = ["form" => ["postalCode"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["formPhoneNumber" => "J6Z 1B2"]

phoneNumber

Convert into a phone number following the format 111, 111-1111 or (111) 111-1111

    use Anekdotes\Formatter\Formatter;
    $input = ["formPhoneNumber" => "1234567890"];
    $rules = ["formPhoneNumber" => ["phoneNumber"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["form" => "(123) 456-7890"]

float

Cuts the characters following a float

    use Anekdotes\Formatter\Formatter;
    $input = ["form" => "122.2abc"];
    $rules = ["form" => ["float"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["form" => "122.2"]

int

Cuts the character following an integer

    use Anekdotes\Formatter\Formatter;
    $input = ["form" => "122.2abc"];
    $rules = ["form" => ["int"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["form" => 122]

integer

    use Anekdotes\Formatter\Formatter;
    $input = ["form" => "122.2abc"];
    $rules = ["form" => ["integer"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["form" => 122]

website

Append an http:// to a website.

    use Anekdotes\Formatter\Formatter;
    $input = ["form" => "www.anekdotes.com"];
    $rules = ["form" => ["website"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["form" => "http://www.anekdotes.com"]

Combining Rules

You can chain multiple rules to apply them on top of each other

    use Anekdotes\Formatter\Formatter;
    $input = ["formPhoneNumber" => "1234567890abcsefg"];
    $rules = ["formPhoneNumber" => ["int", "phoneNumber"]];
    $formattedOutput = Formatter::make($input, $rules);
    // $formattedOutput is now ["form" => "(123) 456-7890"]