Completed
Push — master ( 8f5bc0...179bc9 )
by WEBEWEB
03:06
created

FloatHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isFloat() 0 5 2
A parseString() 0 9 3
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\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\Argument
21
 */
22
class FloatHelper {
23
24
    /**
25
     * Determines if a value is a float.
26
     *
27
     * @param mixed $value The value.
28
     * @throws FloatArgumentException Throws a Float argument exception if the value is not of expected type.
29
     */
30
    public static function isFloat($value) {
31
        if (false === is_float($value)) {
32
            throw new FloatArgumentException($value);
33
        }
34
    }
35
36
    /**
37
     * Parse a string.
38
     *
39
     * @param string $value The string value.
40
     * @return float Returns the float represented by the string value.
41
     * @throws FloatArgumentException Throws a float argument exception if the string value does not represent a float.
42
     */
43
    public static function parseString($value) {
44
        if (null === $value) {
45
            return null;
46
        }
47
        if (0 === preg_match("/^[0-9]{1,}(\.[0-9]{0,})?$/", $value)) {
48
            throw new FloatArgumentException($value);
49
        }
50
        return floatval($value);
51
    }
52
53
}
54