1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* doctrine-mongodb-odm-repositories (https://github.com/juliangut/doctrine-mongodb-odm-repositories). |
5
|
|
|
* Doctrine2 MongoDB ODM utility entity repositories. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/juliangut/doctrine-mongodb-odm-repositories |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Jgut\Doctrine\Repository\MongoDB\ODM; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Util\ClassUtils; |
18
|
|
|
use Doctrine\ODM\MongoDB\Cursor; |
19
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
20
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
21
|
|
|
use Jgut\Doctrine\Repository\EventsTrait; |
22
|
|
|
use Jgut\Doctrine\Repository\FiltersTrait; |
23
|
|
|
use Jgut\Doctrine\Repository\PaginatorTrait; |
24
|
|
|
use Jgut\Doctrine\Repository\Repository; |
25
|
|
|
use Jgut\Doctrine\Repository\RepositoryTrait; |
26
|
|
|
use Zend\Paginator\Paginator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* MongoDB document repository. |
30
|
|
|
*/ |
31
|
|
|
class MongoDBRepository extends DocumentRepository implements Repository |
32
|
|
|
{ |
33
|
|
|
use RepositoryTrait; |
34
|
|
|
use EventsTrait; |
35
|
|
|
use FiltersTrait; |
36
|
|
|
use PaginatorTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class name. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $className; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function getClassName(): string |
49
|
|
|
{ |
50
|
|
|
if ($this->className === null) { |
51
|
|
|
$this->className = ClassUtils::getRealClass($this->getDocumentName()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->className; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Finds documents by a set of criteria. |
59
|
|
|
* |
60
|
|
|
* @param array $criteria |
61
|
|
|
* @param array $sort |
|
|
|
|
62
|
|
|
* @param int|null $limit |
63
|
|
|
* @param int|null $skip |
64
|
|
|
* |
65
|
|
|
* @return ArrayCollection |
66
|
|
|
* |
67
|
|
|
* @codeCoverageIgnore |
68
|
|
|
*/ |
69
|
|
|
public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
70
|
|
|
{ |
71
|
|
|
return new ArrayCollection(parent::findBy($criteria, $sort, $limit, $skip)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected function getFilterCollection() |
78
|
|
|
{ |
79
|
|
|
return $this->getManager()->getFilterCollection(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
protected function getManager(): DocumentManager |
86
|
|
|
{ |
87
|
|
|
return $this->getDocumentManager(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
* |
93
|
|
|
* @param array $criteria |
94
|
|
|
* @param array|null $orderBy |
95
|
|
|
* @param int $itemsPerPage |
96
|
|
|
* |
97
|
|
|
* @return \Zend\Paginator\Paginator |
98
|
|
|
*/ |
99
|
|
|
public function findPaginatedBy($criteria, array $orderBy = null, int $itemsPerPage = 10): Paginator |
100
|
|
|
{ |
101
|
|
|
return $this->paginate($this->getDocumentPersister()->loadAll($criteria, $orderBy), $itemsPerPage); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Paginate MongoDB cursor. |
106
|
|
|
* |
107
|
|
|
* @param Cursor $cursor |
108
|
|
|
* @param int $itemsPerPage |
109
|
|
|
* |
110
|
|
|
* @return Paginator |
111
|
|
|
*/ |
112
|
|
|
protected function paginate(Cursor $cursor, int $itemsPerPage = 10): Paginator |
113
|
|
|
{ |
114
|
|
|
return $this->getPaginator(new MongoDBPaginatorAdapter($cursor), $itemsPerPage); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
* |
120
|
|
|
* @param array $criteria |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function countBy($criteria): int |
125
|
|
|
{ |
126
|
|
|
return $this->getDocumentPersister()->loadAll($criteria)->count(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.