Completed
Push — master ( 7942ad...1c8947 )
by WEBEWEB
01:50
created

FloatUtility::parseString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Utility\Argument;
13
14
use WBW\Library\Core\Exception\Argument\FloatArgumentException;
15
16
/**
17
 * Float utility.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Utility\Argument
21
 * @final
22
 */
23
final class FloatUtility {
24
25
    /**
26
     * Parse a string.
27
     *
28
     * @param string $value The string value.
29
     * @return float Returns the float represented by the string value.
30
     * @throws FloatArgumentException Throws a float argument exception if the string value does not represent a float.
31
     */
32
    public static function parseString($value) {
33
        if (null === $value) {
34
            return null;
35
        }
36
        if (0 === preg_match("/^[0-9]{1,}(\.[0-9]{0,})?$/", $value)) {
37
            throw new FloatArgumentException($value);
38
        }
39
        return floatval($value);
40
    }
41
42
}
43