1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Minotaur |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Minotaur\Db; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A wrapper around a `Traversable` that includes the total count of the superset. |
25
|
|
|
*/ |
26
|
|
|
class CursorSubset extends \IteratorIterator implements \JsonSerializable |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $total; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new CursorSubset. |
35
|
|
|
* |
36
|
|
|
* @param \Traversable $iterable The traversable to wrap |
37
|
|
|
* @param int $total The total number of items in the superset |
38
|
|
|
* @throws \RangeException if the total is negative |
39
|
|
|
*/ |
40
|
5 |
|
public function __construct(\Traversable $iterable, int $total) |
41
|
|
|
{ |
42
|
5 |
|
parent::__construct($iterable); |
43
|
5 |
|
if ($total < 0) { |
44
|
1 |
|
throw new \RangeException("Total cannot be a negative number"); |
45
|
|
|
} |
46
|
4 |
|
$this->total = $total; |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the superset total. |
51
|
|
|
* |
52
|
|
|
* @return - The total number of items in the superset (never negative). |
|
|
|
|
53
|
|
|
*/ |
54
|
4 |
|
public function getTotal(): int |
55
|
|
|
{ |
56
|
4 |
|
return $this->total; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return data which can be serialized with json_encode. |
61
|
|
|
*/ |
62
|
1 |
|
public function jsonSerialize() |
63
|
|
|
{ |
64
|
1 |
|
$it = $this->getInnerIterator(); |
65
|
1 |
|
return $it instanceof \JsonSerializable ? $it : $this->toArray(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Converts this thing into an array. |
70
|
|
|
* |
71
|
|
|
* @return array<mixed> The array version of this thing |
72
|
|
|
*/ |
73
|
4 |
|
public function toArray(): array |
74
|
|
|
{ |
75
|
4 |
|
$it = $this->getInnerIterator(); |
76
|
4 |
|
if ($it instanceof \MongoDB\Driver\Cursor) { |
|
|
|
|
77
|
|
|
return $it->toArray(); |
78
|
4 |
|
} elseif ($it instanceof Iterable) { |
|
|
|
|
79
|
|
|
return $it->toArray(); |
80
|
4 |
|
} elseif ($it instanceof \ArrayObject) { |
81
|
|
|
return $it->getArrayCopy(); |
82
|
4 |
|
} elseif ($it instanceof \ArrayIterator) { |
83
|
3 |
|
return $it->getArrayCopy(); |
84
|
|
|
} |
85
|
1 |
|
return iterator_to_array($it, false); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.