Download the PHP package shetabit/transformer without Composer
On this page you can find all versions of the php package shetabit/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shetabit/transformer
More information about shetabit/transformer
Files in shetabit/transformer
Package transformer
Short Description transform or modify array keys in an easy way
License MIT
Homepage https://github.com/shetabit/transformer
Informations about the package transformer
Transformer
transform array keys easily.
This package supports PHP 8.4+ and has no dependencies of its own, so it can be used in any PHP framework —
Laravel, Symfony and the rest — as well as in no framework at all.
List of contents
- Install
- How to use
- Renaming with a format
- Renaming with from and to
- Nested arrays
- How keys that collide are resolved
- Custom transformers
- Errors
- Testing
- Change log
- Contributing
- Security
- Credits
- License
Install
Via Composer
How to use
Renaming with a format
A format maps the keys the data has onto the keys it should have. Hand it to a Transformer and run it with
get($transformer).
A key the format does not mention is left as it is, and every key keeps the place it had in the data.
The original data can be set at any time with setOriginalData(), so one Transform can be run over row after row:
Renaming with from and to
The same format can be written down pair by pair with from($currentFormat) and to($destinationFormat).
Both take a list as well:
Nested arrays
By default only the keys of the array itself are renamed. A recursive transformer applies the same format to every array nested in the data, including the arrays of a list:
recursive() turns it on for a transformer that was built without it, recursive(false) turns it off again.
How keys that collide are resolved
Every key is renamed at once, so a key can take the name another key is giving up in the same run. Two columns that were filled in the wrong order are swapped back with:
Where two keys would end up with the same name the renamed one wins, and of two renamed ones the one that comes first in the data:
Custom transformers
For a structure a format cannot describe, write a transformer of your own by implementing TransformerInterface.
Errors
Every exception of the package extends Shetabit\Transformer\Exceptions\TransformerException, so all of them can be
caught at once.
| Exception | Thrown when |
|---|---|
TransformerNotValidException |
Transform::get() is run without a transformer. |
InvalidFormatException |
from() and to() were not given the same number of keys. |
Testing
Every pull request and every push to master is checked by GitHub Actions: the test suite runs on
PHP 8.4 and 8.5 (against both the lowest and the highest supported dependencies), the coding style is checked with
PHP_CodeSniffer, the sources are analysed with PHPStan and the code coverage of the test suite is measured and has to
stay above 95%.
Next to the unit tests there are feature tests that run realistic nested payloads through the package end to end: a whole tree renamed and renamed back with the flipped format, rows whose columns are swapped or shifted along, and the output of one transformer handed to the next.
You can run the same checks locally. With PHP and Composer installed on your machine:
If you would rather not install PHP on your machine, the shipped Dockerfile and Makefile run everything inside a
container:
Another PHP version can be used with make test PHP_VERSION=8.5.
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Mahdi khanzadi
- All Contributors
License
The MIT License (MIT). Please see License File for more information.