|
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\Util\ClassUtils; |
|
17
|
|
|
use Doctrine\ODM\MongoDB\Cursor; |
|
18
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
19
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
|
20
|
|
|
use Jgut\Doctrine\Repository\EventsTrait; |
|
21
|
|
|
use Jgut\Doctrine\Repository\FiltersTrait; |
|
22
|
|
|
use Jgut\Doctrine\Repository\PaginatorTrait; |
|
23
|
|
|
use Jgut\Doctrine\Repository\Repository; |
|
24
|
|
|
use Jgut\Doctrine\Repository\RepositoryTrait; |
|
25
|
|
|
use Zend\Paginator\Paginator; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* MongoDB document repository. |
|
29
|
|
|
*/ |
|
30
|
|
|
class MongoDBRepository extends DocumentRepository implements Repository |
|
31
|
|
|
{ |
|
32
|
|
|
use RepositoryTrait; |
|
33
|
|
|
use EventsTrait; |
|
34
|
|
|
use FiltersTrait; |
|
35
|
|
|
use PaginatorTrait; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class name. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $className; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getClassName(): string |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->className === null) { |
|
50
|
|
|
$this->className = ClassUtils::getRealClass($this->getDocumentName()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->className; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getFilterCollection() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getManager()->getFilterCollection(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getManager(): DocumentManager |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->getDocumentManager(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $criteria |
|
76
|
|
|
* @param array|null $orderBy |
|
77
|
|
|
* @param int $itemsPerPage |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Zend\Paginator\Paginator |
|
80
|
|
|
*/ |
|
81
|
|
|
public function findPaginatedBy($criteria, array $orderBy = null, int $itemsPerPage = 10): Paginator |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->paginate($this->getDocumentPersister()->loadAll($criteria, $orderBy), $itemsPerPage); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Paginate MongoDB cursor. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Cursor $cursor |
|
90
|
|
|
* @param int $itemsPerPage |
|
91
|
|
|
* |
|
92
|
|
|
* @return Paginator |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function paginate(Cursor $cursor, int $itemsPerPage = 10): Paginator |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getPaginator(new MongoDBPaginatorAdapter($cursor), $itemsPerPage); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $criteria |
|
103
|
|
|
* |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public function countBy($criteria): int |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->getDocumentPersister()->loadAll($criteria)->count(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|