setono/economic-php-sdk

Consume the Economic API with this PHP SDK

v0.2.1 2024-02-23 09:03 UTC

This package is auto-updated.

Last update: 2024-04-23 09:23:21 UTC


README

Latest Version Software License Build Status Code Coverage Mutation testing

Consume the E-conomic API in PHP.

Installation

composer require setono/economic-php-sdk

Usage

Get a collection

<?php
use Setono\Economic\Client\Client;

$client = new Client('demo', 'demo');

$products = $client
    ->products()
    ->get(filter: 'name$like:b', sortBy: 'name')
;

print_r($products);

will output something like:

Setono\Economic\Response\Collection\Collection Object
(
    [collection] => Array
        (
            [0] => Setono\Economic\Response\Product\Product Object
                (
                    [productNumber] => 2
                    [name] => Barred product
                    [salesPrice] => 50
                )

            [1] => Setono\Economic\Response\Product\Product Object
                (
                    [productNumber] => 5
                    [name] => Fountain Pen, Blue
                    [salesPrice] => 30
                )

            [2] => Setono\Economic\Response\Product\Product Object
                (
                    [productNumber] => 1
                    [name] => Noname T-shirt Black
                    [salesPrice] => 70
                )

            [3] => Setono\Economic\Response\Product\Product Object
                (
                    [productNumber] => 3
                    [name] => SIlk Fabric
                    [salesPrice] => 50
                )

        )

    [pagination] => Setono\Economic\Response\Pagination\Pagination Object
        (
            [maxPageSizeAllowed] => 1000
            [skipPages] => 0
            [pageSize] => 20
            [results] => 4
            [resultsWithoutFilter] => 7
            [firstPage] => Setono\Economic\Response\Pagination\Page Object
                (
                    [endpoint] => products
                    [skipPages] => 0
                    [pageSize] => 20
                    [url] => https://restapi.e-conomic.com/products?skippages=0&pagesize=20&filter=name%24like%3Ab&sort=name
                )

            [lastPage] => Setono\Economic\Response\Pagination\Page Object
                (
                    [endpoint] => products
                    [skipPages] => 0
                    [pageSize] => 20
                    [url] => https://restapi.e-conomic.com/products?skippages=0&pagesize=20&filter=name%24like%3Ab&sort=name
                )

            [nextPage] =>
        )

)

Paginate

<?php
use Setono\Economic\Client\Client;

$client = new Client('demo', 'demo');

$skipPages = 0;

do {
    $products = $client
        ->products()
        ->get(skipPages: $skipPages++);
} while(!$products->isEmpty());

Other requests

If the endpoint or method you want to call isn't present yet, you have two options: 1) Create a PR and add the missing parts or 2) use the generic request method:

<?php

use Setono\Economic\Client\Client;

require_once '../vendor/autoload.php';

$client = new Client('API_KEY', 'API_SECRET');

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $client->request(/** @var \Psr\Http\Message\RequestInterface $request */ $request);

Production usage

Internally this library uses the CuyZ/Valinor library which is particularly well suited for turning API responses in DTOs. However, this library has some overhead and works best with a cache enabled.

When you instantiate the Client you can provide a MapperBuilder instance. Use this opportunity to set a cache:

<?php

use CuyZ\Valinor\Cache\FileSystemCache;use CuyZ\Valinor\MapperBuilder;use Setono\Economic\Client\Client;use Setono\Economic\DTO\Box;

require_once '../vendor/autoload.php';

$cache = new FileSystemCache('path/to/cache-directory');
$client = new Client('API_KEY', 'API_SECRET', (new MapperBuilder())->withCache($cache));

You can read more about it here: Valinor: Performance and caching.