|
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
|
|
|
* 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
|
|
|
*/ |
|
33
|
|
|
public static function getLinesLimit($pageNumber, $divider, $total = -1) { |
|
34
|
|
|
if ($pageNumber < 0 || $divider < 0) { |
|
35
|
|
|
return -1; |
|
36
|
|
|
} |
|
37
|
|
|
$offset = $pageNumber * $divider; |
|
38
|
|
|
$limit = $divider; |
|
39
|
|
|
if (0 <= $total && ($total < $offset || $total < ($offset + $limit))) { |
|
40
|
|
|
$offset = (self::getPagesCount($total, $divider) - 1) * $divider; |
|
41
|
|
|
$limit = $total - $offset; |
|
42
|
|
|
} |
|
43
|
|
|
return [$offset, $limit]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get a pages count. |
|
48
|
|
|
* |
|
49
|
|
|
* @param integer $linesNumber The lines number. |
|
50
|
|
|
* @param integer $divider The divider. |
|
51
|
|
|
* @return integer Returns the pages count in case of success, -1 otherwise. |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getPagesCount($linesNumber, $divider) { |
|
54
|
|
|
if ($linesNumber < 0 || $divider < 0) { |
|
55
|
|
|
return -1; |
|
56
|
|
|
} |
|
57
|
|
|
$pagesCount = intval($linesNumber / $divider); |
|
58
|
|
|
if (0 < ($linesNumber % $divider)) { |
|
59
|
|
|
++$pagesCount; |
|
60
|
|
|
} |
|
61
|
|
|
return $pagesCount; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Parse a boolean. |
|
66
|
|
|
* |
|
67
|
|
|
* @param boolean $value The boolean value. |
|
68
|
|
|
* @return integer Returns 1 in case of success, 0 otherwise. |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function parseBoolean($value) { |
|
71
|
|
|
return $value === true ? 1 : 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Parse a string. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $value The string value. |
|
78
|
|
|
* @return integer Returns the integer represented by the string value.. |
|
79
|
|
|
* @throws IntegerArgumentException Throws an integer argument exception if the string value does not represent an integer. |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function parseString($value) { |
|
82
|
|
|
if (null === $value) { |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
if (0 === preg_match("/^[0-9]{1,}$/", $value)) { |
|
86
|
|
|
throw new IntegerArgumentException($value); |
|
87
|
|
|
} |
|
88
|
|
|
return intval($value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|