1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the SomeWork/OffsetPage/Logic package. |
5
|
|
|
* |
6
|
|
|
* (c) Pinchuk Igor <[email protected]> |
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 SomeWork\OffsetPage\Logic; |
13
|
|
|
|
14
|
|
|
class Offset |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param int $offset |
18
|
|
|
* @param int $limit |
19
|
|
|
* @param int $nowCount |
20
|
|
|
* |
21
|
|
|
* @throws \LogicException |
22
|
|
|
* @throws \SomeWork\OffsetPage\Logic\AlreadyGetNeededCountException |
23
|
|
|
* |
24
|
|
|
* @return OffsetLogicResult |
25
|
|
|
*/ |
26
|
|
|
public static function logic($offset, $limit, $nowCount = 0) |
27
|
|
|
{ |
28
|
|
|
$offset = (int) $offset; |
29
|
|
|
$limit = (int) $limit; |
30
|
|
|
$nowCount = (int) $nowCount; |
31
|
|
|
|
32
|
|
|
$offset = $offset >= 0 ? $offset : 0; |
33
|
|
|
$limit = $limit >= 0 ? $limit : 0; |
34
|
|
|
$nowCount = $nowCount >= 0 ? $nowCount : 0; |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* Means that you should get all |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
if ($offset === 0 && $limit === 0 && $nowCount === 0) { |
|
|
|
|
40
|
|
|
return new OffsetLogicResult(0, 0); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
if ($offset === 0 && $limit > 0 && $nowCount === 0) { |
|
|
|
|
44
|
|
|
return new OffsetLogicResult(1, $limit); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($offset > 0 && $limit === 0) { |
48
|
|
|
return new OffsetLogicResult(2, $nowCount + $offset); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($nowCount > 0) { |
52
|
|
|
if ($limit > $nowCount) { |
53
|
|
|
return static::logic($offset + $nowCount, $limit - $nowCount); |
54
|
|
|
} |
55
|
|
|
/* |
56
|
|
|
* Means that you should stop fetching |
57
|
|
|
*/ |
58
|
|
|
throw new AlreadyGetNeededCountException('Limit <= no count. You should stop asking (:'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($offset > 0 && $limit > 0) { |
62
|
|
|
if ($offset === $limit) { |
63
|
|
|
return new OffsetLogicResult(2, $limit); |
64
|
|
|
} |
65
|
|
|
if ($offset < $limit) { |
66
|
|
|
return new OffsetLogicResult(2, $offset); |
67
|
|
|
} |
68
|
|
|
if ($offset > $limit) { |
69
|
|
|
for ($i = $limit; $i > 0; $i--) { |
70
|
|
|
if ($offset % $i === 0) { |
71
|
|
|
return new OffsetLogicResult( |
72
|
|
|
($offset / $i) + 1, |
73
|
|
|
$i |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* |
81
|
|
|
* Really its can be here oO |
82
|
|
|
*/ |
83
|
|
|
throw new \LogicException('Something gone wrong'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.