PHPackages                             mtrn/apiservice - 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. mtrn/apiservice

ActiveLibrary[API Development](/categories/api)

mtrn/apiservice
===============

api service: get api response and map it into any object of your choice.

00PHP

Since Dec 8Pushed 3y ago1 watchersCompare

[ Source](https://github.com/matrianpour/apiprovider)[ Packagist](https://packagist.org/packages/mtrn/apiservice)[ RSS](/packages/mtrn-apiservice/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

ApiService
==========

[](#apiservice)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d3b78a46febfa9effa96b106580f06af9e1a11f3e502621ad66455b881675271/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d74726e2f617069736572766963652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mtrn/apiservice)[![Total Downloads](https://camo.githubusercontent.com/fbe5ad706ee3fbfa638cd905f6bbc5e12749ec7db2345fe9c752f203776d5e12/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d74726e2f617069736572766963652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mtrn/apiservice)[![Build Status](https://camo.githubusercontent.com/6646f8db535cc439e44d9fe4b2d485729665de4b000b03e137402a919a5fb04e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d74726e2f617069736572766963652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/mtrn/apiservice)[![StyleCI](https://camo.githubusercontent.com/cb13a877afd1dbe223c631789c3f922d3ace958fdb334a9cce9b26afefbc2ebd/68747470733a2f2f7374796c6563692e696f2f7265706f732f31323334353637382f736869656c64)](https://styleci.io/repos/12345678)

This package pdrovides an api-service to ease working with different apis. It maps api-responses to objects of your choice by using a trait named IsApiClient. In this way you are free to define different clients for a single api. You'r also able to use diffrent apis for a single client.

let's get a look at structur:

├── [configs](#configs)
│ └── apiservice.php
├── app
│ └── Models
│ └── [Client.php](#clients)
│ └── Services
│ └── ApiService
│ └── [ApiProviders](#apiProviders)
│ └── GoogleApiProvider.php
│ └── [Decorators](#decorators)
│ └──GoogleClientDecorator.php

Configs
-------

[](#configs)

The package's config-file is apiservice.php. Its options are:

##### apis:

[](#apis)

This is an associative array where keys are the name of apis and values are array of configs for the api.

Each api has three attributes:

> url: the api's url
>
> response\_type: the type of api response (currently supported type is json)
>
> data\_access\_keys: it is an associative array where a key is client-name and its value is access-key to the client-related-data.

- default access-key would be the name of the client; like 'user' =&gt; 'user'.
- 'user' =&gt; ' ' indicates that the whole response-body is related to the client user.

##### defaults:

[](#defaults)

this option controls the default. You're free to change them as you want. Its only value it response\_type which is json.

##### path\_to\_decorators:

[](#path_to_decorators)

This value is used to access decorators.

##### path\_to\_apiproviders:

[](#path_to_apiproviders)

This value is used to access to api-providers.

Client.php
----------

[](#clientphp)

This is an example client object which get use of trait **IsApiClient**.
It must implement function getMappedArray() to present its data in array format.

ApiProviders
------------

[](#apiproviders)

All your api-provider classes should be placed in this directory.
An api-provider is used to handle api related functionalities; like sending a request to the api, or choosing a way to extract data from response based on its type.
For each one of your apis you should create an api-provider and name it in format **ApinameApiProvider**.

GoogleApiProvider is an example of api-provider.

**Note:** They must extend ApiProvider abstract and implement method requestFromApi().

Decorators
----------

[](#decorators)

All your decorator classes should be placed in this directory.
A decorator is used to decorate your clients. The nameing format is **ApinameClientnameDecorator**.

**Note:** They must extend Decorator abstract and implement method mapApiData();

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

[](#installation)

Via Composer

```
$ composer require mtrn/apiservice
```

Usage
-----

[](#usage)

lets take a look at the provided example.

> GoogleApiProvider

```
use Mtrn\ApiService\Services\ApiService\ApiProviders\ApiProvider;

class GoogleApiProvider extends ApiProvider
{
    public function requestFromApi()
    {
        return Http::get($this->url);
    }
}

```

> Client

```
use Mtrn\ApiService\Traits\IsApiClient;

class Client
{
    use IsApiClient

    public function getMappedArray()
    {
        return $this->toArray();
    }
}

```

> Decorator

```
use Mtrn\ApiService\Services\ApiService\Decorators\Decorator;

class GoogleClientDecorator extends Decorator
{
    /**
     * Define your map rules here.
     *
     * @param array $data
     * @return object
     */
    public function mapApiData(array $data): object
    {
        $this->client->setAttribute('name', $data['first_name'].' '.$data['last_name']);
        return $this->client;
    }
}

```

> request to google and get response object:

```
$client = new Client();
$response = $client->requestFromApi($apiname='google', $map=false);

```

> request to google and get extracted data

```
$client = new Client();
$client->requestFromApi($amiName='google', $map=false);
$extractedData = $client->getApiBody();

```

> request to google and get mapped data object

```
$client = new Client();
$mappedData = $client->requestFromApi($apiname='google', $map=true);

```

> request to google and get mapped array

```
$client = new Client();
$client->requestFromApi('google', true);
$mappedArray = $client->getMappedArray();

```

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity25

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ccd974bd61eb3cea499f400d2281fdf927846387356c176ad52e64bc1846bdfa?d=identicon)[matrianpour](/maintainers/matrianpour)

---

Top Contributors

[![matrianpour](https://avatars.githubusercontent.com/u/44110168?v=4)](https://github.com/matrianpour "matrianpour (35 commits)")

### Embed Badge

![Health badge](/badges/mtrn-apiservice/health.svg)

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

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M474](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M270](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M186](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M33](/packages/facebook-php-business-sdk)[microsoft/microsoft-graph

The Microsoft Graph SDK for PHP

65723.5M95](/packages/microsoft-microsoft-graph)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)

PHPackages © 2026

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