-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseQueryTrait.php
More file actions
114 lines (93 loc) · 2.61 KB
/
Copy pathBaseQueryTrait.php
File metadata and controls
114 lines (93 loc) · 2.61 KB
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
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\Model\Queries\Abstract;
use ADT\Components\AjaxSelect\Traits\OrByIdFilterTrait;
use ADT\DoctrineComponents\QueryObject\Filters\IsActiveFilter;
use ADT\DoctrineComponents\QueryObject\Filters\IsActiveFilterTrait;
use ADT\DoctrineComponents\QueryObject\QueryObjectByMode;
use ADT\FancyAdmin\Model\Entities\Account;
use ADT\FancyAdmin\Model\Security\SecurityUser;
use Doctrine\ORM\QueryBuilder;
trait BaseQueryTrait
{
use OrByIdFilterTrait;
use IsActiveFilterTrait;
abstract protected function applySecurityFilter(): void;
abstract protected function applyAccountFilter(QueryBuilder $qb, Account $account): void;
protected SecurityUser $securityUser;
public function init(): void
{
parent::init();
$this->applySecurityFilter();
if ($this instanceof IsActiveFilter) {
$this->filter[IsActiveFilter::IS_ACTIVE_FILTER] = fn() => $this->byIsActive();
}
$this->filter[BaseQuery::ACCOUNT_FILTER] = function (QueryBuilder $qb) {
if (
$this->securityUser->isLoggedIn()
&&
($account = $this->securityUser->getIdentity()->getSelectedAccount())
) {
$this->applyAccountFilter($qb, $account);
}
};
}
/**
* @return string
*/
public function getEntityClass(): string
{
$fullClassQueryName = get_class($this);
$fullClassEntityName = str_replace("Queries", "Entities", $fullClassQueryName);
$fullClassEntityName = str_replace("Query", "", $fullClassEntityName);
return $fullClassEntityName;
}
public function setSecurityUser(SecurityUser $securityUser): static
{
$this->securityUser = $securityUser;
return $this;
}
public function disableSecurityFilter(): static
{
unset($this->filter[BaseQuery::SECURITY_FILTER]);
return $this;
}
public function disableAccountFilter(): static
{
unset($this->filter[BaseQuery::ACCOUNT_FILTER]);
return $this;
}
public function fetchPairs(?string $value = 'name', ?string $key = 'id'): array
{
return parent::fetchPairs($value, $key);
}
public function byIdNot(int|array|null $id): static
{
$id = (array) $id;
$id = array_filter($id);
if (!$id) {
return $this;
}
return $this->by('id', $id, QueryObjectByMode::NOT_IN_ARRAY);
}
public function byId($id): static
{
if ($this instanceof IsActiveFilter) {
$this->disableIsActiveFilter();
}
return parent::byId($id);
}
final protected function addFilter(callable $callback, ?string $name = null): static
{
if ($name) {
$this->filter[$name] = $callback;
} else {
$this->filter[] = $callback;
}
return $this;
}
protected function getSecurityUser(): SecurityUser
{
return $this->securityUser;
}
}