- How do I integrate florianv/swap into a Laravel application for real-time currency conversion?
- Bind the Swap instance in your `AppServiceProvider` using Laravel’s service container. Example: `$this->app->singleton(Swap::class, fn() => (new Builder())->add('fastforex', ['api_key' => config('services.fastforex.key')])->add('european_central_bank')->build());`. Then inject `Swap` into controllers/services via constructor injection. Use `swap->latest('EUR/USD')` for real-time rates.
- Does florianv/swap support Laravel’s caching system (e.g., Redis, database) for exchange rates?
- Yes, Swap uses PSR-16 caching (like Laravel’s `Cache` facade). Configure it via `Cache::store('redis')` or `Cache::store('database')`. Example: `$swap->withCache(Cache::store('redis'))->latest('USD/JPY')`. Cache keys are auto-generated, but you can customize them via `Cache::remember()`.
- What Laravel versions does florianv/swap officially support?
- Swap requires PHP 8.2+ and works with Laravel 9.x/10.x/11.x. It leverages PSR standards (PSR-16, PSR-18) that align with Laravel’s ecosystem. Tested with Laravel’s built-in HTTP client (`HttpClient`) and `Cache` facade. No Laravel-specific dependencies exist, but the companion package `laravel-swap` adds Laravel integration helpers.
- How do I handle provider failures or rate limits in florianv/swap for production?
- Swap supports fallback chains—configure multiple providers in order of priority. Example: `$builder->add('fastforex')->add('european_central_bank')->add('national_bank_of_japan')`. If one fails (e.g., rate limit), it automatically tries the next. Log fallbacks with Laravel’s `Log` facade or monitor via Sentry/New Relic for critical paths.
- Can I store historical exchange rates in a Laravel database using florianv/swap?
- Yes, Swap provides `Rate` objects with metadata (pair, value, date, provider). Create a Laravel migration for a `rates` table with columns like `pair`, `value` (decimal), `date`, and `provider`. Use Eloquent to persist rates: `$rate = $swap->historical('EUR/USD', '2023-01-01'); Rate::create($rate->toArray());`.
- What are the costs associated with using florianv/swap in production, especially for non-EUR currencies?
- Free providers (e.g., ECB) only support EUR-base pairs. Non-EUR pairs require commercial APIs like fastFOREX (paid plans start at $18/month). Document currency coverage gaps in your API contracts. Use free providers for non-critical paths (e.g., dashboards) and mandate paid APIs for invoicing or high-volume transactions.
- How do I test florianv/swap in Laravel with mocked exchange rates?
- Use Laravel’s `Mockery` or `PHPUnit` to mock the `Swap` instance. Example: `$swap = Mockery::mock(Swap::class); $swap->shouldReceive('latest')->andReturn(new Rate('EUR/USD', 1.10));`. For integration tests, use a test cache store (e.g., `Cache::store('array')`) and disable real API calls via environment variables or provider configuration.
- Is florianv/swap suitable for high-frequency trading or low-latency applications?
- Swap’s sequential fallback chain may introduce latency if multiple providers fail. Mitigate this by using parallel polling (`swap->latest('EUR/USD', ['parallel' => true])`) or prioritizing low-latency providers (e.g., fastFOREX). For ultra-low latency, consider caching rates aggressively (e.g., 1-minute TTL) or using a dedicated microservice for rate fetching.
- Are there alternatives to florianv/swap for Laravel currency conversion with better commercial provider support?
- Alternatives include `spatie/currency` (simpler, fewer providers) or `moneyphp/money` (focused on monetary arithmetic). For Laravel-specific solutions, `laravel-money/monetary` integrates with Swap but adds Laravel helpers. Swap’s strength is its multi-provider abstraction and fallback logic, which is harder to replicate with lighter libraries.
- How do I validate exchange rates from florianv/swap to ensure accuracy for financial applications?
- Swap does not validate rates by default. For financial apps, implement custom validation (e.g., check rate ranges against known bounds, cross-validate with multiple providers). Use Laravel’s `Validator` facade or a library like `webmozart/assert` to enforce rules. Log invalid rates and alert stakeholders via Laravel Notifications or Slack.