Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Number | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| parseDecimal | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Util; |
| 6 | |
| 7 | use Exception; |
| 8 | |
| 9 | class Number |
| 10 | { |
| 11 | /** |
| 12 | * Parses a number string to a computer processible string |
| 13 | * which works with floatval. |
| 14 | * |
| 15 | * This works for any kind of input, American or European style. |
| 16 | */ |
| 17 | public static function parseDecimal(string $value): string |
| 18 | { |
| 19 | $value = preg_replace('/\s/', '', $value); |
| 20 | |
| 21 | if (preg_match('/^[0-9.,]+$/', $value)) { |
| 22 | $value = str_replace(',', '.', $value); |
| 23 | |
| 24 | // remove all dots but the last one |
| 25 | return preg_replace('/\.(?=.*\.)/', '', $value); |
| 26 | } |
| 27 | |
| 28 | throw new Exception(_('This is not a valid number')); |
| 29 | } |
| 30 | } |