jordanbrauer / unit-converter

Convert standard units from one to another with this easy to use, lightweight package
https://jordanbrauer.github.io/unit-converter/
MIT License
129 stars 39 forks source link

Relax `ext-bcmath` requirement in composer.json and instead add checks when attempting to use BinaryCalculator #187

Open pointybeard opened 1 year ago

pointybeard commented 1 year ago

Currently, there is a requirement for ext-bcmath to be installed:

https://github.com/jordanbrauer/unit-converter/blob/a8732503f8e0c6038c805e1449dc498162bfc9a2/composer.json#L12-L16

However, it is possible to use SimpleCalculator instead of BinaryCalculator:

https://github.com/jordanbrauer/unit-converter/blob/a8732503f8e0c6038c805e1449dc498162bfc9a2/src/Unit/AbstractUnit.php#L128-L135

To give flexibility, and support systems that do not or cannot install php-bcmath, I suggest adding some assertions in AbstractUnit::as() or in BinaryCalculator to check that the bcmath extension is installed if someone is attempting to use the BinaryCalculator.

It appears this is approach is already taken for ext-intl here.

pointybeard commented 1 year ago

Looks like BinaryCalculator already has an assertion:

https://github.com/jordanbrauer/unit-converter/blob/a8732503f8e0c6038c805e1449dc498162bfc9a2/src/Calculator/BinaryCalculator.php#L40-L53

So, it seems it just a matter of changing composer.json. ext-intl and ext-bcmath could be put into the suggest block of composer.json. E.g.

"suggest": {
    "ext-bcmath": "Needed to enable usage of BinaryCalculator",
    "ext-intl": "Needed to support spellout() feature of UnitConverter"
}