|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\core\entity; |
|
4
|
|
|
|
|
5
|
|
|
use WebComplete\core\condition\Condition; |
|
6
|
|
|
use WebComplete\core\condition\ConditionMicroDbParser; |
|
7
|
|
|
use WebComplete\core\factory\EntityFactory; |
|
8
|
|
|
use WebComplete\microDb\MicroDb; |
|
9
|
|
|
|
|
10
|
|
|
class AbstractEntityRepositoryMicro extends AbstractEntityRepository |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
protected $collectionName = null; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var MicroDb |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $microDb; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConditionMicroDbParser |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $conditionParser; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param EntityFactory $factory |
|
26
|
|
|
* @param MicroDb $microDb |
|
27
|
|
|
* @param ConditionMicroDbParser $conditionParser |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(EntityFactory $factory, MicroDb $microDb, ConditionMicroDbParser $conditionParser) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($factory); |
|
32
|
|
|
$this->microDb = $microDb; |
|
33
|
|
|
$this->conditionParser = $conditionParser; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \Closure $closure |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function transaction(\Closure $closure) |
|
42
|
|
|
{ |
|
43
|
|
|
$closure(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $id |
|
48
|
|
|
* |
|
49
|
|
|
* @return AbstractEntity|null |
|
50
|
|
|
* @throws \WebComplete\microDb\Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function findById($id) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->findOne($this->createCondition(['id' => $id])); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Condition $condition |
|
59
|
|
|
* |
|
60
|
|
|
* @return AbstractEntity|null |
|
61
|
|
|
* @throws \WebComplete\microDb\Exception |
|
62
|
|
|
*/ |
|
63
|
|
|
public function findOne(Condition $condition) |
|
64
|
|
|
{ |
|
65
|
|
|
$result = null; |
|
66
|
|
|
$row = $this->microDb->getCollection($this->collectionName)->fetchOne( |
|
67
|
|
|
$this->conditionParser->filter($condition), |
|
68
|
|
|
$this->conditionParser->sort($condition) |
|
69
|
|
|
); |
|
70
|
|
|
if ($row) { |
|
71
|
|
|
/** @var AbstractEntity $result */ |
|
72
|
|
|
$result = $this->factory->create(); |
|
73
|
|
|
$result->mapFromArray($row); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Condition $condition |
|
81
|
|
|
* |
|
82
|
|
|
* @return AbstractEntity[] |
|
83
|
|
|
* @throws \WebComplete\microDb\Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function findAll(Condition $condition = null): array |
|
86
|
|
|
{ |
|
87
|
|
|
$result = []; |
|
88
|
|
|
$rows = $this->microDb->getCollection($this->collectionName)->fetchAll( |
|
89
|
|
|
$this->conditionParser->filter($condition, $limit, $offset), |
|
90
|
|
|
$this->conditionParser->sort($condition), |
|
91
|
|
|
$limit, |
|
92
|
|
|
$offset |
|
93
|
|
|
); |
|
94
|
|
|
foreach ($rows as $row) { |
|
95
|
|
|
/** @var AbstractEntity $entity */ |
|
96
|
|
|
$entity = $this->factory->create(); |
|
97
|
|
|
$entity->mapFromArray($row); |
|
98
|
|
|
$result[$entity->getId()] = $entity; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Condition|null $condition |
|
106
|
|
|
* |
|
107
|
|
|
* @return int |
|
108
|
|
|
* @throws \WebComplete\microDb\Exception |
|
109
|
|
|
*/ |
|
110
|
|
|
public function count(Condition $condition = null): int |
|
111
|
|
|
{ |
|
112
|
|
|
return \count($this->findAll($condition)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param AbstractEntity $item |
|
117
|
|
|
* @throws \WebComplete\microDb\Exception |
|
118
|
|
|
*/ |
|
119
|
|
|
public function save(AbstractEntity $item) |
|
120
|
|
|
{ |
|
121
|
|
|
$collection = $this->microDb->getCollection($this->collectionName); |
|
122
|
|
|
if ($id = (string)$item->getId()) { |
|
123
|
|
|
$collection->update(function ($row) use ($id) { |
|
124
|
|
|
return isset($row['id']) && (string)$row['id'] === $id; |
|
125
|
|
|
}, $item->mapToArray()); |
|
126
|
|
|
} else { |
|
127
|
|
|
$id = $collection->insert($item->mapToArray()); |
|
128
|
|
|
$item->setId($id); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $id |
|
134
|
|
|
* @throws \WebComplete\microDb\Exception |
|
135
|
|
|
*/ |
|
136
|
|
|
public function delete($id) |
|
137
|
|
|
{ |
|
138
|
|
|
$id = (string)$id; |
|
139
|
|
|
$this->microDb->getCollection($this->collectionName)->delete(function ($row) use ($id) { |
|
140
|
|
|
return isset($row['id']) && (string)$row['id'] === $id; |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param Condition|null $condition |
|
146
|
|
|
* @throws \WebComplete\microDb\Exception |
|
147
|
|
|
*/ |
|
148
|
|
|
public function deleteAll(Condition $condition = null) |
|
149
|
|
|
{ |
|
150
|
|
|
$filter = $this->conditionParser->filter($condition); |
|
151
|
|
|
if (!$filter) { |
|
152
|
|
|
$filter = function () { |
|
153
|
|
|
return true; |
|
154
|
|
|
}; |
|
155
|
|
|
} |
|
156
|
|
|
$this->microDb->getCollection($this->collectionName)->delete($filter); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $field |
|
161
|
|
|
* @param string $key |
|
162
|
|
|
* @param Condition|null $condition |
|
163
|
|
|
* |
|
164
|
|
|
* @return array |
|
165
|
|
|
* @throws \WebComplete\microDb\Exception |
|
166
|
|
|
* @throws \TypeError |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getMap(string $field, string $key = 'id', Condition $condition = null): array |
|
169
|
|
|
{ |
|
170
|
|
|
$result = []; |
|
171
|
|
|
$items = $this->findAll($condition); |
|
172
|
|
|
foreach ($items as $item) { |
|
173
|
|
|
$data = $item->mapToArray(); |
|
174
|
|
|
if (isset($data[$key])) { |
|
175
|
|
|
$result[$data[$key]] = $data[$field] ?? null; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
return $result; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|