andrejbarna / demo-php-api-client
A simple PHP API client library for rate calculations
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/andrejbarna/demo-php-api-client
Requires
- php: ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-12-08 15:20:56 UTC
README
A simple and lightweight PHP API client library for rate calculations based on API key characteristics and currency types.
Installation
Install via Composer:
composer require andrejbarna/demo-php-api-client
Usage
Basic Usage
<?php require_once 'vendor/autoload.php'; use AndrejBarna\DemoPhpApiClient\ApiClient; // Initialize with API key $client = new ApiClient('YOUR_API_KEY'); // Get rate calculation $rate = $client->getRate('USD', 100.0); echo "Rate: $rate";
Rate Calculation Logic
The library calculates rates based on the first character of your API key:
- Uppercase letter:
value × 115 × currency_multiplier - Lowercase letter:
value × 58 × currency_multiplier - Other characters:
value × 1 × currency_multiplier
Supported Currencies
The library includes built-in currency multipliers for enhanced calculations:
- USD: 1.0 (base)
- EUR: 0.85
- GBP: 0.75
- JPY: 150.0
- CAD: 1.35
- AUD: 1.50
- CHF: 0.90
- CNY: 7.25
- RSD: 110.0
- SEK: 11.0
- NOK: 11.5
- DKK: 6.9
Unsupported currencies default to a multiplier of 1.0.
Examples
// Uppercase API key example $client1 = new ApiClient('MYAPIKEY123'); $rate1 = $client1->getRate('USD', 100); // 100 × 115 × 1.0 = 11,500 // Lowercase API key example $client2 = new ApiClient('myapikey123'); $rate2 = $client2->getRate('USD', 100); // 100 × 58 × 1.0 = 5,800 // Different currency example $client3 = new ApiClient('MYAPIKEY123'); $rate3 = $client3->getRate('EUR', 100); // 100 × 115 × 0.85 = 9,775
Additional Methods
// Get supported currencies $currencies = $client->getSupportedCurrencies(); // Validate API key (checks if length >= 8) $isValid = $client->validateApiKey(); // Get API key information (masked for security) $info = $client->getApiKeyInfo();
API Reference
__construct(string $apiKey)
Creates a new API client instance.
- Parameters:
$apiKey- The API key for authentication - Throws:
InvalidArgumentExceptionif API key is empty
getRate(string $currency, float $value): float
Calculates the rate for a given currency and value.
- Parameters:
$currency- Currency code (e.g., 'USD', 'EUR')$value- Base value to convert
- Returns: Calculated rate (rounded to 2 decimal places)
- Throws:
InvalidArgumentExceptionif value is negative
getSupportedCurrencies(): array
Returns an array of supported currency codes.
validateApiKey(): bool
Validates the API key format (must be at least 8 characters).
getApiKeyInfo(): array
Returns masked API key information for debugging/logging purposes.
Testing
Run tests using PHPUnit:
vendor/bin/phpunit tests/
Requirements
- PHP 8.0 or higher
License
MIT License
Author
Andrej Barna