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

IntegerUtility::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\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