PHPackages                             nilportugues/api-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. [API Development](/categories/api)
4. /
5. nilportugues/api-transformer

ActiveLibrary[API Development](/categories/api)

nilportugues/api-transformer
============================

Base library providing the core functionality for API transformation.

3.1.1(9y ago)9108.7k↑325%10[3 issues](https://github.com/nilportugues/php-api-transformer/issues)5MITPHPPHP &gt;=7.0

Since Aug 14Pushed 9y ago2 watchersCompare

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

READMEChangelog (10)Dependencies (6)Versions (35)Used By (5)

API Transformer
===============

[](#api-transformer)

[![Build Status](https://camo.githubusercontent.com/32ba1e8b468296bfd743130fca39fb295a69350c8a87b315a7f6c742f6e369a2/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d6170692d7472616e73666f726d65722e737667)](https://travis-ci.org/nilportugues/php-api-transformer)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7307d8067c0f358ca3722b16d1bb5ff4528d4fe04362fa822145cc31e27d66a0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f6170692d7472616e73666f726d65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/api-transformer/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/861a3f730b13b83bd7e37c0ef4f0aeaa464f389f2a2819ef469ebd4933b9c0cb/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f62346535303536642d633535322d343037652d616532312d3264613638356530376330362f6d696e692e706e67)](https://insight.sensiolabs.com/projects/b4e5056d-c552-407e-ae21-2da685e07c06)[![Latest Stable Version](https://camo.githubusercontent.com/f1a3b82a6344f9e5624b05bb6382fdfcb6688114dfd39956ad7ca311ff7f4bb1/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6170692d7472616e73666f726d65722f762f737461626c65)](https://packagist.org/packages/nilportugues/api-transformer)[![Total Downloads](https://camo.githubusercontent.com/94b8270549404b838f41dbee40bf1c9ec7d5812fe02513e3848c7a4b2b05374b/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6170692d7472616e73666f726d65722f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/api-transformer)[![License](https://camo.githubusercontent.com/457028dffb589c98fce0a4fc0245a2b27d0d4c81ae07b9aa05d5d5a907f38cb9/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6170692d7472616e73666f726d65722f6c6963656e7365)](https://packagist.org/packages/nilportugues/api-transformer)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

Purpose
-------

[](#purpose)

This library provides the core functionality for API transformation and is a base library for many other packages.

By itself it's not usable at all. Check the projects using it below.

Used by
-------

[](#used-by)

Currently the following transformers make use of this library as foundation:

- [nilportugues/json](https://github.com/nilportugues/json-transformer)
- [nilportugues/jsend](https://github.com/nilportugues/jsend-transformer)
- [nilportugues/hal](https://github.com/nilportugues/hal-transformer)
- [nilportugues/json-api](https://github.com/nilportugues/jsonapi-transformer)

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

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
$ composer require nilportugues/api-transformer
```

How it works
------------

[](#how-it-works)

Loading must be done using the `Mapper` class, as it expects an array containing a defined structure, or a class name implementing `ApiMapping`.

There are 2 styles for flexibility when integrating the library into PHP frameworks.

While I discourage having 2 styles for mapping it is well possible to have them side by side in the very same configuration file. The `Mapper` class that loads all Mappings does internal transformation for both, so client does not have to worry.

### Usage

[](#usage)

```
use NilPortugues\Api\Mapping\Mapper;
use NilPortugues\AcmeProject\Infrastructure\Api\Mappings\PostApiMapping;

$arrayConfig = include 'mappings.php';
$classConfig = [
	PostApiMapping::class,
];

$mappings = array_merge($classConfig, $arrayConfig);

//Now $mapper can be passed to a Transformer.
$mapper = new Mapper($mappings);
```

Creating the Mapping files
--------------------------

[](#creating-the-mapping-files)

### Implementing ApiMapping (Prefered method)

[](#implementing-apimapping-prefered-method)

To create a Mapping you may implement the following interfaces:

- `ApiMapping`: transform your data into plain JSON for API consumtion or the JSend API format.
- `JsonApiMapping` to transform your data into the JSONAPI 1.0 standard.
- `HalMapping` to transform your data to HAL+JSON and HAL+XML API standards.

As expected you may implement many interfaces to support multiple API formats.

```
