PHPackages                             happyr/json-api-response-factory - 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. happyr/json-api-response-factory

ActiveLibrary[API Development](/categories/api)

happyr/json-api-response-factory
================================

Response factory in compliance with apijson

0.6.0(2y ago)979.7k↓50%3[1 PRs](https://github.com/Happyr/json-api-response-factory/pulls)MITPHPPHP &gt;=7.4

Since Jul 25Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Happyr/json-api-response-factory)[ Packagist](https://packagist.org/packages/happyr/json-api-response-factory)[ GitHub Sponsors](https://github.com/Nyholm)[ RSS](/packages/happyr-json-api-response-factory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (10)Used By (0)

JsonApi Response factory
========================

[](#jsonapi-response-factory)

[![Latest Version](https://camo.githubusercontent.com/9e70aac53ea4998438eeb3d2901dca1f9cb239321b3aa02c09906e2227f65fa9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6861707079722f6a736f6e2d6170692d726573706f6e73652d666163746f72792e7376673f7374796c653d666c61742d737175617265)](https://github.com/happyr/json-api-response-factory/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/50bec5508f8d583da9c29f015cd4d25fa27fbcd04e7cf9a0e369a6de4bcc2595/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6861707079722f6a736f6e2d6170692d726573706f6e73652d666163746f72792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/happyr/json-api-response-factory)

A small wrapper around `league/fractal` to support JsonApi error AND success responses.

Install
-------

[](#install)

```
composer require happyr/json-api-response-factory

```

Usage
-----

[](#usage)

`ResponseFactory` can be used for creating single object, collection of objects or custom responses.

### Transformers

[](#transformers)

Each object that is used in the response needs a transformer that implements `Happyr\JsonApiResponseFactory\Transformer\AbstractTransformer`:

```
use Happyr\JsonApiResponseFactory\Transformer\AbstractTransformer;

final class FooTransformer extends AbstractTransformer
{
   public function getResourceName(): string
   {
       return 'foo';
   }

   public function transform(Foo $item): array
   {
       return [
           'id' => $item->getId(),
           'bar' =>  (string)$item->getBar(),
       ];
   }
}
```

### Response with single item

[](#response-with-single-item)

```
$item = new Foo('bar');
$response = $responseFactory->createWithItem($item, new FooTransformer());
```

Response will look like this:

```
{
    "data": {
        "type": "foo",
        "id": "1",
        "attributes": {
            "bar": "bar"
        }
    }
}
```

### Response with collection of items

[](#response-with-collection-of-items)

```
$items = [
    new Foo('bar'),
    new Foo('baz'),
];
$response = $responseFactory->createWithCollection($items, new FooTransformer());
```

Response will look like this:

```
{
    "data": [
        {
            "type": "foo",
            "id": "1",
            "attributes": {
                "bar": "bar"
            }
        },
        {
            "type": "foo",
            "id": "2",
            "attributes": {
                "bar": "baz"
            }
        }
    ]
}
```

### Custom responses

[](#custom-responses)

To use response `ResponseFactory` to create response with custom payload/status codes you should create class that implements `Happyr\JsonApiResponseFactory\ResponseModelInterface`:

```
use Happyr\JsonApiResponseFactory\ResponseModelInterface;

final class InvalidRequestResponseModel implements ResponseModelInterface
{
   public function getHttpStatusCode() : int
    {
        return 400;
    }

    public function getPayload() : array
    {
        return [
            'error' => 'Invalid request.',
        ];
    }
}
```

and pass it to response factory:

```
$model = new InvalidRequestResponseModel();
$response = $responseFactory->createWithResponseModel($model);
```

Response will look lie this:

```
{
    "error": "Invalid request."
}
```

In `src/Model/` there are models for usual message responses (accepted, created etc), and error responses in compliance with json-api error standard that you can use, or take a hint how we are using the library and write your own models.

Example response for message:

```
{
    "meta": {
        "message": "Accepted"
    }
}
```

Example response for validation failed:

```
{
  "errors": [
    {
      "status": "400",
      "title": "Validation failed",
      "detail": "This value should not be blank.",
      "source": {
        "parameter": "foo",
      },
      "links": {
        "about": "http://docs.docs/errors/missing-parameter"
      }
    },
    {
      "status": "400",
      "title": "Validation failed",
      "detail": "This value has to be larger than 30.",
      "source": {
        "parameter": "bar",
      },
      "links": {
        "about": "http://docs.docs/errors/range"
      }
    }
  ]
}
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50.9% 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 ~207 days

Recently: every ~294 days

Total

8

Last Release

1034d ago

PHP version history (4 changes)0.1.0PHP ^7.1.3

0.2.0PHP ^7.2

0.3.0PHP &gt;=7.2

0.5.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/401ccc5eea13c60cf807ae982af00e368e2166e2f26d8eb541dcd881a57385bc?d=identicon)[Nyholm](/maintainers/Nyholm)

![](https://www.gravatar.com/avatar/5bbb02667f759341b253837a99a4074a873b363098b1f28895e9e9cf6b0e84f1?d=identicon)[aradoje](/maintainers/aradoje)

---

Top Contributors

[![Nyholm](https://avatars.githubusercontent.com/u/1275206?v=4)](https://github.com/Nyholm "Nyholm (28 commits)")[![aradoje](https://avatars.githubusercontent.com/u/30075502?v=4)](https://github.com/aradoje "aradoje (24 commits)")[![carlos-ea](https://avatars.githubusercontent.com/u/5512089?v=4)](https://github.com/carlos-ea "carlos-ea (1 commits)")[![jongotlin](https://avatars.githubusercontent.com/u/165154?v=4)](https://github.com/jongotlin "jongotlin (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/happyr-json-api-response-factory/health.svg)

```
[![Health](https://phpackages.com/badges/happyr-json-api-response-factory/health.svg)](https://phpackages.com/packages/happyr-json-api-response-factory)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[statamic/cms

The Statamic CMS Core Package

4.8k3.2M720](/packages/statamic-cms)[stfalcon-studio/api-bundle

Base classes and helper services to build API application via Symfony.

1032.1k](/packages/stfalcon-studio-api-bundle)[mapado/rest-client-sdk

Rest Client SDK for hydra API

1125.9k2](/packages/mapado-rest-client-sdk)

PHPackages © 2026

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