- How do I install this package in a Laravel project?
- Run `composer require mesilov/moneyphp-percentage` in your project root. Ensure your Laravel app uses PHP 8.0+ and MoneyPHP v3.0+ (install via `composer require moneyphp/money`). No additional Laravel-specific setup is required.
- Can I use this package without MoneyPHP? What happens if I don’t have it installed?
- No, this package *requires* MoneyPHP as a dependency. If you don’t already use `moneyphp/money`, installing this package will pull it in automatically. Avoid using this package if you’re not already committed to MoneyPHP’s immutable value objects.
- How does this handle floating-point precision issues with percentages (e.g., 19.9% VAT)?
- The package relies on MoneyPHP’s exact arithmetic, but floating-point percentages (e.g., 19.9%) may still introduce rounding errors. Use `Money::ofExact()` for critical calculations or implement custom rounding logic via MoneyPHP’s `round()` methods.
- Is this package compatible with Laravel 10+ and PHP 8.1/8.2?
- Yes, the package requires PHP 8.0+ and works with Laravel 10+. It leverages PHP 8+ features like named arguments and union types, making it a solid fit for modern Laravel applications. Test thoroughly with your PHP version.
- What’s the best way to apply a percentage (e.g., 10% discount) to a Money object?
- Use the `Percentage::of(10)->applyTo($money)` chain. Example: `$discount = Percentage::of(10)->applyTo(Money::USD(100));` returns `Money::USD(10)`. This ensures type safety and precision throughout your app.
- Are there alternatives to this package for percentage calculations in Laravel?
- If you’re not using MoneyPHP, consider `league/math` for broader math operations or raw `bcmath` for precision. For MoneyPHP users, this is the most specialized option, but you could also roll your own value object with MoneyPHP’s core.
- How do I test this package in a Laravel application?
- Mock `Money` and `Percentage` objects in your tests using PHPUnit. Test edge cases like negative percentages, zero values, and fractional cents. Example: `assertEquals(Money::of(5, 'USD'), Percentage::of(5)->applyTo(Money::of(100, 'USD')));`.
- Will this package work in production for high-volume financial transactions?
- Yes, but validate precision handling for your use case (e.g., tax calculations). The package is stateless and thread-safe, but ensure your MoneyPHP configuration (e.g., rounding rules) aligns with legal requirements for your region.
- Can I use this for dynamic pricing tiers (e.g., bulk discounts)?
- Absolutely. Chain percentages or combine with MoneyPHP’s arithmetic. Example: `$finalPrice = $basePrice->subtract(Percentage::of(15)->applyTo($basePrice))->add(Percentage::of(5)->applyTo($basePrice));` for a 15% discount + 5% fee.
- How do I handle negative percentages (e.g., refunds or credits)?
- Pass a negative value to `Percentage::of(-10)` for refunds or credits. The package validates inputs but relies on MoneyPHP’s behavior for negative amounts. Test thoroughly to ensure compliance with your business rules.