|
1
|
|
|
<?php |
|
2
|
|
|
namespace vakata\orm; |
|
3
|
|
|
|
|
4
|
|
|
use \vakata\database\schema\TableQuery; |
|
5
|
|
|
|
|
6
|
|
|
class DatabaseRepository implements Repository |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var DataMapper |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $mapper; |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TableQuery |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $query; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $consumed = false; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $modified = false; |
|
24
|
|
|
|
|
25
|
14 |
|
public function __construct(DataMapper $mapper, TableQuery $query) |
|
26
|
|
|
{ |
|
27
|
14 |
|
$this->mapper = $mapper; |
|
28
|
14 |
|
$this->query = $query; |
|
29
|
14 |
|
} |
|
30
|
|
|
public function __clone() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->reset(); |
|
33
|
|
|
} |
|
34
|
|
|
public function find($key) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!is_array($key)) { |
|
37
|
|
|
$key = [ $key ]; |
|
38
|
|
|
} |
|
39
|
|
|
foreach ($this->query->getDefinition()->getPrimaryKey() as $field) { |
|
40
|
|
|
$this->filter($field, $key[$field] ?? array_shift($key) ?? null); |
|
41
|
|
|
} |
|
42
|
|
|
return $this->offsetGet(0); |
|
43
|
|
|
} |
|
44
|
1 |
|
public function any(array $criteria) : Repository |
|
45
|
|
|
{ |
|
46
|
1 |
|
$this->query->any($criteria); |
|
47
|
1 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
1 |
|
public function all(array $criteria) : Repository |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->query->all($criteria); |
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
1 |
|
public function filter(string $column, $value) : Repository |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->query->filter($column, $value); |
|
57
|
1 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
public function reject(string $column, $value) : Repository |
|
60
|
|
|
{ |
|
61
|
|
|
$this->query->filter($column, $value, true); |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
public function sort(string $column, bool $desc = false) : Repository |
|
65
|
|
|
{ |
|
66
|
|
|
$this->query->sort($column, $desc); |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
public function limit(int $limit, int $offset = 0) : Repository |
|
70
|
|
|
{ |
|
71
|
|
|
$this->query->limit($limit, $offset, true); |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
5 |
|
public function count() : int |
|
75
|
|
|
{ |
|
76
|
5 |
|
return $this->query->count(); |
|
77
|
|
|
} |
|
78
|
|
|
public function reset() : Repository |
|
79
|
|
|
{ |
|
80
|
|
|
$this->query->reset(); |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
2 |
|
public function current() |
|
84
|
|
|
{ |
|
85
|
2 |
|
if(!($data = $this->query->getIterator()->current())) { |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
2 |
|
$this->consumed = true; |
|
89
|
2 |
|
return $this->mapper->entity($data); |
|
90
|
|
|
} |
|
91
|
12 |
|
public function offsetGet($offset) |
|
92
|
|
|
{ |
|
93
|
12 |
|
if(!($data = $this->query->offsetGet($offset))) { |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
12 |
|
$this->consumed = true; |
|
97
|
12 |
|
return $this->mapper->entity($data); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function append($entity) : Repository |
|
101
|
|
|
{ |
|
102
|
2 |
|
$this->modified = true; |
|
103
|
2 |
|
$this->mapper->insert($entity); |
|
104
|
2 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
1 |
|
public function change($entity) : Repository |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->modified = true; |
|
109
|
1 |
|
$this->mapper->update($entity); |
|
110
|
1 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
1 |
|
public function remove($entity) : Repository |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->modified = true; |
|
115
|
1 |
|
$this->mapper->delete($entity); |
|
116
|
1 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
public function key() |
|
120
|
|
|
{ |
|
121
|
2 |
|
return $this->query->getIterator()->key(); |
|
122
|
|
|
} |
|
123
|
4 |
|
public function rewind() |
|
124
|
|
|
{ |
|
125
|
4 |
|
$this->query->getIterator()->rewind(); |
|
126
|
4 |
|
} |
|
127
|
2 |
|
public function next() |
|
128
|
|
|
{ |
|
129
|
2 |
|
$this->query->getIterator()->next(); |
|
130
|
2 |
|
} |
|
131
|
4 |
|
public function valid() |
|
132
|
|
|
{ |
|
133
|
4 |
|
return $this->query->getIterator()->valid(); |
|
134
|
|
|
} |
|
135
|
|
|
public function offsetExists($offset) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->query->offsetExists($offset); |
|
138
|
|
|
} |
|
139
|
|
|
public function offsetUnset($offset) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->remove($this->offsetGet($offset)); |
|
142
|
|
|
} |
|
143
|
|
|
public function offsetSet($offset, $value) |
|
144
|
|
|
{ |
|
145
|
|
|
if ($offset !== null) { |
|
146
|
|
|
throw new \BadMethodCallException(); |
|
147
|
|
|
} |
|
148
|
|
|
$this->append($value); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function isConsumed() : bool |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->consumed; |
|
154
|
|
|
} |
|
155
|
|
|
public function isModified() : bool |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->modified; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
public function toArray($entity) : array |
|
161
|
|
|
{ |
|
162
|
1 |
|
return $this->mapper->toArray($entity, false); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function search(string $q) : Repository |
|
166
|
|
|
{ |
|
167
|
|
|
$sql = []; |
|
168
|
|
|
$par = []; |
|
169
|
|
|
$table = $this->query->getDefinition()->getName(); |
|
170
|
|
|
foreach ($this->query->getDefinition()->getFullColumns() as $name => $column) { |
|
171
|
|
|
if ($column->getBasicType() === 'text') { |
|
172
|
|
|
$sql[] = $table . '.' . $name . ' = ?'; |
|
173
|
|
|
$par[] = $q; |
|
174
|
|
|
$sql[] = $table . '.' . $name . ' LIKE ?'; |
|
175
|
|
|
$par[] = '%' . str_replace(['%', '_'], ['\\%','\\_'], $q) . '%'; |
|
176
|
|
|
} |
|
177
|
|
|
if ($column->getBasicType() === 'int' && is_numeric($q)) { |
|
178
|
|
|
$sql[] = $table . '.' . $name . ' = ?'; |
|
179
|
|
|
$par[] = $q; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
if (count($sql)) { |
|
183
|
|
|
$this->query->where("(".implode(" OR ", $sql).")", $par); |
|
184
|
|
|
} |
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|