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

IntegerHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 67
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLinesLimit() 0 3 1
A getPagesCount() 0 3 1
A isInteger() 0 5 2
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) 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\IntegerArgumentException;
15
use WBW\Library\Core\Database\PaginateHelper;
16
17
/**
18
 * Integer helper.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Argument
22
 */
23
class IntegerHelper {
24
25
    /**
26
     * Get a lines limit.
27
     *
28
     * @param integer $pageNumber The page number.
29
     * @param integer $divider The divider.
30
     * @param integer $total The total.
31
     * @return integer[] Returns the offset and limit in case of success, -1 otherwise.
32
     * @deprecated
33
     */
34
    public static function getLinesLimit($pageNumber, $divider, $total = -1) {
35
        return PaginateHelper::getPageOffsetAndLimit($pageNumber, $divider, $total);
36
    }
37
38
    /**
39
     * Get a pages count.
40
     *
41
     * @param integer $linesNumber The lines number.
42
     * @param integer $divider The divider.
43
     * @return integer Returns the pages count in case of success, -1 otherwise.
44
     * @deprecated
45
     */
46
    public static function getPagesCount($linesNumber, $divider) {
47
        return PaginateHelper::getPagesCount($linesNumber, $divider);
48
    }
49
50
    /**
51
     * Determines if a value is an integer.
52
     *
53
     * @param mixed $value The value.
54
     * @throws IntegerArgumentException Throws a Integer argument exception if the value is not of expected type.
55
     */
56
    public static function isInteger($value) {
57
        if (false === is_integer($value)) {
58
            throw new IntegerArgumentException($value);
59
        }
60
    }
61
62
    /**
63
     * Parse a boolean.
64
     *
65
     * @param boolean $value The boolean value.
66
     * @return integer Returns 1 in case of success, 0 otherwise.
67
     */
68
    public static function parseBoolean($value) {
69
        return $value === true ? 1 : 0;
70
    }
71
72
    /**
73
     * Parse a string.
74
     *
75
     * @param string $value The string value.
76
     * @return integer Returns the integer represented by the string value..
77
     * @throws IntegerArgumentException Throws an integer argument exception if the string value does not represent an integer.
78
     */
79
    public static function parseString($value) {
80
        if (null === $value) {
81
            return null;
82
        }
83
        if (0 === preg_match("/^[0-9]{1,}$/", $value)) {
84
            throw new IntegerArgumentException($value);
85
        }
86
        return intval($value);
87
    }
88
89
}
90