1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineDatatable; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\Expr; |
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
7
|
|
|
use DoctrineDatatable\Exception\ResolveColumnNotHandle; |
8
|
|
|
use DoctrineDatatable\Exception\UnfilterableColumn; |
9
|
|
|
use DoctrineDatatable\Exception\WhereColumnNotHandle; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Column. |
13
|
|
|
* |
14
|
|
|
* @author Mathieu Petrini <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Column |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $alias; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|Expr|null |
30
|
|
|
*/ |
31
|
|
|
private $where; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|callable|null |
35
|
|
|
*/ |
36
|
|
|
private $resolve; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string|callable|null |
40
|
|
|
*/ |
41
|
|
|
private $aliasOrderby; |
42
|
|
|
|
43
|
|
|
public const GLOBAL_ALIAS = 'value'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Column constructor. |
47
|
|
|
* |
48
|
|
|
* @author Mathieu Petrini <[email protected]> |
49
|
|
|
* |
50
|
|
|
* @param string $alias |
51
|
|
|
* @param string $name |
52
|
|
|
* @param mixed|null $where (string, \Doctrine\ORM\Query\Expr) |
53
|
|
|
* @param mixed|null $resolve (string, callable) |
54
|
|
|
*/ |
55
|
|
|
public function __construct(string $alias, string $name, $where = '', $resolve = '', string $aliasOrderby = null) |
56
|
|
|
{ |
57
|
|
|
$this->alias = $alias; |
58
|
|
|
$this->name = $name; |
59
|
|
|
$this->where = $where; |
60
|
|
|
$this->resolve = $resolve; |
61
|
|
|
$this->aliasOrderby = $aliasOrderby ?? $alias; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* PRIVATE METHODS. |
66
|
|
|
*/ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @author Mathieu Petrini <[email protected]> |
70
|
|
|
* |
71
|
|
|
* @param QueryBuilder $query |
72
|
|
|
* @param string $data |
73
|
|
|
* |
74
|
|
|
* @throws ResolveColumnNotHandle |
75
|
|
|
*/ |
76
|
|
|
private function setParameter(QueryBuilder &$query, string $data): void |
77
|
|
|
{ |
78
|
|
|
if (!\is_string($this->resolve) && !\is_callable($this->resolve)) { |
79
|
|
|
throw new ResolveColumnNotHandle(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$query->setParameter( |
83
|
|
|
$this->alias, |
84
|
|
|
\is_string($this->resolve) ? |
85
|
|
|
str_replace(':'.$this->alias, $data, $this->resolve) : |
86
|
|
|
\call_user_func($this->resolve, $data) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* PUBLIC METHODS. |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @author Mathieu Petrini <[email protected]> |
96
|
|
|
* |
97
|
|
|
* @param QueryBuilder $query |
98
|
|
|
* @param mixed $data |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
* |
102
|
|
|
* @throws ResolveColumnNotHandle |
103
|
|
|
* @throws WhereColumnNotHandle |
104
|
|
|
* @throws UnfilterableColumn |
105
|
|
|
*/ |
106
|
|
|
public function where(QueryBuilder &$query, $data): string |
107
|
|
|
{ |
108
|
|
|
if (null === $this->where) { |
109
|
|
|
throw new UnfilterableColumn(); |
110
|
|
|
} elseif (!\is_string($this->where) && !$this->where instanceof Expr) { |
|
|
|
|
111
|
|
|
throw new WhereColumnNotHandle(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->setParameter($query, $data); |
115
|
|
|
|
116
|
|
|
return (string) $this->where; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* GETTERS / SETTERS. |
121
|
|
|
*/ |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getAlias(): string |
127
|
|
|
{ |
128
|
|
|
return $this->alias; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getName(): string |
135
|
|
|
{ |
136
|
|
|
return $this->name; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function isHaving(): bool |
143
|
|
|
{ |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getAliasOrderBy(): string |
151
|
|
|
{ |
152
|
|
|
return $this->aliasOrderby; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|