setono / economic-php-sdk
Consume the Economic API with this PHP SDK
v0.2.1
2024-02-23 09:03 UTC
Requires
- php: >=8.1
- cuyz/valinor: ^1.9
- php-http/discovery: ^1.19
- psr/http-client: ^1.0
- psr/http-client-implementation: ^1
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- infection/infection: ^0.27
- nyholm/psr7: ^1.8
- phpspec/prophecy-phpunit: ^2.1
- phpunit/phpunit: ^9.6
- psalm/plugin-phpunit: ^0.18
- setono/code-quality-pack: ^2.7
- symfony/http-client: ^6.4 || ^7.0
This package is auto-updated.
Last update: 2024-10-23 10:17:40 UTC
README
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.