Download the PHP package collab-corp/formatter without Composer
On this page you can find all versions of the php package collab-corp/formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download collab-corp/formatter
More information about collab-corp/formatter
Files in collab-corp/formatter
Package formatter
Short Description Formatting utility package for php
License MIT
Informations about the package formatter
Input Formatter
A php package for formatting data/values using a laravel validation like syntax, powered by Illuminate\Support
components.
Installation
composer require collab-corp/formatter
Use
The most basic use is simple, just pass your value and array of callables that your value should be called against:
Passing Arguments To Callables
You can specify arguments using a :
followed by a comma delimited list e.g callable:arg1,arg2
:
Note: Notice that we only specified the suffix
parameter. The ValueFormatter
class
automatically passes your value as the first parameter to every function with the exception of
delegation to objects/instances (See Using Instances section below).
What if value isnt the first parameter to my function?
No problem, you can specify what order you want your value passed by using the :value:
placeholder:
For example, preg_replace
accepts a value to format as the third parameter:
Whitelisting Allowed Callables
By default, all callables are allowed to be called, but if you are dynamically calling callables or want to add a protection layer, it may be worth specifying what callable functions should be allowed:
Using Instances/Object Values & Method Chaining
It is possible to pass an object/instance to the formatter and utilize any methods
on that instance. Using a .<methodName>
convention you can specify method chaining on that
instance. For example take a Carbon instance:
Closures/Formattable Classes
You can use closures for formatting your value as well:
Or you can also implement the CollabCorp\Formatter\Support\Contracts\Formattable
contract and use instances:
Optional Formatting/Blank Input
Sometimes you may only want to format a value if the value isnt null
or "blank":
You can specify ?
anywhere in the chain of callables to specify if the formatter
should break out of processing callables, often this should be defined in front of all
your callables:
Note: This packages uses Laravel's blank helper to determine blank/empty values.
If you have more complicated logic to break out of rules, use a closure or a Formattable
class and call the 2nd argument exit
callback:
Formatting multiple values
So far all the examples have been using a single value, but often times we are working
with much more data from client requests. This is where the DataFormatter
class is preferred:
Array Input
You may also format array data using dot notation:
Wildcard Formatting
Callables can contain wildcards as well:
This includes array input:
Concerns
If you prefer using a trait to easily create formatter instances, we provide the CollabCorp\Formatter\Support\Concerns\FormatsData
trait for easy access:
Contribute
Contributions are always welcome in the following manner:
- Issue Tracker
- Pull Requests
- Collab Corp Discord (Will send invite as requested)
License
The project is licensed under the MIT license.
All versions of formatter with dependencies
illuminate/support Version ~7.0|^8.0|^9.0
illuminate/validation Version ~7.0|^8.0|^9.0