forked from acquia/mc-cs-plugin-custom-objects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableConfig.php
111 lines (91 loc) · 2.47 KB
/
TableConfig.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
<?php
declare(strict_types=1);
namespace MauticPlugin\CustomObjectsBundle\DTO;
use Doctrine\DBAL\Query\QueryBuilder as DbalQueryBuilder;
use Doctrine\ORM\QueryBuilder as OrmQueryBuilder;
use Mautic\CoreBundle\Helper\ArrayHelper;
class TableConfig
{
/**
* @var int
*/
private $limit;
/**
* @var int
*/
private $page;
/**
* @var string
*/
private $orderBy;
/**
* @var string
*/
private $orderDirection;
/**
* @var mixed[]
*/
private $parameters = [];
public function __construct(int $limit, int $page, string $orderBy, string $orderDirection = 'ASC')
{
$this->limit = $limit;
$this->page = $page;
$this->orderBy = $orderBy;
$this->orderDirection = $orderDirection;
}
public function getOrderBy(): string
{
return $this->orderBy;
}
public function getOrderDirection(): string
{
return $this->orderDirection;
}
public function getOffset(): int
{
$offset = 1 === $this->page ? 0 : (($this->page - 1) * $this->limit);
return $offset < 0 ? 0 : $offset;
}
public function getLimit(): int
{
return $this->limit;
}
/**
* @param mixed $value
*/
public function addParameter(string $key, $value): void
{
$this->parameters[$key] = $value;
}
/**
* @param mixed $defaultValue
*
* @return mixed
*/
public function getParameter(string $key, $defaultValue = null)
{
return ArrayHelper::getValue($key, $this->parameters, $defaultValue);
}
public function configureOrmQueryBuilder(OrmQueryBuilder $queryBuilder): OrmQueryBuilder
{
return $this->configureQueryBuilder($queryBuilder);
}
public function configureDbalQueryBuilder(DbalQueryBuilder $queryBuilder): DbalQueryBuilder
{
return $this->configureQueryBuilder($queryBuilder);
}
/**
* Both builders can be configured exactly the same way. Let's do it in one method then.
*
* @param DbalQueryBuilder|OrmQueryBuilder $queryBuilder
*
* @return DbalQueryBuilder|OrmQueryBuilder
*/
private function configureQueryBuilder($queryBuilder)
{
$queryBuilder->setMaxResults($this->getLimit());
$queryBuilder->setFirstResult($this->getOffset());
$queryBuilder->orderBy($this->getOrderBy(), $this->getOrderDirection());
return $queryBuilder;
}
}