Passed
Pull Request — master (#480)
by Def
02:06
created

NumericHelper::normalize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Helper;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
9
final class NumericHelper
10
{
11
    /**
12
     * Returns string representation of a number value without thousands separators and with dot as decimal separator.
13
     *
14
     * @param bool|float|int|string $value
15
     *
16
     * @throws InvalidArgumentException if value is not scalar.
17
     *
18
     * @return string
19
     */
20
    public static function normalize($value): string
21
    {
22
        /** @psalm-suppress DocblockTypeContradiction */
23
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
24
            $type = gettype($value);
25
            throw new InvalidArgumentException("Value must be scalar. $type given.");
26
        }
27
28
        if (is_bool($value)) {
29
            $value = $value ? '1' : '0';
30
        } else {
31
            $value = (string)$value;
32
        }
33
        $value = str_replace([' ', ','], ['', '.'], $value);
34
        return preg_replace('/\.(?=.*\.)/', '', $value);
35
    }
36
}
37