Passed
Pull Request — master (#78)
by
unknown
11:31
created

NumericHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 29
dl 0
loc 88
ccs 31
cts 31
cp 1
rs 10
c 4
b 1
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 15 4
A isInteger() 0 3 1
B toOrdinal() 0 23 7
A isPositiveInteger() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Strings;
6
7
use InvalidArgumentException;
8
use function in_array;
9
use function is_bool;
10
11
/**
12
 * Provides static methods to work with numeric strings.
13
 */
14
final class NumericHelper
15
{
16
    /**
17
     * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd etc.
18
     *
19
     * @param float|int|string $value The number to get its ordinal value.
20
     *
21
     * @return string
22
     */
23 2
    public static function toOrdinal($value): string
24
    {
25 2
        if (!is_numeric($value)) {
26 1
            $type = gettype($value);
27 1
            throw new InvalidArgumentException("Value must be numeric. $type given.");
28
        }
29
30 1
        if (fmod((float)$value, 1) !== 0.00) {
31 1
            return (string)$value;
32
        }
33
34 1
        if (in_array($value % 100, [11, 12, 13], true)) {
35 1
            return $value . 'th';
36
        }
37 1
        switch ($value % 10) {
38 1
            case 1:
39 1
                return $value . 'st';
40 1
            case 2:
41 1
                return $value . 'nd';
42 1
            case 3:
43 1
                return $value . 'rd';
44
            default:
45 1
                return $value . 'th';
46
        }
47
    }
48
49
    /**
50
     * Returns string representation of a number value without thousands separators and with dot as decimal separator.
51
     *
52
     * @param bool|float|int|string $value
53
     *
54
     * @throws InvalidArgumentException if value is not scalar.
55
     *
56
     * @return string
57
     */
58 10
    public static function normalize($value): string
59
    {
60
        /** @psalm-suppress DocblockTypeContradiction */
61 10
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
62 1
            $type = gettype($value);
63 1
            throw new InvalidArgumentException("Value must be scalar. $type given.");
64
        }
65
66 9
        if (is_bool($value)) {
67 2
            $value = $value ? '1' : '0';
68
        } else {
69 7
            $value = (string)$value;
70
        }
71 9
        $value = str_replace([' ', ','], ['', '.'], $value);
72 9
        return preg_replace('/\.(?=.*\.)/', '', $value);
73
    }
74
75
    /**
76
     * Checks whether the given value is an integer number.
77
     *
78
     * @param mixed $value
79
     *
80
     * @return bool
81
     */
82 9
    public static function isInteger($value): bool
83
    {
84 9
        return filter_var($value, FILTER_VALIDATE_INT) !== false;
85
    }
86
87
    /**
88
     * Checks whether the given string is a positive integer number.
89
     *
90
     * @param string $string
91
     *
92
     * @return bool
93
     */
94 7
    public static function isPositiveInteger(string $string): bool
95
    {
96
        // Exclude strings like "+5"
97 7
        if (!preg_match('/^\d+$/', $string)) {
98 5
            return false;
99
        }
100
101 2
        return filter_var($string, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) !== false;
102
    }
103
}
104