| 1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace App\Support;
- /** Presentation only. Accounting and eligibility keep their original decimal precision. */
- final class PromotionAmountFormatter
- {
- private const MONEY_FIELDS = ['amount', 'required_flow', 'balance', 'available_balance', 'frozen_balance',
- 'before_balance', 'after_balance', 'budget', 'budget_used', 'deposit_min', 'deposit_max', 'deposit_total',
- 'loss_amount', 'reward_amount', 'min_deposit', 'min_effective_bet', 'deposit_amount', 'team_deposit_amount',
- 'bet_amount', 'team_bet_amount', 'loss', 'coins', 'remaining'];
- private const MONEY_TASKS = ['deposit', 'team_deposit', 'bet', 'team_bet'];
- public static function amount($value)
- {
- if ((!is_string($value) && !is_int($value) && !is_float($value))
- || !preg_match('/^-?\d+(?:\.\d+)?$/D', (string) $value)) return $value;
- $fixed = bcadd((string) $value, '0', 2);
- if (bccomp($fixed, '0', 2) === 0) return '0';
- return str_ends_with($fixed, '.00') ? substr($fixed, 0, -3) : $fixed;
- }
- public static function response($value)
- {
- if ($value instanceof \JsonSerializable) $value = $value->jsonSerialize();
- elseif ($value instanceof \Traversable) $value = iterator_to_array($value);
- $object = $value instanceof \stdClass;
- if ($object) $value = (array) $value;
- if (!is_array($value)) return $value;
- $moneyTask = in_array($value['key'] ?? null, self::MONEY_TASKS, true);
- $result = [];
- foreach ($value as $key => $item) {
- $isAmount = in_array($key, self::MONEY_FIELDS, true) || ($moneyTask && in_array($key, ['value', 'target'], true));
- $result[$key] = $isAmount ? self::amount($item) : self::response($item);
- }
- return $object ? (object) $result : $result;
- }
- }
|