1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Service; |
20
|
|
|
|
21
|
|
|
use Doctrine\ORM\Query; |
22
|
|
|
use Doctrine\ORM\QueryBuilder; |
23
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
24
|
|
|
use Pagerfanta\Pagerfanta; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\AbstractQuery; |
27
|
|
|
|
28
|
|
|
class AbstractSearchService |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param \Doctrine\ORM\QueryBuilder|\Doctrine\ORM\Query $doctrineQuery |
32
|
|
|
* @param AbstractQuery $query |
33
|
|
|
* @return Pagerfanta |
34
|
|
|
*/ |
35
|
|
|
protected function createPaginatorFrom($doctrineQuery, AbstractQuery $query) |
36
|
|
|
{ |
37
|
|
|
$queryObject = $doctrineQuery; |
38
|
|
|
if ($doctrineQuery instanceof QueryBuilder) { |
39
|
|
|
$queryObject = $doctrineQuery->getQuery(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!$queryObject instanceof Query) { |
43
|
|
|
throw InvalidArgumentException::invalidType( |
44
|
|
|
'Doctrine\ORM\Query or Doctrine\ORM\QueryBuilder', |
45
|
|
|
'searchQuery', |
46
|
|
|
$doctrineQuery |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$adapter = new DoctrineORMAdapter($doctrineQuery); |
51
|
|
|
$paginator = new Pagerfanta($adapter); |
52
|
|
|
$paginator->setMaxPerPage($query->itemsPerPage); |
53
|
|
|
$paginator->setCurrentPage($query->pageNumber); |
54
|
|
|
|
55
|
|
|
return $paginator; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Query $doctrineQuery |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
protected function getFilteredQueryOptions(Query $doctrineQuery) |
63
|
|
|
{ |
64
|
|
|
$filters = []; |
65
|
|
|
$results = $doctrineQuery->getArrayResult(); |
66
|
|
|
foreach ($results as $options) { |
67
|
|
|
foreach ($options as $key => $value) { |
68
|
|
|
$val = (string)$value; |
69
|
|
|
$filters[$key][$val] = (string)$val; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach ($filters as $key => $filter) { |
74
|
|
|
asort($filters[$key]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $filters; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|