From 51279644f199acdb6bc4feac9d01182f17b588f0 Mon Sep 17 00:00:00 2001 From: mahdi khanzadi Date: Mon, 26 Aug 2019 23:19:51 +0430 Subject: [PATCH] add custom transformer docs --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index 2fd4050..9b49e9f 100755 --- a/README.md +++ b/README.md @@ -83,6 +83,53 @@ data: as you see, you can set original data dynamically using `setOriginalData` method. +## Transformers + +you can use `Shetabit\Transform\Classes\Transformer` to transform array keys. + +if you have complex array structures, you can create a custom transformer by implementing `TransformerInterface` + +```php +// at the top +use Shetabit\Transformer\Contracts\TransformerInterface; + +// ... + +class CustomTranformerName implements TransformerInterface +{ + /** + * Transform data + * + * @return array + */ + public function transform(array $data) : array + { + return [ + 'user_name' => $data['u'], + 'password' => $data['p'] + ]; + } +} + +// use your custom transformer +$originalData = [ + 'u' => 'mahdikhanzadi', + 'p' => '246810' +]; + +$transformer = new CustomTranformerName(); + +$transformedData = (new Transform($originalData))->get($transformer); + +/* +data: +[ + 'username' => 'mahdikhanzadi', + 'password' => '246810' +] +*/ +``` + ## Change log Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.