-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBObject.php
More file actions
296 lines (266 loc) · 6.46 KB
/
LudoDBObject.php
File metadata and controls
296 lines (266 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* Base class for LudoDB models and collections.
* User: Alf Magne Kalleland
* Date: 20.12.12
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* Base class for LudoDBModel and LudoDBCollection
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
abstract class LudoDBObject
{
/**
* Internal LudoDB instance.
* @var LudoDB
*/
protected $db;
/**
* Constructor arguments
* @var array
*/
protected $arguments;
/**
* Internal cache of config parsers for LudoDBObjects.
* @var array
*/
protected static $configParsers = array();
/**
* True when config is in JSONConfig/<class name>.json file
* @var bool
*/
protected $JSONConfig = false;
/**
* Internal reference to LudoDBSql
* @var LudoDBSql
*/
private $sql_handler;
/**
* LudoDB config
* @example examples/cities/DemoCity.php
* @var array
*/
protected $config;
/**
* Valid public services offered by this class, example "read", "delete" and "save"
* @return array
*/
public function getValidServices()
{
return array();
}
/**
* LudoDBService getOnSuccessMessageFor method. By default, it returns an empty string.
* @param $service
* @return string
*/
public function getOnSuccessMessageFor($service){
return "";
}
/**
* Internal reference to config parser
* @var LudoDBCollectionConfigParser|LudoDBConfigParser
*/
protected $parser;
/**
* Constructs a new LudoDBModel/LudoDBCollection
*/
public function __construct()
{
$this->db = LudoDb::getInstance();
if (func_num_args() > 0) {
$this->arguments = $this->escapeArguments(func_get_args());
}
$this->parser = $this->configParser();
$this->onConstruct();
}
/**
* Return SQL handler
* @return LudoDBSql
*/
protected function sqlHandler()
{
if (!isset($this->sql_handler)) {
$this->sql_handler = new LudoDBSql($this);
}
return $this->sql_handler;
}
/**
* Escape constructor arguments.
* @param $values
* @return array
*/
protected function escapeArguments($values)
{
$ret = array();
foreach ($values as $value) {
if(isset($value))$ret[] = $this->db->escapeString($value);
}
return $ret;
}
/**
* On construct method which can be implemented by sub classes.
*/
protected function onConstruct()
{
}
/**
* Returns true if config is defined in external file.
* @return bool
*/
public function hasConfigInExternalFile()
{
return $this->JSONConfig;
}
/**
* Return array of values sent to constructor.
* @return array
*/
public function getConstructorValues()
{
return $this->arguments;
}
/**
* Commit method implemented by sub classes.
*/
public function commit()
{
}
/**
* Return reference to config parser.
* @return LudoDBConfigParser|LudoDBCollectionConfigParser
*/
public function configParser()
{
if (!isset($this->parser)) {
$key = $this->getConfigParserKey();
if (!isset(self::$configParsers[$key])) {
self::$configParsers[$key] = $this->getConfigParserInstance();
}
$this->parser = self::$configParsers[$key];
}
return $this->parser;
}
/**
* Return config parser instance.
* @return LudoDBConfigParser
*/
protected function getConfigParserInstance()
{
return new LudoDBConfigParser($this, isset($this->config) ? $this->config : array());
}
/**
* The key of this class in the static $configParsers cache array.
* @var string
*/
private $configParserKey;
/**
* Return config parser key of this class.
* @return string
*/
protected function getConfigParserKey()
{
if (!isset($this->configParserKey)) {
$this->configParserKey = get_class($this);
}
return $this->configParserKey;
}
/**
* Clear all cached config parsers
*/
public static function clearParsers()
{
self::$configParsers = array();
}
/**
* Return uncommitted data. This method is implemented in LudoDBModel.
* @return array
*/
public function getUncommitted()
{
return array();
}
/**
* Implemented by sub classes.
* @return null
*/
public function getId()
{
return null;
}
/**
* Return data as JSON string.
* @return string
*/
public function __toString()
{
return $this->asJSON();
}
/**
* Return data as JSON.
* @return string
*/
public function asJSON()
{
return json_encode($this->getValues());
}
/**
* When handled by LudoDBRequestHandler no services will by default be cached. This method should
* be implemented by sub classes when needed.
* @param string $service
* @return bool
*/
public function shouldCache($service)
{
return false;
}
/**
* Implemented by sub classes.
* @return mixed
*/
abstract public function getValues();
/**
* Clear database cache for this instance.
*/
protected function clearCache()
{
if ($this->shouldCache("read") && !empty($this->arguments)) {
LudoDBCache::clearBy(get_class($this) . "_" . implode("_", $this->arguments));
}
}
/**
* Return data for this instance.
* @return mixed
*/
public function read()
{
return $this->getValues();
}
/**
*
* Returns true if database table has rows where one of the given columns has one of the
* given values.
*
* Example:
*
* <code>
* if($this->hasRowWith(array("email" => "name@dhtmlgoodies.com"));
* </code>
*
* @param array $columnsEqual
*
*/
public function hasRowWith(array $columnsEqual){
$sql= "select * from ". $this->parser->getTableName()." where ";
$sql.= implode("=? or ", array_keys($columnsEqual));
$sql.= "=?";
$row = $this->db->one($sql, array_values($columnsEqual));
if(isset($row)){
return true;
}
return false;
}
}