|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace As3\Modlr\Persister\MongoDb; |
|
4
|
|
|
|
|
5
|
|
|
use As3\Modlr\Metadata\EntityMetadata; |
|
6
|
|
|
use As3\Modlr\Persister\RecordSetInterface; |
|
7
|
|
|
use As3\Modlr\Store\Store; |
|
8
|
|
|
use Doctrine\MongoDB\Cursor; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents records from MongoDb via a Cursor. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Jacob Bare <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class CursorRecordSet implements RecordSetInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Cursor |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cursor; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Hydrator |
|
24
|
|
|
*/ |
|
25
|
|
|
private $hydrator; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var EntityMetadata |
|
29
|
|
|
*/ |
|
30
|
|
|
private $metadata; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Store |
|
34
|
|
|
*/ |
|
35
|
|
|
private $store; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param EntityMetadata $metadata |
|
41
|
|
|
* @param Cursor $cursor |
|
42
|
|
|
* @param Store $store |
|
43
|
|
|
* @param Hydrator $hydrator |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(EntityMetadata $metadata, Cursor $cursor, Store $store, Hydrator $hydrator) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->metadata = $metadata; |
|
48
|
|
|
$this->cursor = $cursor; |
|
49
|
|
|
$this->hydrator = $hydrator; |
|
50
|
|
|
$this->store = $store; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function count() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->cursor->count(true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function current() |
|
65
|
|
|
{ |
|
66
|
|
|
$current = $this->cursor->current(); |
|
67
|
|
|
return $this->hydrator->normalize($this->metadata, $current, $this->store); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function key() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->cursor->key(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getSingleResult() |
|
82
|
|
|
{ |
|
83
|
|
|
$record = $this->cursor->getSingleResult(); |
|
84
|
|
|
if (!is_array($record)) { |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
return $this->hydrator->normalize($this->metadata, $record, $this->store); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function next() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->cursor->next(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function rewind() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->cursor->rewind(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function totalCount() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->cursor->count(false); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function valid() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->cursor->valid(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.