PHPackages                             e0ipso/shaper - 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. e0ipso/shaper

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

e0ipso/shaper
=============

Lightweight library to handle in and out transformations in PHP.

1.2.7(1y ago)157.8M—4.3%5[1 issues](https://github.com/e0ipso/shaper/issues)3GPL-2.0PHP

Since Feb 27Pushed 1y ago2 watchersCompare

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

READMEChangelog (3)Dependencies (4)Versions (19)Used By (3)

[![Coverage Status](https://camo.githubusercontent.com/e3a738777dcf56eb0a9218a4c12b6026a5de4272d51c646937ab5ee5019faa01/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f65306970736f2f7368617065722f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/e0ipso/shaper?branch=master) [![Build Status](https://camo.githubusercontent.com/b188604e580fcd494dca609f3db428b9de173a96459471dcbccb32a6ceb470b9/68747470733a2f2f7472617669732d63692e6f72672f65306970736f2f7368617065722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/e0ipso/shaper)

Shaper
======

[](#shaper)

Shaper is a simple library that enables type safe data transformations. You can either have simple transformations or queued transformations.

One-way Transformations
-----------------------

[](#one-way-transformations)

One way transformations are useful when data only flows in one direction. An example of this is a read-only HTTP API that based on the contents of one property in the output will make a request to an external service to include extra data in the original response.

### Simple Transformations

[](#simple-transformations)

Performs a single operation to transform the data in a type safe manner.

```
use JsonSchema\Validator;
use Shaper\Transformation\TransformationBase;
use Shaper\Util\Context;
use Shaper\Validator\JsonSchemaValidator;

class NumberToArray extends TransformationBase {
  public function getInputValidator() {
    return new JsonSchemaValidator(['type' => 'number'], new Validator());
  }
  public function getOutputValidator() {
    $schema = ['type' => 'array', 'items' => ['type' => 'number']];
    return new JsonSchemaValidator($schema, new Validator());
  }
  protected function doTransform($data, Context $context) {
    return [$context['keyName'] => $data];
  }
}

$t = new NumberToArray();
$t->transform(42, new Context(['keyName' => 'data'])); // ['data' => 42]
$t->transform(['foo']); // TypeError exception.
```

### Queued Transformations

[](#queued-transformations)

Performs a set of operations, one after the other in a type safe manner.

```
use JsonSchema\Validator;
use Shaper\Transformation\TransformationBase;
use Shaper\Util\Context;
use Shaper\Validator\InstanceofValidator;
use Shaper\Validator\JsonSchemaValidator;
use Shaper\Transformation\TransformationsQueue;

class ObjectToNumber extends TransformationBase {
  public function getInputValidator() {
    return new InstanceofValidator(\stdClass::class);
  }
  public function getOutputValidator() {
    $schema = ['type' => 'number'];
    return new JsonSchemaValidator($schema, new Validator());
  }
  protected function doTransform($data, Context $context) {
    return isset($data->value) ? $data->value : 0;
  }
}

$t = new TransformationsQueue();
$t->add(new ObjectToNumber());
$t->add(new NumberToArray());
$input = new \stdClass();
$input->value = 42;
$t->transform($input, new Context(['keyName' => 'data'])); // ['data' => 42]
```

Two-way Transformations (aka Data Adaptor)
------------------------------------------

[](#two-way-transformations-aka-data-adaptor)

Data adaptors, or reversible transformations, are useful when the data can flow in both directions. An example of this is an HTTP API that exposes an internal storage. However the internal storage format is not the format that the API wants to expose. For that data coming in needs to be transformed into the internal storage format, and data coming out needs to be transformed into the shape we want to expose to the outside world via our API.

```
use JsonSchema\Validator;
use Shaper\DataAdaptor\DataAdaptorBase;
use Shaper\Util\Context;
use Shaper\Validator\InstanceofValidator;
use Shaper\Validator\JsonSchemaValidator;

class MyDataAdaptor extends DataAdaptorBase {
  protected function doTransform($data, Context $context) {
    return $data->{$context['keyName']};
  }
  protected function doUndoTransform($data, Context $context) {
    return (object) [$context['keyName'] => $data, 'bar' => 'default'];
  }
  public function getInputValidator() {
    // In a real world scenario we would describe this as a JSON Schema.
    return new InstanceofValidator(\stdClass::class);
  }
  public function getInternalValidator() {
    // In a real world this would be your internal data object. Something like cheking that this is
    // an object of class FieldItemInstance.
    return new JsonSchemaValidator(['type' => 'string'], new Validator());
  }
  public function getOutputValidator() {
    // In a real world scenario we would describe this as a JSON Schema. In most cases the output
    // validator is the same as the input validator, so you can return the input validator here.
    return new InstanceofValidator(\stdClass::class);
  }
}

$da = new MyDataAdaptor();
$data = new \stdClass();
$data->lorem = 'caramba!';
$da->transform($data, new Context(['keyName' => 'lorem'])); // 'caramba!'
$da->transform(new NodeObject()); // TypeError exception.
$da->undoTransform('caramba!', new Context(['keyName' => 'lorem'])); // (object) ['lorem' => 'caramba!', 'bar' => 'default']
$da->undoTransform([]); // TypeError exception.
```

Class Diagram
-------------

[](#class-diagram)

[![Class Diagram](./shaper.png)](./shaper.png)

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance45

Moderate activity, may be stable

Popularity54

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 58.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~162 days

Recently: every ~519 days

Total

17

Last Release

410d ago

Major Versions

0.0.7 → 1.0.02018-03-09

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1140906?v=4)[Mateu Aguiló Bosch](/maintainers/e0ipso)[@e0ipso](https://github.com/e0ipso)

---

Top Contributors

[![e0ipso](https://avatars.githubusercontent.com/u/1140906?v=4)](https://github.com/e0ipso "e0ipso (7 commits)")[![BackEndTea](https://avatars.githubusercontent.com/u/14289961?v=4)](https://github.com/BackEndTea "BackEndTea (1 commits)")[![jurgenhaas](https://avatars.githubusercontent.com/u/1475675?v=4)](https://github.com/jurgenhaas "jurgenhaas (1 commits)")[![mxr576](https://avatars.githubusercontent.com/u/1755573?v=4)](https://github.com/mxr576 "mxr576 (1 commits)")[![ndobromirov](https://avatars.githubusercontent.com/u/1191506?v=4)](https://github.com/ndobromirov "ndobromirov (1 commits)")[![ptmkenny](https://avatars.githubusercontent.com/u/1451472?v=4)](https://github.com/ptmkenny "ptmkenny (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/e0ipso-shaper/health.svg)

```
[![Health](https://phpackages.com/badges/e0ipso-shaper/health.svg)](https://phpackages.com/packages/e0ipso-shaper)
```

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.4k187.2M2.6k](/packages/composer-composer)[ergebnis/composer-normalize

Provides a composer plugin for normalizing composer.json.

1.1k37.3M2.1k](/packages/ergebnis-composer-normalize)[getdkan/dkan

DKAN Open Data Catalog

385135.4k2](/packages/getdkan-dkan)[nfephp-org/sped-esocial

e-Social library

18030.7k](/packages/nfephp-org-sped-esocial)[optimizely/optimizely-sdk

Optimizely PHP SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts

191.8M1](/packages/optimizely-optimizely-sdk)[helmich/schema2class

Build PHP classes from JSON schema definitions

36142.2k](/packages/helmich-schema2class)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
