number-to-words icon indicating copy to clipboard operation
number-to-words copied to clipboard

Make NumberTransformerBuilder injectable

Open dsentker opened this issue 5 years ago • 0 comments

First, thank you for your work.

If you use one of the supplied transformers (for example, GermanCurrencyTransformer), there is no way to modify the NumberTransformerBuilder. In my case, I need to customize the "withWordsSeparatedBy()" method, but the rest should stay the same.

Perhaps the CurrencyTransformer interface could get a "getTransformerBuilder()" method so that I can override this method to provde a custom provider in a child class.

Here is a simple example:

class CustomizedGermanCurrencyTransformer extends GermanCurrencyTransformer {

    /**
     * Overrides the default transformer builder to modify builder options
     * 
     * @return \NumberToWords\NumberTransformer\NumberTransformer
     */
    public function getTransformerBuilder()
    {
        return $numberTransformer = (new NumberTransformerBuilder())
            ->withDictionary($dictionary)
            ->withWordsSeparatedBy('')
            ->transformNumbersBySplittingIntoPowerAwareTriplets($numberToTripletsConverter, $tripletTransformer)
            ->inflectExponentByNumbers($exponentInflector)
            ->build();
    }
    
}

dsentker avatar Jan 18 '19 10:01 dsentker