Passed
Pull Request — master (#39)
by Alexander
01:23
created

NumericHelper::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Yiisoft\Strings;
4
5
/**
6
 * Provides static methods to work with numeric strings.
7
 */
8
final class NumericHelper
9
{
10
    /**
11
     * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
12
     * @param int $number The number to get its ordinal value.
13
     * @return string
14
     */
15 1
    public static function toOrdinal(int $number): ?string
16
    {
17 1
        if (\in_array($number % 100, range(11, 13), true)) {
18 1
            return $number . 'th';
19
        }
20 1
        switch ($number % 10) {
21 1
            case 1:
22 1
                return $number . 'st';
23 1
            case 2:
24 1
                return $number . 'nd';
25 1
            case 3:
26 1
                return $number . 'rd';
27
            default:
28 1
                return $number . 'th';
29
        }
30
    }
31
32
    /**
33
     * Returns string representation of a number value without thousands separators and with dot as decimal separator.
34
     * @param int|float|string $value
35
     * @return string
36
     */
37 5
    public static function normalize($value): string
38
    {
39 5
        $value = (string)$value;
40 5
        $value = str_replace([" ", ","], ["", "."], $value);
41 5
        return preg_replace('/\.(?=.*\.)/', '', $value);
42
    }
43
}
44