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
|
|
|
public const GLOBAL_ALIAS = 'value'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Column constructor. |
42
|
|
|
* |
43
|
|
|
* @author Mathieu Petrini <[email protected]> |
44
|
|
|
* |
45
|
|
|
* @param string $alias |
46
|
|
|
* @param string $name |
47
|
|
|
* @param mixed|null $where (string, \Doctrine\ORM\Query\Expr) |
48
|
|
|
* @param mixed|null $resolve (string, callable) |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $alias, string $name, $where = '', $resolve = '') |
51
|
|
|
{ |
52
|
|
|
$this->alias = $alias; |
53
|
|
|
$this->name = $name; |
54
|
|
|
$this->where = $where; |
55
|
|
|
$this->resolve = $resolve; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* PRIVATE METHODS. |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @author Mathieu Petrini <[email protected]> |
64
|
|
|
* |
65
|
|
|
* @param QueryBuilder $query |
66
|
|
|
* @param string $data |
67
|
|
|
* |
68
|
|
|
* @throws ResolveColumnNotHandle |
69
|
|
|
*/ |
70
|
|
|
private function setParameter(QueryBuilder &$query, string $data): void |
71
|
|
|
{ |
72
|
|
|
if (!\is_string($this->resolve) && !\is_callable($this->resolve)) { |
73
|
|
|
throw new ResolveColumnNotHandle(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$query->setParameter( |
77
|
|
|
$this->alias, |
78
|
|
|
\is_string($this->resolve) ? |
79
|
|
|
str_replace(':'.$this->alias, $data, $this->resolve) : |
80
|
|
|
\call_user_func($this->resolve, $data) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* PUBLIC METHODS. |
86
|
|
|
*/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @author Mathieu Petrini <[email protected]> |
90
|
|
|
* |
91
|
|
|
* @param QueryBuilder $query |
92
|
|
|
* @param mixed $data |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
* |
96
|
|
|
* @throws ResolveColumnNotHandle |
97
|
|
|
* @throws WhereColumnNotHandle |
98
|
|
|
* @throws UnfilterableColumn |
99
|
|
|
*/ |
100
|
|
|
public function where(QueryBuilder &$query, $data): string |
101
|
|
|
{ |
102
|
|
|
if (null === $this->where) { |
103
|
|
|
throw new UnfilterableColumn(); |
104
|
|
|
} elseif (!\is_string($this->where) && !$this->where instanceof Expr) { |
|
|
|
|
105
|
|
|
throw new WhereColumnNotHandle(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->setParameter($query, $data); |
109
|
|
|
|
110
|
|
|
return (string) $this->where; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* GETTERS / SETTERS. |
115
|
|
|
*/ |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getAlias(): string |
121
|
|
|
{ |
122
|
|
|
return $this->alias; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getName(): string |
129
|
|
|
{ |
130
|
|
|
return $this->name; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|