|
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\Helper\Argument; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Library\Core\Exception\Argument\IntegerArgumentException; |
|
15
|
|
|
use WBW\Library\Core\Helper\Database\PaginateHelper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Integer helper. |
|
19
|
|
|
* |
|
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
21
|
|
|
* @package WBW\Library\Core\Helper\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
|
|
|
* Parse a boolean. |
|
52
|
|
|
* |
|
53
|
|
|
* @param boolean $value The boolean value. |
|
54
|
|
|
* @return integer Returns 1 in case of success, 0 otherwise. |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function parseBoolean($value) { |
|
57
|
|
|
return $value === true ? 1 : 0; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Parse a string. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $value The string value. |
|
64
|
|
|
* @return integer Returns the integer represented by the string value.. |
|
65
|
|
|
* @throws IntegerArgumentException Throws an integer argument exception if the string value does not represent an integer. |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function parseString($value) { |
|
68
|
|
|
if (null === $value) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
if (0 === preg_match("/^[0-9]{1,}$/", $value)) { |
|
72
|
|
|
throw new IntegerArgumentException($value); |
|
73
|
|
|
} |
|
74
|
|
|
return intval($value); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|