| Total Complexity | 9 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | final class NumericHelper |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd etc. |
||
| 12 | * @param int|float|string $value The number to get its ordinal value. |
||
| 13 | * @return string |
||
| 14 | */ |
||
| 15 | 2 | public static function toOrdinal($value): ?string |
|
| 16 | { |
||
| 17 | 2 | if (!is_numeric($value)) { |
|
| 18 | 1 | throw new \InvalidArgumentException('Value must be numeric.'); |
|
| 19 | } |
||
| 20 | |||
| 21 | 1 | if (fmod($value, 1) !== 0.00) { |
|
|
|
|||
| 22 | 1 | return $value; |
|
| 23 | } |
||
| 24 | |||
| 25 | 1 | if (\in_array($value % 100, range(11, 13), false)) { |
|
| 26 | 1 | return $value . 'th'; |
|
| 27 | } |
||
| 28 | 1 | switch ($value % 10) { |
|
| 29 | 1 | case 1: |
|
| 30 | 1 | return $value . 'st'; |
|
| 31 | 1 | case 2: |
|
| 32 | 1 | return $value . 'nd'; |
|
| 33 | 1 | case 3: |
|
| 34 | 1 | return $value . 'rd'; |
|
| 35 | default: |
||
| 36 | 1 | return $value . 'th'; |
|
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns string representation of a number value without thousands separators and with dot as decimal separator. |
||
| 42 | * @param int|float|string $value |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 6 | public static function normalize($value): string |
|
| 54 | } |
||
| 55 | } |
||
| 56 |