-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
DocumentRepository.php
323 lines (285 loc) · 9.15 KB
/
DocumentRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ODM\PHPCR;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
use Doctrine\ODM\PHPCR\Query\Query;
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface as Constants;
use Doctrine\ODM\PHPCR\Exception\InvalidArgumentException;
/**
* A DocumentRepository serves as a repository for documents with generic as well as
* business specific methods for retrieving documents.
*
* This class is designed for inheritance and users can subclass this class to
* write their own repositories with business-specific methods to locate documents.
*
* @license http://www.opensource.org/licenses/MIT-license.php MIT license
* @link www.doctrine-project.com
* @since 1.0
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Pascal Helfenstein <nicam@nicam.ch>
*/
class DocumentRepository implements ObjectRepository
{
const QUERY_REPLACE_WITH_FIELDNAMES = 1;
/**
* @var DocumentManager
*/
protected $dm;
/**
* @var ClassMetadata
*/
protected $class;
/**
* @var UnitOfWork
*/
protected $uow;
/**
* @var string
*/
protected $className;
/**
* Initializes a new <tt>DocumentRepository</tt>.
*
* @param DocumentManager $dm The DocumentManager to use.
* @param ClassMetadata $classMetadata The class descriptor.
*/
public function __construct($dm, ClassMetadata $class)
{
$this->dm = $dm;
$this->class = $class;
$this->uow = $this->dm->getUnitOfWork();
$this->className = $class->name;
}
/**
* Find a single document by its id
*
* The id may either be a PHPCR path or UUID
*
* @param string $id document id
*
* @return object document or null
*/
public function find($id)
{
return $this->dm->find($this->className, $id);
}
/**
* Find many document by id
*
* The ids may either be PHPCR paths or UUID's, but all must be of the same type
*
* @param array $ids document ids
*
* @return array of document objects
*/
public function findMany(array $ids)
{
return $this->dm->findMany($this->className, $ids);
}
/**
* Finds all documents in the repository.
*
* @return array The entities.
*/
public function findAll()
{
return $this->findBy(array());
}
/**
* Finds document by a set of criteria.
*
* Optionally sorting and limiting details can be passed. An implementation may throw
* an UnexpectedValueException if certain values of the sorting or limiting details are
* not supported.
*
* @param array $criteria
* @param array|null $orderBy
* @param int|null $limit
* @param int|null $offset
*
* @return array The objects matching the criteria.
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$qb = $this->createQueryBuilder('a');
if ($limit) {
$qb->setMaxResults($limit);
}
if ($offset) {
$qb->setFirstResult($offset);
}
$orderByNode = $qb->orderBy();
if ($orderBy) {
foreach ($orderBy as $field => $order) {
$order = strtolower($order);
if (!in_array($order, array('asc', 'desc'))) {
throw new InvalidArgumentException(sprintf(
'Invalid order specified by order, expected either "asc" or "desc", got "%s"',
$order
));
}
$method = $order == 'asc' ? 'asc' : 'desc';
$orderByNode->$method()->field('a.'.$field);
}
}
$first = true;
foreach ($criteria as $field => $value) {
if ($first) {
$first = false;
$where = $qb->where();
} else {
$where = $qb->andWhere();
}
$where->eq()->field('a.'.$field)->literal($value);
}
return $qb->getQuery()->execute();
}
/**
* Finds a single document by a set of criteria.
*
* @param array $criteria
*
* @return object|null The first document matching the criteria or null if
* none found
*/
public function findOneBy(array $criteria)
{
$documents = $this->findBy($criteria, null, 1);
return $documents->isEmpty() ? null : $documents->first();
}
/**
* Refresh a document with the data from PHPCR.
*
* @param object $document
*/
public function refresh($document)
{
$this->dm->refresh($document);
}
/**
* @param object $document
*/
public function refreshDocumentForProxy($document)
{
$this->uow->refreshDocumentForProxy($this->className, $document);
}
/**
* Get the document class name this repository is for.
*
* @return string
*/
public function getClassName()
{
return $this->className;
}
/**
* @return DocumentManager
*/
public function getDocumentManager()
{
return $this->dm;
}
/**
* @return \Doctrine\ODM\PHPCR\Mapping\ClassMetadata
*/
public function getClassMetadata()
{
return $this->class;
}
/**
* Quote a string for inclusion in an SQL2 query
*
* @param string $val
* @param int $type
*
* @return string the quoted value
*
* @see \PHPCR\PropertyType
*/
public function quote($val, $type = null)
{
return $this->dm->quote($val, $type);
}
/**
* Escape the illegal characters for inclusion in an SQL2 query. Escape Character is \\.
*
* @param string $string
*
* @return string Escaped String
*
* @see http://jackrabbit.apache.org/api/1.4/org/apache/jackrabbit/util/Text.html #escapeIllegalJcrChars
*/
public function escapeFullText($string)
{
return $this->dm->escapeFullText($string);
}
/**
* Create a Query
*
* @param string $statement the SQL2 statement
* @param string $language (see QueryInterface for list of supported types)
* @param bool $replaceWithFieldnames if * should be replaced with field names automatically
*
* @return Query
*/
public function createQuery($statement, $language, $options = 0)
{
// TODO: refactor this to use the odm query builder
$qb = $this->dm->createPhpcrQueryBuilder()->setFromQuery($statement, $language);
if ($options & self::QUERY_REPLACE_WITH_FIELDNAMES) {
$columns = $qb->getColumns();
if (1 === count($columns)) {
$column = reset($columns);
if ('*' === $column->getColumnName() && null == $column->getPropertyName()) {
$qb->setColumns(array());
foreach ($this->class->getFieldNames() as $name) {
$qb->addSelect('a', $name);
}
}
}
}
$factory = $qb->getQOMFactory();
$comparison = $factory->comparison(
$factory->propertyValue('a', 'phpcr:class'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal($this->className)
);
$qb->andWhere($comparison);
return new Query($qb->getQuery(), $this->getDocumentManager());
}
/**
* Create a QueryBuilder that is pre-populated for this repositories document
*
* The returned query builder will be pre-populated with the criteria
* required to search for this repositories document class.
*
* NOTE: When adding criteria to the query builder you should
* use ->andWhere(...) as ->where(...) will overwrite
* the class criteria.
*
* @param string $selectorName name of the selector, defaults to 'a'
*
* @return \Doctrine\ODM\PHPCR\Query\Builder
*/
public function createQueryBuilder($selectorName)
{
$qb = $this->dm->createQueryBuilder();
$qb->from()->document($this->className, $selectorName);
return $qb;
}
}