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

FloatUtility   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 20
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A parseString() 0 9 3
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