1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: jg |
5
|
|
|
* Date: 21/06/16 |
6
|
|
|
* Time: 16:17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ByJG\MicroOrm; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use ByJG\AnyDataset\DbDriverInterface; |
13
|
|
|
use ByJG\Serializer\BinderObject; |
14
|
|
|
|
15
|
|
|
class Repository |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Mapper |
20
|
|
|
*/ |
21
|
|
|
protected $mapper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DbDriverInterface |
25
|
|
|
*/ |
26
|
|
|
protected $dbDriver = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Repository constructor. |
30
|
|
|
* @param DbDriverInterface $dbDataset |
31
|
|
|
* @param Mapper $mapper |
32
|
|
|
*/ |
33
|
8 |
|
public function __construct(DbDriverInterface $dbDataset, Mapper $mapper) |
34
|
|
|
{ |
35
|
8 |
|
$this->dbDriver = $dbDataset; |
36
|
8 |
|
$this->mapper = $mapper; |
37
|
8 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Mapper |
41
|
|
|
*/ |
42
|
|
|
public function getMapper() |
43
|
|
|
{ |
44
|
|
|
return $this->mapper; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return DbDriverInterface |
49
|
|
|
*/ |
50
|
8 |
|
protected function getDbDriver() |
51
|
|
|
{ |
52
|
8 |
|
return $this->dbDriver; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array|string $id |
57
|
|
|
* @return mixed|null |
58
|
|
|
*/ |
59
|
5 |
|
public function get($id) |
60
|
|
|
{ |
61
|
5 |
|
$result = $this->getByFilter($this->mapper->getPrimaryKey() . ' = [[id]]', ['id' => $id]); |
62
|
|
|
|
63
|
5 |
|
if (count($result) === 1) { |
64
|
5 |
|
return $result[0]; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $id |
72
|
|
|
* @return mixed|null |
73
|
|
|
*/ |
74
|
1 |
|
public function delete($id) |
75
|
|
|
{ |
76
|
1 |
|
$params = ['id' => $id]; |
77
|
1 |
|
$query = new Query(); |
78
|
1 |
|
$query->table($this->mapper->getTable()) |
79
|
1 |
|
->where($this->mapper->getPrimaryKey() . ' = [[id]]', $params); |
80
|
|
|
|
81
|
1 |
|
return $this->deleteByQuery($query); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $query |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
2 |
|
public function deleteByQuery($query) |
89
|
|
|
{ |
90
|
2 |
|
$delete = $query->getDelete(); |
91
|
2 |
|
$sql = $delete['sql']; |
92
|
2 |
|
$params = $delete['params']; |
93
|
|
|
|
94
|
2 |
|
$this->getDbDriver()->execute($sql, $params); |
95
|
|
|
|
96
|
2 |
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $filter |
101
|
|
|
* @param array $params |
102
|
|
|
* @param bool $forUpdate |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
5 |
|
public function getByFilter($filter, array $params, $forUpdate = false) |
106
|
|
|
{ |
107
|
5 |
|
$query = new Query(); |
108
|
5 |
|
$query->table($this->mapper->getTable()) |
109
|
5 |
|
->where($filter, $params); |
110
|
|
|
|
111
|
5 |
|
if ($forUpdate) { |
112
|
|
|
$query->forUpdate(); |
113
|
|
|
} |
114
|
|
|
|
115
|
5 |
|
return $this->getByQuery($query); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Query $query |
120
|
|
|
* @param Mapper[] $mapper |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
8 |
|
public function getByQuery(Query $query, array $mapper = []) |
124
|
|
|
{ |
125
|
8 |
|
$mapper = array_merge([$this->mapper], $mapper); |
126
|
8 |
|
$query = $query->getSelect(); |
127
|
|
|
|
128
|
8 |
|
$result = []; |
129
|
8 |
|
$iterator = $this->getDbDriver()->getIterator($query['sql'], $query['params']); |
|
|
|
|
130
|
|
|
|
131
|
8 |
|
foreach ($iterator as $row) { |
|
|
|
|
132
|
8 |
|
$collection = []; |
133
|
8 |
|
foreach ($mapper as $item) { |
134
|
8 |
|
$instance = $item->getEntity(); |
135
|
8 |
|
BinderObject::bindObject($row->toArray(), $instance); |
136
|
|
|
|
137
|
8 |
|
foreach ((array)$item->getFieldMap() as $property => $fieldName) { |
138
|
3 |
|
$instance->$property = $row->getField($fieldName); |
139
|
8 |
|
} |
140
|
8 |
|
$collection[] = $instance; |
141
|
8 |
|
} |
142
|
8 |
|
$result[] = count($collection) === 1 ? $collection[0] : $collection; |
143
|
8 |
|
} |
144
|
|
|
|
145
|
8 |
|
return $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param mixed $instance |
150
|
|
|
*/ |
151
|
2 |
|
public function save($instance) |
152
|
|
|
{ |
153
|
2 |
|
$array = BinderObject::toArrayFrom($instance, true); |
154
|
|
|
|
155
|
2 |
|
$query = new Query(); |
156
|
2 |
|
$query->table($this->mapper->getTable()) |
157
|
2 |
|
->fields(array_keys($array)); |
158
|
|
|
|
159
|
2 |
|
if (empty($array[$this->mapper->getPrimaryKey()]) || count($this->get($array[$this->mapper->getPrimaryKey()])) === 0) { |
160
|
1 |
|
$array[$this->mapper->getPrimaryKey()] = $this->insert($query, $array); |
161
|
1 |
|
BinderObject::bindObject($array, $instance); |
162
|
1 |
|
} else { |
163
|
1 |
|
$this->update($query, $array); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Query $query |
170
|
|
|
* @param array $params |
171
|
|
|
* @return int |
172
|
|
|
* @throws \Exception |
173
|
|
|
*/ |
174
|
1 |
|
protected function insert(Query $query, array $params) |
175
|
|
|
{ |
176
|
1 |
|
$sql = $query->getInsert(); |
177
|
1 |
|
$dbFunctions = $this->getDbDriver()->getDbHelper(); |
178
|
1 |
|
return $dbFunctions->executeAndGetInsertedId($this->getDbDriver(), $sql, $params); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Query $query |
183
|
|
|
* @param array $params |
184
|
|
|
* @throws \Exception |
185
|
|
|
*/ |
186
|
1 |
|
protected function update(Query $query, array $params) |
187
|
|
|
{ |
188
|
1 |
|
$params = array_merge($params, ['_id' => $params[$this->mapper->getPrimaryKey()]]); |
189
|
1 |
|
$query->where($this->mapper->getPrimaryKey() . ' = [[_id]] ', ['_id' => $params['_id']]); |
190
|
1 |
|
$update = $query->getUpdate(); |
191
|
1 |
|
$sql = $update['sql']; |
192
|
|
|
|
193
|
1 |
|
$this->getDbDriver()->execute($sql, $params); |
194
|
1 |
|
} |
195
|
|
|
} |
196
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.