1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Database; |
4
|
|
|
|
5
|
|
|
use MongoDB\Driver\Manager; |
6
|
|
|
use PDO; |
7
|
|
|
use DrMVC\Config\ConfigInterface; |
8
|
|
|
use DrMVC\Database\Drivers\Interfaces\QueryInterface; |
9
|
|
|
use DrMVC\Database\Drivers\Interfaces\SQLInterface; |
10
|
|
|
use DrMVC\Database\Drivers\Interfaces\MongodbInterface; |
11
|
|
|
use DrMVC\Database\Interfaces\ModelInterface; |
12
|
|
|
use Stringy\Stringy; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Wrapper for simple work with database objects |
16
|
|
|
* |
17
|
|
|
* @package DrMVC\Database |
18
|
|
|
* @since 3.0 |
19
|
|
|
* |
20
|
|
|
* Virtual methods from SQLInterface: |
21
|
|
|
* @method SQLInterface rawSQL(string $query, array $bind = null, bool $fetch = true); |
22
|
|
|
* @method SQLInterface truncate(); |
23
|
|
|
* |
24
|
|
|
* Virtual methods from MoSQLInterface: |
25
|
|
|
* @method MongodbInterface command(array $query); |
26
|
|
|
*/ |
27
|
|
|
class Model implements ModelInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Connection with database will be stored here |
31
|
|
|
* @var PDO|Manager|QueryInterface |
32
|
|
|
*/ |
33
|
|
|
private $_instance; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Default connection name |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $connection = Database::DEFAULT_CONNECTION; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Name of collection for query |
43
|
|
|
* @var string|null |
44
|
|
|
*/ |
45
|
|
|
protected $collection; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Model constructor. |
49
|
|
|
* @param ConfigInterface $config |
50
|
|
|
* @param string $collection name of active collection |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ConfigInterface $config, string $collection = null) |
53
|
|
|
{ |
54
|
|
|
// Create database object with config from above |
55
|
|
|
$config_db = $this->getConfigDB($config); |
56
|
|
|
$database = new Database($config_db); |
57
|
|
|
|
58
|
|
|
// If collection name is not provided |
59
|
|
|
if (null === $collection) { |
60
|
|
|
// Get current collection name |
61
|
|
|
$collection = $this->getCollection(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Extract instance created by driver and put collection name |
65
|
|
|
$instance = $database->getInstance($collection); |
66
|
|
|
|
67
|
|
|
// Keep driver's instance as local parameter |
68
|
|
|
$this->setInstance($instance); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Extract configuration of current database |
73
|
|
|
* |
74
|
|
|
* @param ConfigInterface $config |
75
|
|
|
* @return ConfigInterface |
76
|
|
|
*/ |
77
|
|
|
private function getConfigDB(ConfigInterface $config): ConfigInterface |
78
|
|
|
{ |
79
|
|
|
// get current connection |
80
|
|
|
$connection = $this->getConnection(); |
81
|
|
|
// Get config of required connection |
82
|
|
|
$config_db = $config->get($connection); |
83
|
|
|
try { |
84
|
|
|
if (null === $config_db) { |
85
|
|
|
throw new Exception("Connection \"$connection\" is not found in database config"); |
86
|
|
|
} |
87
|
|
|
} catch (Exception $e) { |
88
|
|
|
// __constructor |
89
|
|
|
} |
90
|
|
|
return $config_db; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get current connection |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getConnection(): string |
99
|
|
|
{ |
100
|
|
|
return $this->connection; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Convert class name for snake case |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
private function classToShake(): string |
109
|
|
|
{ |
110
|
|
|
$className = \get_class($this); |
111
|
|
|
$classArray = explode('\\', $className); |
112
|
|
|
$class = end($classArray); |
113
|
|
|
try { |
114
|
|
|
$class = Stringy::create($class)->underscored(); |
115
|
|
|
} catch (\InvalidArgumentException $e) { |
116
|
|
|
new Exception('Invalid argument provided'); |
117
|
|
|
} |
118
|
|
|
return $class; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get current collection |
123
|
|
|
* |
124
|
|
|
* @return string|null |
125
|
|
|
*/ |
126
|
|
|
public function getCollection() |
127
|
|
|
{ |
128
|
|
|
return $this->collection ?? $this->classToShake(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set name of collection for queries |
133
|
|
|
* |
134
|
|
|
* @param null|string $collection |
135
|
|
|
* @return ModelInterface |
136
|
|
|
*/ |
137
|
|
|
public function setCollection(string $collection): ModelInterface |
138
|
|
|
{ |
139
|
|
|
$this->collection = $collection; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set database instance |
145
|
|
|
* |
146
|
|
|
* @param PDO|Manager $instance |
147
|
|
|
*/ |
148
|
|
|
private function setInstance($instance) |
149
|
|
|
{ |
150
|
|
|
$this->_instance = $instance; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get database instance |
155
|
|
|
* |
156
|
|
|
* @return PDO|Manager|QueryInterface |
157
|
|
|
*/ |
158
|
|
|
public function getInstance() |
159
|
|
|
{ |
160
|
|
|
return $this->_instance; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Insert data into table/collection |
165
|
|
|
* |
166
|
|
|
* @param array $data array of columns and values |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
|
|
public function insert(array $data) |
170
|
|
|
{ |
171
|
|
|
return $this->getInstance()->insert($data); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Read data from table/collection |
176
|
|
|
* |
177
|
|
|
* @param array $where |
178
|
|
|
* @param array $nosql_options |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
public function select(array $where = [], array $nosql_options = []) |
182
|
|
|
{ |
183
|
|
|
return $this->getInstance()->select($where, $nosql_options); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Update data in table/collection |
188
|
|
|
* |
189
|
|
|
* @param array $data |
190
|
|
|
* @param array $where |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
|
|
public function update(array $data, array $where = []) |
194
|
|
|
{ |
195
|
|
|
return $this->getInstance()->update($data, $where); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Delete data from table/collection |
200
|
|
|
* |
201
|
|
|
* @param array $where |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
|
|
public function delete(array $where) |
205
|
|
|
{ |
206
|
|
|
return $this->getInstance()->delete($where); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Call some dynamic method |
211
|
|
|
* |
212
|
|
|
* @param string $name |
213
|
|
|
* @param array $arguments |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
|
|
public function __call($name, $arguments) |
217
|
|
|
{ |
218
|
|
|
return $this->getInstance()->$name($arguments); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|