1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Collections\DataSource; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
14
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Data Source Class. |
18
|
|
|
* |
19
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class DataSource implements DataSourceInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var SpecificationInterface |
25
|
|
|
*/ |
26
|
|
|
protected $searchCriteria; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ComparatorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $sortCriteria; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $offset; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $length; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
private $count; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param SpecificationInterface $searchCriteria |
50
|
|
|
* @param ComparatorInterface $sortCriteria |
51
|
|
|
* @param int $offset |
52
|
|
|
* @param int $length |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
SpecificationInterface $searchCriteria = null, |
56
|
|
|
ComparatorInterface $sortCriteria = null, |
57
|
|
|
$offset = null, |
58
|
|
|
$length = null |
59
|
|
|
) { |
60
|
|
|
$this->searchCriteria = $searchCriteria; |
61
|
|
|
$this->sortCriteria = $sortCriteria; |
62
|
|
|
$this->offset = $offset; |
63
|
|
|
$this->length = $length; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function count() |
70
|
|
|
{ |
71
|
|
|
if ($this->count === null) { |
72
|
|
|
$this->count = $this->calculateCount(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->count; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function length() |
82
|
|
|
{ |
83
|
|
|
return $this->length; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function offset() |
90
|
|
|
{ |
91
|
|
|
return $this->offset; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function searchCriteria() |
98
|
|
|
{ |
99
|
|
|
return $this->searchCriteria; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function sortCriteria() |
106
|
|
|
{ |
107
|
|
|
return $this->sortCriteria; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function isSorted() |
114
|
|
|
{ |
115
|
|
|
return $this->sortCriteria() !== null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isFiltered() |
122
|
|
|
{ |
123
|
|
|
return $this->searchCriteria() !== null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function isSliced() |
130
|
|
|
{ |
131
|
|
|
return $this->offset() !== null || $this->length() !== null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $offset |
136
|
|
|
*/ |
137
|
|
|
protected function actualOffset($offset) |
138
|
|
|
{ |
139
|
|
|
$actualOffset = (int) $offset; |
140
|
|
|
if ($this->offset() !== null) { |
141
|
|
|
$actualOffset += $this->offset(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $actualOffset; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int $offset |
149
|
|
|
* @param int $length |
150
|
|
|
*/ |
151
|
|
|
protected function actualLength($offset, $length = null) |
152
|
|
|
{ |
153
|
|
|
$actualLength = $length; |
154
|
|
|
if ($this->isSliced()) { |
155
|
|
|
if ($this->length() !== null && $offset >= $this->length()) { |
156
|
|
|
return 0; |
157
|
|
|
} |
158
|
|
|
if ($this->length() !== null) { |
159
|
|
|
$actualLength = $this->length() - (int) $offset; |
160
|
|
|
if ($length !== null) { |
161
|
|
|
$actualLength = \min([$actualLength, $length]); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $actualLength; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
abstract protected function calculateCount(); |
173
|
|
|
} |
174
|
|
|
|