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

IntegerUtility   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseBoolean() 0 3 2
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\IntegerArgumentException;
15
16
/**
17
 * Integer utility.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Utility\Argument
21
 * @final
22
 */
23
final class IntegerUtility {
24
25
    /**
26
     * Parse a boolean.
27
     *
28
     * @param boolean $value The boolean value.
29
     * @return integer Returns 1 in case of success, 0 otherwise.
30
     */
31
    public static function parseBoolean($value) {
32
        return $value === true ? 1 : 0;
33
    }
34
35
    /**
36
     * Parse a string.
37
     *
38
     * @param string $value The string value.
39
     * @return integer Returns the integer represented by the string value..
40
     * @throws IntegerArgumentException Throws an integer argument exception if the string value does not represent an integer.
41
     */
42
    public static function parseString($value) {
43
        if (null === $value) {
44
            return null;
45
        }
46
        if (0 === preg_match("/^[0-9]{1,}$/", $value)) {
47
            throw new IntegerArgumentException($value);
48
        }
49
        return intval($value);
50
    }
51
52
}
53