|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\core\entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
7
|
|
|
use WebComplete\core\condition\ConditionDbParser; |
|
8
|
|
|
use WebComplete\core\factory\ObjectFactory; |
|
9
|
|
|
use WebComplete\core\utils\hydrator\HydratorInterface; |
|
10
|
|
|
use WebComplete\core\condition\Condition; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractEntityRepositoryDb extends AbstractEntityRepository |
|
13
|
|
|
{ |
|
14
|
|
|
const SERIALIZE_STRATEGY_JSON = 1; |
|
15
|
|
|
const SERIALIZE_STRATEGY_PHP = 2; |
|
16
|
|
|
|
|
17
|
|
|
protected $table; |
|
18
|
|
|
protected $serializeFields = []; |
|
19
|
|
|
protected $serializeStrategy = self::SERIALIZE_STRATEGY_JSON; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Connection */ |
|
22
|
|
|
protected $db; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ConditionDbParser |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $conditionParser; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param ObjectFactory $factory |
|
31
|
|
|
* @param HydratorInterface $hydrator |
|
32
|
|
|
* @param ConditionDbParser $conditionParser |
|
33
|
|
|
* @param Connection $db |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
ObjectFactory $factory, |
|
37
|
|
|
HydratorInterface $hydrator, |
|
38
|
|
|
ConditionDbParser $conditionParser, |
|
39
|
|
|
Connection $db |
|
40
|
|
|
) { |
|
41
|
|
|
parent::__construct($factory, $hydrator); |
|
42
|
|
|
$this->db = $db; |
|
43
|
|
|
$this->conditionParser = $conditionParser; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \Closure $closure |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
public function transaction(\Closure $closure) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->db->transactional($closure); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $id |
|
57
|
|
|
* @return AbstractEntity|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function findById($id) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->findOne(new Condition(['t1.id' => $id])); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Condition $condition |
|
66
|
|
|
* @return AbstractEntity|null |
|
67
|
|
|
*/ |
|
68
|
|
|
public function findOne(Condition $condition) |
|
69
|
|
|
{ |
|
70
|
|
|
$result = null; |
|
71
|
|
|
$select = $this->selectQuery($condition); |
|
72
|
|
|
if ($row = $select->execute()->fetch()) { |
|
73
|
|
|
$result = $this->rowToEntity($row); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Condition $condition |
|
81
|
|
|
* @return AbstractEntity[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function findAll(Condition $condition = null): array |
|
84
|
|
|
{ |
|
85
|
|
|
$result = []; |
|
86
|
|
|
$select = $this->selectQuery($condition); |
|
87
|
|
|
if ($rows = $select->execute()->fetchAll(\PDO::FETCH_ASSOC)) { |
|
88
|
|
|
foreach ($rows as $row) { |
|
89
|
|
|
$entity = $this->rowToEntity($row); |
|
90
|
|
|
$result[$entity->getId()] = $entity; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param Condition $condition |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
|
|
public function count(Condition $condition): int |
|
102
|
|
|
{ |
|
103
|
|
|
$select = $this->selectQuery($condition); |
|
104
|
|
|
return $select->select(['id'])->execute()->rowCount(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param AbstractEntity $item |
|
109
|
|
|
*/ |
|
110
|
|
|
public function save(AbstractEntity $item) |
|
111
|
|
|
{ |
|
112
|
|
|
$data = $this->hydrator->extract($item); |
|
113
|
|
|
$this->beforeDataSave($data); |
|
114
|
|
|
$this->serializeFields($data); |
|
115
|
|
|
if ($id = $item->getId()) { |
|
116
|
|
|
$this->db->update($this->table, $data, ['id' => $id]); |
|
117
|
|
|
} else { |
|
118
|
|
|
unset($data['id']); |
|
119
|
|
|
$this->db->insert($this->table, $data); |
|
120
|
|
|
$item->setId((int)$this->db->lastInsertId()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param $id |
|
126
|
|
|
* |
|
127
|
|
|
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function delete($id) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->db->delete($this->table, ['id' => $id]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* |
|
136
|
|
|
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function deleteAll() |
|
139
|
|
|
{ |
|
140
|
|
|
$this->db->delete($this->table, []); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param Condition|null $condition |
|
145
|
|
|
* @return QueryBuilder |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function selectQuery(Condition $condition = null): QueryBuilder |
|
148
|
|
|
{ |
|
149
|
|
|
$queryBuilder = $this->db->createQueryBuilder(); |
|
150
|
|
|
$queryBuilder->select(['t1.*'])->from($this->table, 't1'); |
|
151
|
|
|
$this->conditionParser->parse($queryBuilder, $condition); |
|
152
|
|
|
return $queryBuilder; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Adjust data before save |
|
157
|
|
|
* @param $data |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function beforeDataSave(&$data) |
|
160
|
|
|
{ |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param $data |
|
165
|
|
|
* |
|
166
|
|
|
* @return AbstractEntity |
|
167
|
|
|
*/ |
|
168
|
|
|
private function rowToEntity($data) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->unserializeFields($data); |
|
171
|
|
|
$entity = $this->factory->createFromData($data); |
|
172
|
|
|
/** @var AbstractEntity $entity */ |
|
173
|
|
|
return $entity; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param $data |
|
178
|
|
|
*/ |
|
179
|
|
|
private function serializeFields(&$data) |
|
180
|
|
|
{ |
|
181
|
|
|
foreach ($this->serializeFields as $field) { |
|
182
|
|
|
if (isset($data[$field])) { |
|
183
|
|
|
$data[$field] = $this->serializeStrategy === self::SERIALIZE_STRATEGY_JSON |
|
184
|
|
|
? \json_encode($data[$field]) |
|
185
|
|
|
: \serialize($data[$field]); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param $row |
|
192
|
|
|
*/ |
|
193
|
|
|
private function unserializeFields(&$row) |
|
194
|
|
|
{ |
|
195
|
|
|
foreach ($this->serializeFields as $field) { |
|
196
|
|
|
if (isset($row[$field])) { |
|
197
|
|
|
$row[$field] = $this->serializeStrategy === self::SERIALIZE_STRATEGY_JSON |
|
198
|
|
|
? \json_decode($row[$field], true) |
|
199
|
|
|
: \unserialize($row[$field], ['allowed_classes' => true]); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.