he new instance. */ public static function create_from_float( float $value ): self { return self::create( new Precision_Value( $value ) ); } /** * Set the default values for the class. * * Use this to allow for setting default values for all instances of this class * that are created with the create() method. * * @since 5.18.0 * * @param ?string $currency_symbol The currency symbol. * @param ?string $thousands_separator The thousands separator. * @param ?string $decimal_separator The decimal separator. * @param ?string $currency_symbol_position The currency symbol position. * * @return void */ public static function set_defaults( ?string $currency_symbol = null, ?string $thousands_separator = null, ?string $decimal_separator = null, ?string $currency_symbol_position = null ) { $position = self::map_position( $currency_symbol_position ?? self::$defaults['currency_symbol_position'] ); self::$defaults = [ 'currency_symbol' => $currency_symbol ?? self::$defaults['currency_symbol'], 'currency_symbol_position' => $position, ]; self::set_separator_defaults( $decimal_separator, $thousands_separator ); } /** * Add a value to the current value. * * @since 5.18.0 * * @param Currency_Value $value The value to add. * * @return Currency_Value The new value object. */ public function add( Currency_Value $value ): Currency_Value { $result = $this->value->add( $value->get_raw_value() ); return new self( $result, $this->currency_symbol, $this->thousands_separator, $this->decimal_separator, $this->currency_symbol_position ); } /** * Subtract a value from the current value. * * @since 5.18.0 * * @param Currency_Value $value The value to subtract. * * @return Currency_Value The new value object. */ public function subtract( Currency_Value $value ): Currency_Value { $result = $this->value->subtract( $value->get_raw_value() ); return new self( $result, $this->currency_symbol, $this->thousands_separator, $this->decimal_separator, $this->currency_symbol_position ); } /** * Add multiple values together. * * @since 5.18.0 * * @param Currency_Value ...$values The values to add. * * @return Currency_Value The new value object. */ public static function sum( Currency_Value ...$values ): Currency_Value { $sum = new Precision_Value( 0 ); foreach ( $values as $value ) { $sum = $sum->add( $value->get_raw_value() ); } return static::create( $sum ); } /** * Multiply the current value by an integer. * * @since 5.18.0 * * @param Integer_Value $value The value to multiply by. * * @return Currency_Value The new value object. */ public function multiply_by_integer( Integer_Value $value ): Currency_Value { $new_value = $this->value->multiply_by_integer( $value ); return new self( $new_value, $this->currency_symbol, $this->thousands_separator, $this->decimal_separator, $this->currency_symbol_position ); } /** * Map a position to a valid value. * * @since 5.21.0 * * @param string $position The position to map. * * @return string The mapped position, either 'before' or 'after'. */ protected static function map_position( string $position ): string { switch ( $position ) { case 'before': case 'after': return $position; case 'postfix': return 'after'; case 'prefix': default: return 'before'; } } }