Skip to content

Add BcMath\Number support to NumberField#7506

Open
NovakHonza wants to merge 1 commit intoEasyCorp:5.xfrom
NovakHonza:feature/number-field-bcmath-support
Open

Add BcMath\Number support to NumberField#7506
NovakHonza wants to merge 1 commit intoEasyCorp:5.xfrom
NovakHonza:feature/number-field-bcmath-support

Conversation

@NovakHonza
Copy link
Copy Markdown
Contributor

Summary

PHP 8.4 introduced \BcMath\Number — an object type for arbitrary-precision arithmetic that avoids the floating-point rounding issues inherent to float. Doctrine ORM supports it via Types::NUMBER. This PR adds native support for \BcMath\Number properties in NumberField.

Changes

  • Add setStoredAsBcMathNumber() option to NumberField
  • Introduce EaNumberType form type (extends NumberType) with StringToBcMathNumberTransformer that handles conversion between form input and \BcMath\Number
  • Update NumberConfigurator to convert \BcMath\Number to float for IntlFormatter display
  • Throw \LogicException on PHP < 8.4, \InvalidArgumentException when combined with setStoredAsString()
  • Add PHPStan ignores for BcMath\Number (not available when analyzing with phpVersion: 80200)
  • Fully backward compatible — no changes to existing behavior on PHP 8.2/8.3

Usage

yield NumberField::new('price')->setStoredAsBcMathNumber();

With a Doctrine entity:

#[ORM\Column(type: Types::NUMBER, precision: 8, scale: 2, nullable: true)]
public ?\BcMath\Number $price = null;

Without this option, NumberField sends a float on form submit (causing InvalidTypeException) and passes \BcMath\Number directly to IntlFormatter::formatNumber() which only accepts int|float (causing TypeError).

NumberField now supports PHP 8.4's BcMath\Number type via the new
setStoredAsBcMathNumber() option. This allows entities using Doctrine's
Types::NUMBER column type with BcMath\Number properties to work
seamlessly with NumberField in both forms and detail pages.

Implementation:
- Add EaNumberType form type (extends NumberType) with a model
  transformer that converts between string and BcMath\Number
- Add StringToBcMathNumberTransformer for the form data conversion
- Update NumberConfigurator to convert BcMath\Number to float for
  IntlFormatter display, with PHP version guard and conflict detection
- Cannot be combined with setStoredAsString()
@NovakHonza
Copy link
Copy Markdown
Contributor Author

A small note on the exception type: on line 38 of NumberConfigurator.php, I used \LogicException to stay consistent with how a similar environment requirement (missing Intl extension) is handled in DateTimeConfigurator.php (line 35).

That said, from a PHP semantics perspective, \RuntimeException would arguably be a better fit here — the PHP version check is a runtime environment condition, not a programming logic error. The same code works fine on PHP 8.4+ and only fails on older versions.

If you consider \RuntimeException more appropriate, I'm happy to update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant