composer require older777/currex
.env:
CURREX_INTERVAL=60 # Auto-update interval in minutes (default: 60)
CURREX_TIMEOUT=5 # Timeout in seconds (default: 5)
use Older777\CurrEx\Facades\CurrEx;
$rates = CurrEx::getRates(); // Returns associative array of USD to [currency] rates
$eurRate = $rates['EUR']; // Example: 0.85
Older777\CurrEx\Facades\CurrEx (primary entry point).config/currex.php (if published via php artisan vendor:publish).storage/framework/cache/ for cached XML feeds.Fetching Rates
// Get all rates
$allRates = CurrEx::getRates();
// Get specific currency rate
$gbpRate = CurrEx::getRate('GBP');
// Force refresh (bypasses cache)
$freshRates = CurrEx::refreshRates();
Caching & Auto-Update
CURREX_INTERVAL minutes (default: 60).CurrEx::refreshRates() to force an update.Error Handling
if (CurrEx::hasError()) {
$error = CurrEx::getError();
}
Integration with Models
// Example: Convert USD to EUR in a Product model
public function getPriceInEur()
{
return $this->price * CurrEx::getRate('EUR');
}
Custom Rounding
'rounding' => 4, // Custom precision
if (!CurrEx::isRateAvailable('XYZ')) {
// Fallback logic or throw custom exception
}
$currencies = ['EUR', 'GBP', 'JPY'];
$batchRates = CurrEx::getRates($currencies); // Returns subset
Deprecated Laravel Versions
illuminate/cache for caching).Cache Staleness
CURREX_INTERVAL is too long (e.g., 1440 minutes), rates may become outdated.CURREX_INTERVAL=60 for hourly updates in production.XML Feed Failures
CurrEx::setSilentMode(false); // Enable exceptions
try {
$rates = CurrEx::getRates();
} catch (\Exception $e) {
// Log $e->getMessage()
}
Currency Code Sensitivity
'eur' ≠ 'EUR'. Always use uppercase (e.g., 'USD').null for unsupported codes (e.g., 'XYZ').Timeout Issues
CURREX_TIMEOUT may cause delays; low values risk partial data.CURREX_TIMEOUT=5 unless debugging.Publish Config
php artisan vendor:publish --provider="Older777\CurrEx\CurrExServiceProvider"
config/currex.php for rounding, silent mode, or cache drivers.Cache Driver
file, redis).redis for distributed caching in multi-server setups.Testing
$this->app->instance(\Older777\CurrEx\Facades\CurrEx::class, $mockCurrEx);
CurrEx::shouldReturn([]) to simulate empty rates.Rate Conversion Utility Create a helper trait:
trait Convertible
{
public function convertTo(string $targetCurrency, float $amount): float
{
$usdRate = CurrEx::getRate($targetCurrency);
return $amount * $usdRate;
}
}
Fallback Rates Store default rates in config for offline use:
'fallback_rates' => [
'EUR' => 0.85,
'GBP' => 0.75,
],
Then extend the package or override getRate() logic.
Logging Add logging for rate updates:
CurrEx::setLogger(function ($message) {
\Log::info($message, ['package' => 'currex']);
});
Performance
getRates() in loops. Cache results in memory or Redis.$rates = CurrEx::getRates();
foreach ($products as $product) {
$product->eurPrice = $product->usdPrice * $rates['EUR'];
}
How can I help you explore Laravel packages today?