Completed
Push — master ( 44b4d6...c54f73 )
by WEBEWEB
06:15
created

FloatHelper::parseString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 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\Helper\Argument;
13
14
use WBW\Library\Core\Exception\Argument\FloatArgumentException;
15
16
/**
17
 * Float helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Helper\Argument
21
 */
22
class FloatHelper {
23
24
    /**
25
     * Parse a string.
26
     *
27
     * @param string $value The string value.
28
     * @return float Returns the float represented by the string value.
29
     * @throws FloatArgumentException Throws a float argument exception if the string value does not represent a float.
30
     */
31
    public static function parseString($value) {
32
        if (null === $value) {
33
            return null;
34
        }
35
        if (0 === preg_match("/^[0-9]{1,}(\.[0-9]{0,})?$/", $value)) {
36
            throw new FloatArgumentException($value);
37
        }
38
        return floatval($value);
39
    }
40
41
}
42