-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathGroupManager.php
69 lines (56 loc) · 1.96 KB
/
GroupManager.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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\UserBundle\Entity;
use FOS\UserBundle\Doctrine\GroupManager as BaseGroupManager;
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\UserBundle\Model\GroupManagerInterface;
/**
* @author Hugo Briand <briand@ekino.com>
*/
class GroupManager extends BaseGroupManager implements GroupManagerInterface
{
/**
* {@inheritdoc}
*/
public function findGroupsBy(?array $criteria = null, ?array $orderBy = null, $limit = null, $offset = null)
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* {@inheritdoc}
*/
public function getPager(array $criteria, int $page, int $limit = 10, array $sort = []): PagerInterface
{
$query = $this->repository
->createQueryBuilder('g')
->select('g');
$fields = $this->objectManager->getClassMetadata($this->class)->getFieldNames();
foreach ($sort as $field => $direction) {
if (!\in_array($field, $fields, true)) {
throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class));
}
}
if (0 === \count($sort)) {
$sort = ['name' => 'ASC'];
}
foreach ($sort as $field => $direction) {
$query->orderBy(sprintf('g.%s', $field), strtoupper($direction));
}
$parameters = [];
if (isset($criteria['enabled'])) {
$query->andWhere('g.enabled = :enabled');
$parameters['enabled'] = $criteria['enabled'];
}
$query->setParameters($parameters);
return Pager::create($query, $limit, $page);
}
}