Download the PHP package surgiie/transformer without Composer
On this page you can find all versions of the php package surgiie/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download surgiie/transformer
More information about surgiie/transformer
Files in surgiie/transformer
Package transformer
Short Description A data transforming/formatting package for php.
License MIT
Informations about the package transformer
Transformer
transformer is a PHP package for transforming values or input, powered by the Laravel framework's validation components.
Installation
`
Usage
To use the package, pass your data and an array of callable functions that your data should be passed through:
Note that the syntax is similar to the Laravel validation syntax because this package is powered by the same components.
Passing Arguments
You can specify arguments for your functions using a <function>:<comma-delimited-list>
syntax:
Specify Value Argument Order
By default, the first argument passed to your function will be the value being formatted, followed by the arguments specified in the order provided. If your function does not accept the value as the first argument, you can use the :value:
placeholder to specify the order. For example:
Casting Arguments
You may find that when passing arguments to the transforming functions, you may hit run time errors due to typehints on arguments for basic types. Since arguments are being specified as a string here, you may come across a scenario like this:
The above would throw an error because the 1
argument being passed in is a string and the function expects a integer. In these cases, you can use the following convention to cast the argument to specific native type, by suffixing @<type>
to the value. For example to cast the 1
to an integer:
Available Casting Types:
The following basic casting types are available
- int
- str
- float
- bool
- array
- object
Note For more complex type or casting needs, use a Closure or Surgiie\Transformer\Contracts\Transformable
class as documented below.
Optional Transformation
If you only want to transform a value if it is not null or "blank", you can use the ?
character in the chain of functions to specify when to break out of processing. This is often placed at the start of the chain:
Note: This package uses Laravel's blank
helper to determine blank/empty values. If you have more complex logic for breaking out of rules, you can use a closure or a \Surgiie\Transformer\Contracts\Transformable
class and call the 2nd argument exit callback.
Closures and Transformable Classes
You can use closures to transform your values:
Alternatively, you can implement the Surgiie\Transformer\Contracts\Transformable
contract and use class instances:
Array Input
You can also format nested array data using dot notation:
Wildcards
You can also use wildcards on keys to apply transformers on keys that match the wildcard pattern:
Object Values/Method Delegation
It is possible to delegate a function call to an object if the value has been converted to an instance. Using the syntax -><methodName>:args
, you can specify method chaining on that instance:
You can also use class constants that accept a single value as its constructor, for example:
Guard Layer Over Execution
By default, all available functions that are callable at runtime will be executed. However, if you want to add a protection or security layer that prevents certain methods from being called, you can add a guard callback that checks if a method should be called by returning true:
Manually Transforming Values/Single Values
To format a one-off value, use the Transformer class:
Use Traits
To transform data and values on-the-fly in your classes, use the \Surgiie\Transformer\Concerns\UsesTransformer
trait:
Use the Request Macro
To transform data using a macro on a Illuminate\Http\Request
object instance, call the transform()
method on the request, which returns the transformed data.
When calling on a FormRequest
object, it uses the validated()
function to retrieve the input data. Note that this requires the data you are targeting to be defined as a validation rule in your form request's rules function, otherwise the data will be omitted from transformation.
Package Discovery/Don't Discover
Laravel automatically registers the package service provider, but if you don't want this, you can ignore package discovery for the service provider by including the following in your composer.json
:
Contribute
Contributions are always welcome in the following manner:
- Discussions
- Issue Tracker
- Pull Requests
All versions of transformer with dependencies
illuminate/support Version ^9.0|^10.0|^11.0
illuminate/validation Version ^9.0|^10.0|^11.0
php Version ^8.2.0