Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Php Percent

Php Percent Laravel Package

assoconnect/php-percent

Small PHP library to represent and work with percentages in a way that pairs well with MoneyPHP, based on Frederik Bosch’s proposal. Install via Composer and use the PHPDoc-documented API to apply percent calculations safely alongside Money values.

View on GitHub
Deep Wiki
Context7

PHP object to handle percentage with MoneyPHP

Frequently asked questions about Php Percent
How do I integrate **assoconnect/php-percent** with Laravel’s Money facade or laravel-money package?
The package works seamlessly with MoneyPHP, which laravel-money wraps. Install both (`composer require assoconnect/php-percent moneyphp/money laravel-money/laravel-money`), then use the Percent class directly with Money objects. Example: `$percent = new Percent(10.5, Money::EUR(100));` will return a Money object for €10.50 when calling `apply()`.
Does this package support Laravel 10+ and PHP 8.1+?
Yes, **assoconnect/php-percent** is fully compatible with Laravel 10+ and PHP 8.1+. The package requires MoneyPHP 3.0+, which also aligns with Laravel’s modern PHP versions. No Laravel-specific dependencies exist beyond MoneyPHP.
How do I handle user input like '10.5%' strings and convert them to Percent objects?
Strip the '%' symbol and cast the string to float before instantiating the Percent object. Example: `$value = str_replace('%', '', $userInput); $percent = new Percent((float) $value, $baseMoney);`. Always validate input to avoid invalid float values.
Will this package break my existing code if I rely on integer percentages (e.g., 10 instead of 10.0)?
No breaking changes exist, but the package now enforces float return types via PHPStan. If your code assumes `getValue()` returns an int, cast explicitly: `$rate = (int) $percent->getValue()`. Audit legacy code for implicit type coercion issues.
Can I use this in Laravel Blade templates or should I keep calculations in services?
Avoid instantiating Percent objects directly in Blade. Pass pre-computed Money values to views instead. Use the package in Laravel services (e.g., `DiscountService`) to encapsulate logic and maintain type safety.
How does PHPStan integration help me catch errors in my Laravel app?
The package includes a PHPStan baseline to enforce float return types, reducing runtime type errors. Include its config in your project’s PHPStan rules (`includes: - vendor/assoconnect/php-percent/phpstan.neon`) to catch mismatches early in CI/CD.
What’s the best way to handle floating-point precision for currencies (e.g., rounding to 2 decimals)?
Use MoneyPHP’s built-in `round()` method or Laravel’s `number_format()` for display. Example: `$rounded = $percent->apply()->round(2);`. Avoid manual rounding to prevent inconsistencies with currency formatting.
Are there alternatives to this package for percentage calculations in Laravel?
Yes, you could use MoneyPHP’s native methods or create a custom Percentage trait with explicit type hints. However, **assoconnect/php-percent** provides a standardized, PHPDoc-documented API tailored for MoneyPHP, reducing boilerplate and improving type safety.
How do I test this package in a Laravel application?
Mock Money objects in tests and verify Percent calculations. Example: `$percent = new Percent(20, Money::USD(50)); $this->assertEquals(Money::USD(10), $percent->apply());`. Use PHPUnit’s assertions to validate both float and Money return types.
Does this package work in production environments without issues?
Yes, the package is production-ready with no runtime breaking changes. The PHPStan baseline is a developer tooling improvement and doesn’t affect performance. Monitor float precision in financial workflows, especially for currencies.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon