Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
<h1>Openapi® client for PHP</h1>
<h4>The perfect starting point to integrate <a href="https://openapi.com/">Openapi®</a> within your PHP project</h4>

[![Build Status](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml/badge.svg)](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml)
[![Packagist Version](https://img.shields.io/packagist/v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
[![PHP Version](https://img.shields.io/packagist/php-v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
[![License](https://img.shields.io/github/license/openapi/openapi-php-sdk?v=2)](LICENSE)
[![Downloads](https://img.shields.io/packagist/dt/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
<br>
[![Build Status](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml/badge.svg)](https://github.com/openapi/openapi-php-sdk/actions/workflows/php.yml)
[![Packagist Version](https://img.shields.io/packagist/v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
[![PHP Version](https://img.shields.io/packagist/php-v/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
[![License](https://img.shields.io/github/license/openapi/openapi-php-sdk?v=2)](LICENSE)
[![Downloads](https://img.shields.io/packagist/dt/openapi/openapi-sdk)](https://packagist.org/packages/openapi/openapi-sdk)
<br>
[![Linux Foundation Member](https://img.shields.io/badge/Linux%20Foundation-Silver%20Member-003778?logo=linux-foundation&logoColor=white)](https://www.linuxfoundation.org/about/members)

</div>

## Overview
Expand All @@ -27,9 +28,10 @@ Before using the Openapi PHP Client, you will need an account at [Openapi](https

- **Agnostic Design**: No API-specific classes, works with any Openapi service
- **Minimal Dependencies**: Only requires PHP 8.0+ and cURL
- **OAuth Support**: Built-in OAuth client for token management
- **OAuth Support**: Built-in OAuth client for token management
- **HTTP Primitives**: GET, POST, PUT, DELETE, PATCH methods
- **Clean Interface**: Similar to the Rust SDK design
- **Static Analysis**: PHPStan level 6 configuration available via Composer

## What you can do

Expand Down Expand Up @@ -81,7 +83,7 @@ $client = new Client($token);
$params = ['denominazione' => 'Stellantis', 'provincia' => 'TO'];
$response = $client->get('https://test.company.openapi.com/IT-advanced', $params);

// POST request
// POST request
$payload = ['limit' => 10, 'query' => ['country_code' => 'IT']];
$response = $client->post('https://test.postontarget.com/fields/country', $payload);

Expand Down Expand Up @@ -134,6 +136,17 @@ composer run test
composer run test:unit
```

## Static Analysis

This SDK includes PHPStan as a Composer development dependency to help keep the codebase type-safe and maintainable.

PHPStan is configured in `phpstan.neon` and currently runs at level 6.

Run static analysis with:

```bash
composer run analyse
```

## Contributing

Expand Down Expand Up @@ -165,9 +178,9 @@ Meet our partners using Openapi or contributing to this SDK:

## Our Commitments

We believe in open source and we act on that belief. We became Silver Members
of the Linux Foundation because we wanted to formally support the ecosystem
we build on every day. Open standards, open collaboration, and open governance
We believe in open source and we act on that belief. We became Silver Members
of the Linux Foundation because we wanted to formally support the ecosystem
we build on every day. Open standards, open collaboration, and open governance
are part of how we work and how we think about software.

## License
Expand All @@ -179,4 +192,3 @@ The MIT License is a permissive open-source license that allows you to freely us
In short, you are free to use this SDK in your personal, academic, or commercial projects, with minimal restrictions. The project is provided "as-is", without any warranty of any kind, either expressed or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement.

For more details, see the full license text at the [MIT License page](https://choosealicense.com/licenses/mit/).

4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
},
"require-dev": {
"symfony/dotenv": "^5.3",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^2.1"
},
"autoload": {
"psr-4": {
Expand All @@ -67,6 +68,7 @@
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon",
"test": "phpunit tests/",
"test:unit": "phpunit tests/ --testsuite=unit",
"example:token": "php examples/token_generation.php",
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 6
paths:
- src
- tests
6 changes: 6 additions & 0 deletions src/Cache/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

class ArrayCache implements CacheInterface
{
/**
* @var array<string, mixed>
*/
private array $cache = [];
/**
* @var array<string, int>
*/
private array $expiry = [];

public function get(string $key): mixed
Expand Down
14 changes: 14 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public function __construct(?string $token = null, HttpTransportInterface|PsrCli
$this->transport = $transport ?? new CurlTransport($this->token);
}

/**
* Execute HTTP request
*
* @param string $method HTTP method (GET, POST, PUT, DELETE, PATCH)
* @param string $url Target URL
* @param mixed $payload Request body (for POST/PUT/PATCH)
* @param array<string, scalar|null>|null $params Query parameters (for GET) or form data (for other methods)
* @return string Response body
*/
public function request(
string $method,
string $url,
Expand All @@ -34,6 +43,11 @@ public function request(
return $this->transport->request($method, $url, $payload, $params);
}

/**
* Execute GET request
*
* @param array<string, scalar|null>|null $params Query parameters
*/
public function get(string $url, ?array $params = null): string
{
return $this->request('GET', $url, null, $params);
Expand Down
5 changes: 4 additions & 1 deletion src/Interfaces/HttpTransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

interface HttpTransportInterface
{
/**
* @param array<string, mixed>|string|null $params
*/
public function request(
string $method,
string $url,
mixed $payload = null,
?array $params = null
array|string|null $params = null
): string;
}
7 changes: 7 additions & 0 deletions src/OauthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function getScopes(bool $limit = false): string
return $this->request('GET', $url);
}

/**
* @param list<string> $scopes
*/
public function createToken(array $scopes, int $ttl = 3600): string
{
$body = [
Expand Down Expand Up @@ -56,6 +59,10 @@ public function getCounters(string $period, string $date): string
return $this->request('GET', $this->url . '/counters/' . $period . '/' . $date);
}


/**
* @param array<string, mixed>|null $body
*/
private function request(string $method, string $url, ?array $body = null): string
{
$ch = curl_init();
Expand Down
2 changes: 1 addition & 1 deletion src/Transports/CurlTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function request(
string $method,
string $url,
mixed $payload = null,
?array $params = null
array|string|null $params = null
): string {
if ($params && $method === 'GET') {
$url .= '?' . http_build_query($params);
Expand Down
7 changes: 5 additions & 2 deletions tests/Transports/FakeTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ final class FakeTransport implements HttpTransportInterface
public ?string $lastMethod = null;
public ?string $lastUrl = null;
public mixed $lastPayload = null;
public ?array $lastParams = null;
/**
* @var array<string, mixed>|string|null
*/
public array|string|null $lastParams = null;
public int $callCount = 0;

public function request(
string $method,
string $url,
mixed $payload = null,
?array $params = null
array|string|null $params = null
): string {
$this->callCount++;
$this->lastMethod = $method;
Expand Down
Loading