lukeraymonddowning /
mula
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lukeraymonddowning\Mula\Money; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Illuminate\Support\Collection; |
||
| 7 | use Lukeraymonddowning\Mula\Exceptions\UnreadableMonetaryValue; |
||
| 8 | use Lukeraymonddowning\Mula\Money\PhpMoney\FormatResolver\FormatResolver; |
||
| 9 | use Lukeraymonddowning\Mula\Money\PhpMoney\ParserResolver\ParserResolver; |
||
| 10 | use Money\Currency; |
||
| 11 | |||
| 12 | class PhpMoney implements Money |
||
| 13 | { |
||
| 14 | public \Money\Money $value; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 15 | public FormatResolver $formatResolver; |
||
| 16 | public ParserResolver $parserResolver; |
||
| 17 | |||
| 18 | public function __construct(FormatResolver $formatResolver, ParserResolver $parserResolver) |
||
| 19 | { |
||
| 20 | $this->formatResolver = $formatResolver; |
||
| 21 | $this->parserResolver = $parserResolver; |
||
| 22 | } |
||
| 23 | |||
| 24 | public static function locale() |
||
| 25 | { |
||
| 26 | return config('mula.options.phpmoney.locale'); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function create($amount, $currency = null): Money |
||
| 30 | { |
||
| 31 | $currency ??= config('mula.currency'); |
||
| 32 | |||
| 33 | try { |
||
| 34 | $this->value = new \Money\Money($amount, new Currency($currency)); |
||
| 35 | } catch (Exception $exception) { |
||
| 36 | throw new UnreadableMonetaryValue($exception->getMessage()); |
||
| 37 | } |
||
| 38 | |||
| 39 | return $this; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function parse($amount, $currency = null): Money |
||
| 43 | { |
||
| 44 | try { |
||
| 45 | $this->value = $this->parserResolver->resolve()->parse(strval($amount), $currency); |
||
| 46 | } catch (Exception $exception) { |
||
| 47 | throw new UnreadableMonetaryValue($exception->getMessage()); |
||
| 48 | } |
||
| 49 | |||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function displayWithoutCurrency(): string |
||
| 54 | { |
||
| 55 | return $this->display(false); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function display(bool $includeCurrency = true): string |
||
| 59 | { |
||
| 60 | return $this->formatResolver->resolve($includeCurrency)->format($this->value); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function currency(): string |
||
| 64 | { |
||
| 65 | return $this->value->getCurrency(); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function value(): string |
||
| 69 | { |
||
| 70 | return $this->value->getAmount(); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function add(...$money): Money |
||
| 74 | { |
||
| 75 | return $this->alterValue(fn ($instance) => $instance->value->add(...Collection::make($money)->map->value)); |
||
| 76 | } |
||
| 77 | |||
| 78 | protected function alterValue(callable $closure) |
||
| 79 | { |
||
| 80 | $instance = $this->copy(); |
||
| 81 | $instance->value = $closure($instance); |
||
| 82 | |||
| 83 | return $instance; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function copy(): Money |
||
| 87 | { |
||
| 88 | return $this->newInstance($this->value); |
||
| 89 | } |
||
| 90 | |||
| 91 | protected function newInstance(\Money\Money $phpMoney) |
||
| 92 | { |
||
| 93 | $instance = app(self::class); |
||
| 94 | $instance->value = $phpMoney; |
||
| 95 | |||
| 96 | return $instance; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function subtract(...$money): Money |
||
| 100 | { |
||
| 101 | return $this->alterValue(fn ($instance) => $instance->value->subtract(...Collection::make($money)->map->value)); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function multiplyBy($multiplier): Money |
||
| 105 | { |
||
| 106 | return $this->alterValue(fn ($instance) => $instance->value->multiply($multiplier)); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function divideBy($divisor): Money |
||
| 110 | { |
||
| 111 | return $this->alterValue(fn ($instance) => $instance->value->divide($divisor)); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function mod(Money $divisor): Money |
||
| 115 | { |
||
| 116 | return $this->alterValue(fn ($instance) => $instance->value->mod($divisor->value)); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function hasSameCurrencyAs(...$money): bool |
||
| 120 | { |
||
| 121 | return $this->check($money, fn ($value) => $value->isSameCurrency($this->value)); |
||
| 122 | } |
||
| 123 | |||
| 124 | protected function check(array $money, callable $check) |
||
| 125 | { |
||
| 126 | return empty(Collection::make($money)->first(fn ($amount) => ! $check($amount->value))); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function equals(...$money): bool |
||
| 130 | { |
||
| 131 | return $this->check($money, fn ($value) => $value->equals($this->value)); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function isGreaterThan(...$money): bool |
||
| 135 | { |
||
| 136 | return $this->check($money, fn ($value) => $value->lessThan($this->value)); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function isGreaterThanOrEqualTo(...$money): bool |
||
| 140 | { |
||
| 141 | return $this->check($money, fn ($value) => $value->lessThanOrEqual($this->value)); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function isLessThan(...$money): bool |
||
| 145 | { |
||
| 146 | return $this->check($money, fn ($value) => $value->greaterThan($this->value)); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function isLessThanOrEqualTo(...$money): bool |
||
| 150 | { |
||
| 151 | return $this->check($money, fn ($value) => $value->greaterThanOrEqual($this->value)); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function split($allocation): Collection |
||
| 155 | { |
||
| 156 | return collect($this->getAllocatedAmounts($allocation)) |
||
| 157 | ->map(fn ($phpMoney) => $this->newInstance($phpMoney)); |
||
| 158 | } |
||
| 159 | |||
| 160 | protected function getAllocatedAmounts($allocation) |
||
| 161 | { |
||
| 162 | if (is_int($allocation)) { |
||
| 163 | return $this->value->allocateTo($allocation); |
||
| 164 | } |
||
| 165 | |||
| 166 | $allocation = is_array($allocation) ? $allocation : $allocation->toArray(); |
||
| 167 | |||
| 168 | return $this->value->allocate($allocation); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function __toString() |
||
| 172 | { |
||
| 173 | return $this->display(); |
||
| 174 | } |
||
| 175 | } |
||
| 176 |