PHPackages                             oskar/json-api-model - 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. oskar/json-api-model

ActiveLibrary[API Development](/categories/api)

oskar/json-api-model
====================

JSON API model library

0.1.1(10y ago)0221[2 issues](https://github.com/Oskard/json-api-model/issues)MITPHPPHP &gt;=5.4

Since Jun 2Pushed 10y ago1 watchersCompare

[ Source](https://github.com/Oskard/json-api-model)[ Packagist](https://packagist.org/packages/oskar/json-api-model)[ Docs](https://github.com/OskarD/json-api-model)[ RSS](/packages/oskar-json-api-model/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (2)Versions (4)Used By (0)

JsonApiModel
============

[](#jsonapimodel)

Extension to Art4's JsonApiClient

Builds models from a JSON API v1.0-compliant response from JsonApiClient.

Setup
-----

[](#setup)

### Classes

[](#classes)

Create classes that extend `ApiModel` that will be represent the remote resources. For instance, if a resource is called "article", make an `Article` class.

You can also use `ApiModelInterface` to make your own `ApiModel` variants.

### Type Factories

[](#type-factories)

You can add your own factories for specific `type`s.

This is useful, for instance, when you want to implement inheritance in your class structure. Let's say you have a `CreditCard` class and a `DebitCard` class that both extend the `Card` class, but the API returns the same type with different attributes.

```
{
   "id": 1
   "type": "card",
   "attributes": {
        "cardtype": "credit"
   }
}

..

{
   "id": 2
   "type": "card",
   "attributes": {
        "cardtype": "debit"
   }
}

```

You can then extend `OskarD\JsonApiModel\Factories\TypeFactory` and make your own `CardFactory` class.

```
namespace MyApp\TypeFactories;

use OskarD\JsonApiModel\Factories\TypeFactory;

class CardFactory
{
    /**
     * Builds an instance of the type.
     *
     * @param $attributes
     * @return ApiModel
     */
    public function build($attributes)
    {
        if($attributes['cardtype'] == 'credit') {
            return new CreditCard($attributes);
        }

        return new DebitCard($attributes);
    }
}

```

You also have to set the factory in the ApiModelFactory context.

```
$apiModelFactory->setTypeFactories([
    'card' => MyApp\TypeFactories\CardFactory::class,
]);

```

Usage
-----

[](#usage)

### Create The Factory

[](#create-the-factory)

Create the ApiModelFactory by passing the document returned from JsonApiClient's `parse` function (`$document`).

```
use Art4\JsonApiClient\Utils\Helper;
use OskarD\JsonApiModel\Factories\ApiModelFactory;

..

$document = Helper::parse($resource);
$apiModelFactory = new ApiModelFactory($document);

```

### Namespacing (Optional)

[](#namespacing-optional)

If your `ApiModelInterface` classes exist outside the root namespace, you need to provide them. You can do this by setting the default namespace, by mapping them individually, or both.

#### Set Default Namespace

[](#set-default-namespace)

If most or all of your `ApiModelInterface` classes exist in the same namespace, you can avoid mapping them by setting the default namespace for the factory to theirs.

```
$apiModelFactory->setDefaultNamespace('MyApp\\ApiModels\\');

```

#### Map types

[](#map-types)

If one or more of your `ApiModelInterface` classes exist outside the default namespace, you can map them individually.

```
$apiModelFactory->setMappedTypes([
    'articles' => Article::class,
    'people'   => Person::class,
    'comments' => Comment::class,
]);

```

### Building

[](#building)

Building the `Document` will result in an array containing any resources provided.

```
$result = $apiModelFactory->build();

```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

3681d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6658382?v=4)[Oskar Danielsson](/maintainers/OskarD)[@OskarD](https://github.com/OskarD)

---

Top Contributors

[![OskarD](https://avatars.githubusercontent.com/u/6658382?v=4)](https://github.com/OskarD "OskarD (9 commits)")

---

Tags

jsonapimodelJSON-API

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/oskar-json-api-model/health.svg)

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

###  Alternatives

[neomerx/json-api

Framework agnostic JSON API (jsonapi.org) implementation

7433.7M30](/packages/neomerx-json-api)[cloudcreativity/laravel-json-api

JSON API (jsonapi.org) support for Laravel applications.

7881.1M5](/packages/cloudcreativity-laravel-json-api)[art4/json-api-client

JSON API client

138801.0k8](/packages/art4-json-api-client)[alsvanzelf/jsonapi

Human-friendly library to implement JSON:API without needing to know the specification.

55155.4k6](/packages/alsvanzelf-jsonapi)[cloudcreativity/json-api-testing

PHPUnit test helpers to check JSON API documents.

141.7M4](/packages/cloudcreativity-json-api-testing)[joskolenberg/laravel-jory

Create a flexible API for your Laravel application using json based queries.

4513.9k](/packages/joskolenberg-laravel-jory)

PHPackages © 2026

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