bytic /
money
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace ByTIC\Money\Models\Currencies; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Trait CurrencyTrait. |
||
| 8 | * |
||
| 9 | * @property string $code |
||
| 10 | * @property string $symbol |
||
| 11 | * @property string $position |
||
| 12 | */ |
||
| 13 | trait CurrencyTrait |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @return string |
||
| 17 | 1 | */ |
|
| 18 | public function getCode() |
||
| 19 | 1 | { |
|
| 20 | return $this->getAttributeFromArray('code'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param $amount |
||
| 25 | * |
||
| 26 | * @return string |
||
| 27 | 5 | */ |
|
| 28 | public function moneyHTMLFormat($amount) |
||
| 29 | 5 | { |
|
| 30 | $amount = floatval($amount); |
||
| 31 | 5 | $integerValue = floor($amount); |
|
| 32 | 5 | $decimalValue = round(($amount - $integerValue) * 100); |
|
| 33 | |||
| 34 | 5 | $intHTML = '<span class="money-int">'.number_format($integerValue).'</span>'; |
|
| 35 | 5 | ||
| 36 | $decimalValue = str_pad(strval($decimalValue), 2, '0', STR_PAD_LEFT); |
||
| 37 | 5 | $decimalHTML = '<sup class="money-decimal">.'.$decimalValue.'</sup>'; |
|
| 38 | |||
| 39 | 5 | $return = $intHTML.$decimalHTML; |
|
| 40 | 5 | ||
| 41 | $symbolHTML = '<span class="money-currency">'.$this->symbol.'</span>'; |
||
| 42 | if ($this->position == 'before') { |
||
| 43 | 5 | $return = $symbolHTML.' '.$amount; |
|
| 44 | } else { |
||
| 45 | $return .= ' '.$symbolHTML; |
||
| 46 | 5 | } |
|
| 47 | |||
| 48 | return '<span class="price" content="'.number_format($amount,2,'.','').'">'.$return.'</span>'; |
||
| 49 | } |
||
| 50 | } |
||
| 51 |