1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* doctrine-couchdb-odm-repositories (https://github.com/juliangut/doctrine-couchdb-odm-repositories). |
5
|
|
|
* Doctrine2 CouchDB ODM utility entity repositories. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/juliangut/doctrine-couchdb-odm-repositories |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Jgut\Doctrine\Repository\CouchDB\ODM; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Util\ClassUtils; |
17
|
|
|
use Doctrine\ODM\CouchDB\DocumentManager; |
18
|
|
|
use Doctrine\ODM\CouchDB\DocumentRepository; |
19
|
|
|
use Jgut\Doctrine\Repository\EventsTrait; |
20
|
|
|
use Jgut\Doctrine\Repository\FiltersTrait; |
21
|
|
|
use Jgut\Doctrine\Repository\PaginatorTrait; |
22
|
|
|
use Jgut\Doctrine\Repository\Repository; |
23
|
|
|
use Jgut\Doctrine\Repository\RepositoryTrait; |
24
|
|
|
use Zend\Paginator\Paginator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* CouchDB document repository. |
28
|
|
|
*/ |
29
|
|
|
class CouchDBRepository extends DocumentRepository implements Repository |
30
|
|
|
{ |
31
|
|
|
use RepositoryTrait { |
32
|
|
|
refresh as baseRefresh; |
33
|
|
|
} |
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
|
|
|
* {@inheritdoc} |
59
|
|
|
* |
60
|
|
|
* @throws \LogicException |
61
|
|
|
*/ |
62
|
|
|
protected function getFilterCollection() |
63
|
|
|
{ |
64
|
|
|
throw new \LogicException('Doctrine\'s CouchDB manager does not implement filters'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function getManager(): DocumentManager |
71
|
|
|
{ |
72
|
|
|
return $this->getDocumentManager(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* |
78
|
|
|
* @param array $criteria |
79
|
|
|
* @param array|null $orderBy |
80
|
|
|
* @param int $itemsPerPage |
81
|
|
|
* |
82
|
|
|
* @return \Zend\Paginator\Paginator |
83
|
|
|
*/ |
84
|
|
|
public function findPaginatedBy($criteria, array $orderBy = null, int $itemsPerPage = 10): Paginator |
85
|
|
|
{ |
86
|
|
|
if (!is_array($criteria)) { |
87
|
|
|
$criteria = [$criteria]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->paginate($this->findBy($criteria, $orderBy)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Paginate CouchDB results. |
95
|
|
|
* |
96
|
|
|
* @param array $results |
97
|
|
|
* @param int $itemsPerPage |
98
|
|
|
* |
99
|
|
|
* @return Paginator |
100
|
|
|
*/ |
101
|
|
|
protected function paginate(array $results, int $itemsPerPage = 10): Paginator |
102
|
|
|
{ |
103
|
|
|
return $this->getPaginator(new CouchDBPaginatorAdapter($results), $itemsPerPage); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
* |
109
|
|
|
* @param array $criteria |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function countBy($criteria): int |
114
|
|
|
{ |
115
|
|
|
return count($this->findBy($criteria)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Flush managed object. |
120
|
|
|
* |
121
|
|
|
* @param object|object[]|\Traversable $objects |
122
|
|
|
* @param bool $flush |
123
|
|
|
* |
124
|
|
|
* @SuppressWarnings(PMD.UnusedFormalParameter) |
125
|
|
|
*/ |
126
|
|
|
protected function flushObjects($objects, bool $flush) |
127
|
|
|
{ |
128
|
|
|
if ($flush || $this->autoFlush) { |
129
|
|
|
$this->getManager()->flush(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|