| 1: | <?php |
| 2: | namespace TIC\CoreBundle\Form\DataTransformer; |
| 3: | |
| 4: | use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer; |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | class NumberToStringTransformer extends NumberToLocalizedStringTransformer |
| 11: | { |
| 12: | protected $groupingSep; |
| 13: | protected $decimalSep; |
| 14: | protected $multiplier; |
| 15: | protected $locale; |
| 16: | protected $fixed; |
| 17: | protected $precision; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, mixed $multiplier = null, string $locale = null, bool $fixed = false) |
| 30: | { |
| 31: | parent::__construct($scale, $grouping, $roundingMode); |
| 32: | $this->precision = $scale; |
| 33: | |
| 34: | |
| 35: | if (! \is_bool($this->grouping)) { |
| 36: | $this->groupingSep = $this->grouping; |
| 37: | $this->grouping = true; |
| 38: | } |
| 39: | |
| 40: | |
| 41: | if (! empty($multiplier) && \is_numeric($multiplier)) $this->multiplier = $multiplier; |
| 42: | |
| 43: | |
| 44: | if ($locale === null || $locale === true) $this->locale = \Locale::getDefault(); |
| 45: | elseif ($locale === false) $this->locale = 'en'; |
| 46: | else $this->locale = $locale; |
| 47: | |
| 48: | $this->fixed = $fixed; |
| 49: | } |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | public function transform(mixed $value): string |
| 59: | { |
| 60: | |
| 61: | |
| 62: | if (($value !== null) && \is_numeric($value) && ($this->multiplier !== null)) $value *= $this->multiplier; |
| 63: | |
| 64: | return parent::transform($value); |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | public function reverseTransform(mixed $value): int|float|null |
| 75: | { |
| 76: | $result = parent::reverseTransform($value); |
| 77: | |
| 78: | |
| 79: | if (($result !== null) && ($this->multiplier !== null)) $result /= $this->multiplier; |
| 80: | |
| 81: | if (\is_float($result) && ($this->precision < 1)) return (int)$result; |
| 82: | return $result; |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | protected function getNumberFormatter(): \NumberFormatter |
| 91: | { |
| 92: | $formatter = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL); |
| 93: | |
| 94: | if (null !== $this->precision) { |
| 95: | $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); |
| 96: | if ($this->fixed) |
| 97: | $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); |
| 98: | else |
| 99: | $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->precision); |
| 100: | } |
| 101: | $this->decimalSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); |
| 102: | |
| 103: | $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping); |
| 104: | if ($this->grouping) { |
| 105: | |
| 106: | if ($this->groupingSep !== null) $formatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $this->groupingSep); |
| 107: | |
| 108: | else $this->groupingSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL); |
| 109: | } |
| 110: | |
| 111: | return $formatter; |
| 112: | } |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | public function getGroupingSep(): ?string |
| 120: | { |
| 121: | if (! $this->grouping) return null; |
| 122: | |
| 123: | |
| 124: | if ($this->groupingSep === null) $this->getNumberFormatter(); |
| 125: | |
| 126: | return $this->groupingSep; |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public function getDecimalSep(): ?string |
| 135: | { |
| 136: | |
| 137: | if ($this->decimalSep === null) $this->getNumberFormatter(); |
| 138: | |
| 139: | return $this->decimalSep; |
| 140: | } |
| 141: | |
| 142: | } |
| 143: | |