Completed
Push — master ( d99fd7...2fd08f )
by WEBEWEB
03:24
created

IntegerUtility::parseBoolean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
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;
13
14
use WBW\Library\Core\Exception\Argument\IntegerArgumentException;
15
16
/**
17
 * Integer utility.
18
 *
19
 * @author NdC/WBW <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Utility
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