PHPackages                             fabricio872/api-modeller - 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. fabricio872/api-modeller

ActiveSymfony-bundle[API Development](/categories/api)

fabricio872/api-modeller
========================

Library for translating foreign API to Doctrine-like models

v2.2.4(3y ago)02.6k1MITPHPPHP ^7.2|^8.0

Since Nov 25Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Fabricio872/api-modeller)[ Packagist](https://packagist.org/packages/fabricio872/api-modeller)[ RSS](/packages/fabricio872-api-modeller/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (34)Used By (1)

[![GitHub release](https://camo.githubusercontent.com/2b1fbf8ed32def4e348eb2c1c133bf91e10ca02a4f4b678decc4deb7b54b6147/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f466162726963696f3837322f6170692d6d6f64656c6c6572)](https://camo.githubusercontent.com/2b1fbf8ed32def4e348eb2c1c133bf91e10ca02a4f4b678decc4deb7b54b6147/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f466162726963696f3837322f6170692d6d6f64656c6c6572)[![GitHub last commit](https://camo.githubusercontent.com/2dd63789c80533adab86071bf1c7693ddc9b8957ef98cc1d525f152c6353810d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f466162726963696f3837322f6170692d6d6f64656c6c6572)](https://camo.githubusercontent.com/2dd63789c80533adab86071bf1c7693ddc9b8957ef98cc1d525f152c6353810d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f466162726963696f3837322f6170692d6d6f64656c6c6572)[![PHP Composer Test and Tag](https://github.com/Fabricio872/api-modeller/actions/workflows/php-8.0-test.yml/badge.svg)](https://github.com/Fabricio872/api-modeller/actions/workflows/php-8.0-test.yml)[![Packagist Downloads](https://camo.githubusercontent.com/d6963fb864f2ad268fc3b586548940d2ca1e58f0938287ebc699cf8dc952080e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f466162726963696f3837322f6170692d6d6f64656c6c6572)](https://camo.githubusercontent.com/d6963fb864f2ad268fc3b586548940d2ca1e58f0938287ebc699cf8dc952080e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f466162726963696f3837322f6170692d6d6f64656c6c6572)[![GitHub Repo stars](https://camo.githubusercontent.com/3592479bed8cb93635bfb0fdf8d3b0e4929b015fc4555dc281c339875a73b033/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f466162726963696f3837322f6170692d6d6f64656c6c65723f7374796c653d736f6369616c)](https://camo.githubusercontent.com/3592479bed8cb93635bfb0fdf8d3b0e4929b015fc4555dc281c339875a73b033/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f466162726963696f3837322f6170692d6d6f64656c6c65723f7374796c653d736f6369616c)

Valuable partners:

[![PhpStorm logo](https://camo.githubusercontent.com/1e31d98d23a59aa7fddbdae1155126adeac9ad6cded9a478fc8c5d377942c433/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f50687053746f726d2e737667)](https://camo.githubusercontent.com/1e31d98d23a59aa7fddbdae1155126adeac9ad6cded9a478fc8c5d377942c433/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f50687053746f726d2e737667)

Before installation
===================

[](#before-installation)

> If you are using older php then version 7.2 download with command

```
$ composer require fabricio872/api-modeller:^1.0
```

Installation
============

[](#installation)

Make sure Composer is installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md)of the Composer documentation.

### Step 1: Download the Library

[](#step-1-download-the-library)

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

```
$ composer require fabricio872/api-modeller
```

### Step 2: Initialize library

[](#step-2-initialize-library)

Create new instance of `Fabricio872\ApiModeller\Modeller`. For legacy project is easiest to implement it with Singleton pattern which gives you instance anywhere where you call it as described [here](#calling-the-api).

> create new class for client adapter of your choice in this example we use GuzzleHttp/Client

```
use Fabricio872\ApiModeller\ClientAdapter\ClientInterface;
use GuzzleHttp\Client;

class GuzzleClient implements ClientInterface
{
    private Client $client;

    public function __construct()
    {
        $this->client = new Client();
    }

    public function request(string $method, string $endpoint, array $options): string
    {
        return $this->client->request($method, $endpoint, $options)->getBody()->getContents();
    }
}
```

> create new class somewhere in your composer autoload directory with name Modeller and add your namespace

```
use Doctrine\Common\Annotations\AnnotationReader;
use Fabricio872\ApiModeller\ClientAdapter\Symfony;
use Symfony\Component\HttpClient\HttpClient;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class Modeller
{
    /** @var \Fabricio872\ApiModeller\Modeller */
    private static $modeller;

    public static function get()
    {
        if (!isset(self::$modeller)) {
            $reader = new AnnotationReader();

            $client = new GuzzleClient(); // class we have created above

            $loader = new FilesystemLoader();
            $twig = new Environment($loader, [
                'cache' => '/path/to/compilation_cache',
            ]);

            self::$modeller = new \Fabricio872\ApiModeller\Modeller(
                $reader,
                $client,
                $twig
            );
        }
        return self::$modeller;
    }
}
```

Usage
=====

[](#usage)

> This lib uses models with Annotations similar to Doctrine Entities.
>
> Usually they are in a directory `src/ApiModels` but they are not required to be there as long as they have correct namespace

Example model with single Resource
----------------------------------

[](#example-model-with-single-resource)

This is example of a model for receiving list of users from some API

```
// src/ApiModels/Users.php

use Fabricio872\ApiModeller\Annotations\Resource;

/**
 * @Resource(
 *     endpoint="{{api_url}}/api/users",
 *     method="GET",
 *     type="json",
 *     options={
 *          "headers"={
 *              "accept"= "application/json"
 *          }
 *     }
 * )
 */
class Users
{
    public $page;
    public $per_page;
    public $total;
    public $total_pages;
    public $data;
}
```

> endpoint parameter is endpoint which will be called.

> method parameter is method with which the request will be done
>
> default: "GET"

> type parameter defines format of the received data
>
> currently supported: "json", "xml'
>
> default: "json"

> options parameter is array that is directly passed (but can be altered as explained in [setOptions](#setoptions) section) to [symfony/http-client](https://github.com/symfony/http-client) request method as 3. parameter so use [this documentation](https://symfony.com/doc/current/http_client.html)

Example model with multiple Resources
-------------------------------------

[](#example-model-with-multiple-resources)

To define multiple resources you need to wrap multiple Resource annotation into single Resources annotation with identifier at beginning. This identifier is then used while calling this endpoint as described in section [setIdentifier](#setidentifier)

```
// src/ApiModels/Users.php

use Fabricio872\ApiModeller\Annotations\Resource;
use Fabricio872\ApiModeller\Annotations\Resources;

/**
 * @Resources({
 *      "multiple"= @Resource(
 *          endpoint="{{api_url}}/api/users",
 *          method="GET",
 *          type="json",
 *          options={
 *              "headers"={
 *                  "accept"= "application/json"
 *              }
 *          }
 *      ),
 *      "single"= @Resource(
 *          endpoint="{{api_url}}/api/users/{{id}}",
 *          method="GET",
 *          type="json",
 *          options={
 *              "headers"={
 *                  "accept"= "application/json"
 *              }
 *          }
 *      ),
 * })
 */
class Users
{
    public $page;
    public $per_page;
    public $total;
    public $total_pages;
    public $data;
}
```

Calling the API
---------------

[](#calling-the-api)

> Instance of class `Fabricio872\ApiModeller\Modeller` can be received like this if configuration was as described [here](#step-2-initialize-library)

This controller dumps model or collection of models form [this example](#example-model-with-single-resource) with namespace `Users::class`and sets query parameter 'page' to 2

```
// src/Controller/SomeController.php

    public function index()
    {
        var_dump(Modeller::get()->getData(
            Repo::new(Users::class)
                ->setOptions([
                    "query" => [
                        "page" => 2
                    ]
                ])
        ));
    }
```

> Notice `setOptions` have alternative function `addOptions` which merges existing and provided options

> Notice that `Modeller::get()` must have correct namespace pointing to class from [configuration section](#step-2-initialize-library)

This controller dumps model or collection of models form [this example](#example-model-with-multiple-resources) with namespace `Users::class`and fills the {{id}} variable from model with number 2

noticed that now method setIdentifier is required

```
// src/Controller/SomeController.php

    public function index(Modeller $modeller)
    {
        var_dump(Modeller::get()->getData(
            Repo::new(Users::class)
                ->setParameters([
                    "id" => 2
                ])
                ->setIdentifier("single")
        ));
    }
```

> The modeller accepts Repo object which requires namespace of model you want to build and has optional setters:
>
> - setOptions()
> - setParameters()
> - setIdentifier()

### setOptions

[](#setoptions)

This method accepts array of options that will be merged with options configured in a model (and will override overlapped parameters) to [symfony/http-client](https://github.com/symfony/http-client) request method as 3. parameter so use [this documentation](https://symfony.com/doc/current/http_client.html)

### setParameters

[](#setparameters)

This method accepts array and sets twig variables (same as if you render a template but here the template is endpoint parameter from model) to url configuration and can override global twig variables

### setIdentifier

[](#setidentifier)

This method is required in case when you use multiple Resources for single model as shown in [this example](#example-model-with-multiple-resources)

Model Title
-----------

[](#model-title)

Model title is useful if your data arrives covered in another object like this

```
{
  "data": [
    {
      "myData": "data"
    }
  ]
}
```

in this case your model would have title `data` to map your model variable directly to `myData` and not to `data` object:

```
// src/ApiModels/Data.php

use Fabricio872\ApiModeller\Annotations\Resource;
use Fabricio872\ApiModeller\Annotations\ModelTitle;

/**
 * @Resource(
 *     endpoint="{{api_url}}/api/data",
 *     method="GET",
 *     type="json",
 *     options={
 *          "headers"={
 *              "accept"= "application/json"
 *          }
 *     }
 * )
 * @ModelTitle("data")
 */
class Data
{
    public $myData;
}
```

you can also nest ModelTitles to array with multiple options for each title for example:

```
/**
 * @ModelTitle("data", {"subTitle1", "subTitle2"})
 */
```

this will search in incoming response for data and in it for either subTitle1 or subTitle2

###  Health Score

32

—

LowBetter than 71% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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 ~7 days

Recently: every ~37 days

Total

33

Last Release

1410d ago

Major Versions

v0.0.2 → v2.0.12021-11-28

v1.0.7 → v2.0.32021-12-02

v1.0.11 → v2.0.52021-12-05

PHP version history (3 changes)v0.0.1PHP ^7.4|^8.0

v2.0.1PHP ^7.2|^8.0

v1.0.0PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/932c1684f2ac3360d3230023377029d9f7ea388a55dfc509c4f5501e2208ddd4?d=identicon)[Fabricio872](/maintainers/Fabricio872)

---

Top Contributors

[![Fabricio872](https://avatars.githubusercontent.com/u/29705379?v=4)](https://github.com/Fabricio872 "Fabricio872 (36 commits)")

---

Tags

apilibrarymodel

###  Code Quality

TestsPHPUnit

Code StyleECS

### Embed Badge

![Health badge](/badges/fabricio872-api-modeller/health.svg)

```
[![Health](https://phpackages.com/badges/fabricio872-api-modeller/health.svg)](https://phpackages.com/packages/fabricio872-api-modeller)
```

###  Alternatives

[sylius/sylius

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

8.4k5.6M647](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[temporal/sdk

Temporal SDK

4002.2M18](/packages/temporal-sdk)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[netgen/layouts-core

Netgen Layouts enables you to build and manage complex web pages in a simpler way and with less coding. This is the core of Netgen Layouts, its heart and soul.

3689.4k10](/packages/netgen-layouts-core)[chameleon-system/chameleon-base

The Chameleon System core.

1026.5k3](/packages/chameleon-system-chameleon-base)

PHPackages © 2026

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