We can use the code in https://github.com/commerceguys/pricing/blob/master/src/Price.php for an example.

We want a \Drupal\commerce_price\Calculator static class that implements no interface and has the following methods:
- compare
- add
- subtract
- multiply
- divide
- ceil
- floor
- assertAmountFormat

The result should be trimmed of any extra zeroes (since bcmath will add them up to $precision).

Once we have that, we want to introduce the same methods that https://github.com/commerceguys/pricing/blob/master/src/Price.php has to the value object, and use the Calculator inside.

Comments

bojanz created an issue. See original summary.

bojanz’s picture

While we're at it we should also do this:

--- a/modules/price/src/Price.php
+++ b/modules/price/src/Price.php
@@ -3,9 +3,9 @@
 namespace Drupal\commerce_price;
 
 /**
- * Provides a value object for monetary values.
+ * Represents a monetary value.
  */
-class Price {
+final class Price {
 
   /**
    * The decimal amount.

bojanz’s picture

Issue tags: +drupaldevdays

Tagging for dev days. Of course, as long as it's unassigned it can be taken by non-devdays people too :)

bojanz’s picture

Issue tags: -drupaldevdays +DevDaysMilan
fbailey’s picture

Assigned: Unassigned » fbailey
fbailey’s picture

  • bojanz committed 071d9ba on 8.x-2.x authored by fbailey
    Issue #2749287 by bojanz, fbailey: Implement a Calculator class and use...
bojanz’s picture

Status: Needs review » Fixed

Committed after a series of bikeshed discussions :) Thanks!

  • mglaman authored 30b3091 on 8.x-2.x
    Issue #2789651 by mglaman: Follow up to Calculator class, #2749287
    
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.