Download the PHP package selective/transformer without Composer
On this page you can find all versions of the php package selective/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download selective/transformer
More information about selective/transformer
Files in selective/transformer
Package transformer
Short Description A strictly typed array transformer with dot-access, fluent interface and filters.
License MIT
Homepage https://github.com/selective-php/transformer
Informations about the package transformer
selective/transformer
A strictly typed array transformer with dot access and fluent interface. The mapped result can be used for JSON responses and many other things.
Table of Contents
- Requirements
- Installation
- Introduction
- Dot access
- Object access
- Transforming
- Transforming list of arrays
- Mapping rules
- Simple mapping rules
- Complex mapping rules
- Filter
- Custom Filter
- Examples
- JSON conversion
- PDO resultset conversion
- License
Requirements
- PHP 8.1+
Installation
Introduction
This Transformer component provides functionality to map, cast and loop array values from an array or object to another array.
Converting complex data with simple PHP works by using a lot of type casting, if
conditions and looping through the
data with foreach()
. This leads to very high cyclomatic complexity and nesting depth, and thus poor "code rating".
Before: Conditions: 9, Paths: 256, CRAP Score: 9
Click to expand!
After: Conditions: 1, Paths: 1, CRAP Score: 1
Click to expand!
Use Cases
When building an API it is common for people to just grab stuff from the database and pass it to json_encode()
. This
might be passable for “trivial” APIs but if they are in use by the public, or used by mobile applications then this will
quickly lead to inconsistent output. The Transformer is able to create a “barrier” between source data and output, so
schema changes do not affect users.
The Transformer works also very well to put any kind of database resultset (e.g. from PDO) into a new data structure.
The uses cases are not limited.
Dot access
You can copy any data from the source array to any sub-element of the destination array using the dot-syntax.
Object access
It's possible to access the properties of an object using the dot notation.
The result:
Transforming
Transforming arrays
For the sake of simplicity, this example has been put together as though it was one file. In reality, you would spread the manager initiation, data collection and JSON conversion into separate parts of your application.
Sample data:
The result:
Transforming list of arrays
The method toArrays
is able to transform a list of arrays.
This can be useful if you want to transform a resultset from a database query, or a response payload from an API.
Example:
The result:
Mapping Rules
Simple mapping rules
Using strings, separated by |
, to define a filter chain:
Because lastName
is blank but required the result looks like this:
Complex mapping rules
For complexer mapping rules there is a fluent interface available. To create a new rule use the rule
method and pass
it as 3rd. parameter to the map
method.
Because lastName
is blank but required the result looks like this:
Filter
Most filters are directly available as method.
Custom Filter
You can also add your own custom filter:
Add a custom filter using a callback:
Define a custom filter using a mapping specific callback:
There are even more filter classes available that can be registered manually:
You can also implement and register your own filter classes as well.
Examples
JSON conversion
You can use your own json component or just the native json_encode
function.
Writing JSON to a PSR-7 response object:
PDO resultset conversion
The result:
Similar packages
License
The MIT License (MIT). Please see License File for more information.