PHPackages                             selective/transformer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. selective/transformer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

selective/transformer
=====================

A strictly typed array transformer with dot-access, fluent interface and filters.

1.5.0(2mo ago)3817.8k↓22.7%31MITPHPPHP ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Jan 26Pushed 11mo ago2 watchersCompare

[ Source](https://github.com/selective-php/transformer)[ Packagist](https://packagist.org/packages/selective/transformer)[ Docs](https://github.com/selective-php/transformer)[ RSS](/packages/selective-transformer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (8)Versions (11)Used By (1)

selective/transformer
=====================

[](#selectivetransformer)

A strictly typed array transformer with dot access and fluent interface. The mapped result can be used for JSON responses and many other things.

[![Latest Version on Packagist](https://camo.githubusercontent.com/899b2e88c457a9a51d827c859177a67bfdd46e159dc4dfa9985f6929c7ebb436/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73656c6563746976652d7068702f7472616e73666f726d65722e737667)](https://packagist.org/packages/selective/transformer)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/selective-php/transformer/workflows/build/badge.svg)](https://github.com/selective-php/transformer/actions)[![Total Downloads](https://camo.githubusercontent.com/7301f6736b7cde43a864b27763780e46166f5072f87cf435544ca64f7c52b6d7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73656c6563746976652f7472616e73666f726d65722e737667)](https://packagist.org/packages/selective/transformer/stats)

Table of Contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Introduction](#introduction)
- [Dot access](#dot-access)
    - [Object access](#object-access)
- [Transforming](#transforming)
    - [Transforming list of arrays](#transforming-list-of-arrays)
- [Mapping rules](#mapping-rules)
    - [Simple mapping rules](#simple-mapping-rules)
    - [Complex mapping rules](#complex-mapping-rules)
- [Filter](#filter)
    - [Custom Filter](#custom-filter)
- [Examples](#examples)
    - [JSON conversion](#json-conversion)
    - [PDO resultset conversion](#pdo-resultset-conversion)
- [License](#license)

Requirements
------------

[](#requirements)

- PHP 8.1 - 8.4

Installation
------------

[](#installation)

```
composer require selective/transformer

```

Introduction
------------

[](#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![![](https://user-images.githubusercontent.com/781074/107609324-e3c45880-6c3e-11eb-9ca0-ed27e420ec13.png)](https://user-images.githubusercontent.com/781074/107609324-e3c45880-6c3e-11eb-9ca0-ed27e420ec13.png)**After**: Conditions: 1, Paths: 1, CRAP Score: 1

 Click to expand![![](https://user-images.githubusercontent.com/781074/107609468-4584c280-6c3f-11eb-8f10-3cd42bc27b74.png)](https://user-images.githubusercontent.com/781074/107609468-4584c280-6c3f-11eb-8f10-3cd42bc27b74.png)### Use Cases

[](#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
----------

[](#dot-access)

You can copy any data from the source array to any sub-element of the destination array using the dot-syntax.

```
