my-simon 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -20
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/my-simon.gemspec +3 -78
- metadata +2 -77
- data/DOC/Launch Check List.docx +0 -0
- data/DOC/Launch Check List.pdf +0 -0
- data/build/rakefile.rb +0 -12
- data/config.rb +0 -24
- data/scaffolding/simon/controller.tpl +0 -9
- data/scaffolding/simon/model.tpl +0 -10
- data/scaffolding/simon/view.tpl +0 -5
- data/scaffolding/standards/html_template.html +0 -44
- data/scaffolding/standards/html_template.php +0 -44
- data/scaffolding/standards/jquery_plugin_template.js +0 -45
- data/scaffolding/standards/js_template.js +0 -43
- data/www/.htaccess +0 -488
- data/www/404.html +0 -32
- data/www/crossdomain.xml +0 -25
- data/www/favicon.ico +0 -0
- data/www/index.php +0 -17
- data/www/lib/js/homePage.js +0 -43
- data/www/lib/js/jquery/JQbook.js +0 -809
- data/www/lib/js/jquery/jquery-1.8.0.min.js +0 -27
- data/www/lib/js/jquery/jquery.alphanumeric.js +0 -82
- data/www/lib/js/jquery/jquery.cookie.js +0 -96
- data/www/lib/js/jquery/jquery.easing.1.3.js +0 -207
- data/www/lib/js/main.js +0 -117
- data/www/lib/js/master.js +0 -6
- data/www/lib/js/plugins/handlebars-1.0.rc.1.js +0 -1920
- data/www/lib/js/plugins/modernizr-1.7.min.js +0 -2
- data/www/lib/js/plugins/swfobject.js +0 -777
- data/www/lib/js/plugins_mod/README.txt +0 -3
- data/www/lib/php/app.php +0 -53
- data/www/lib/php/controller/index.php +0 -10
- data/www/lib/php/controller/regex.php +0 -23
- data/www/lib/php/model/Book.php +0 -8
- data/www/lib/php/model/Model.php +0 -8
- data/www/lib/php/plugins/php-activerecord/ActiveRecord.php +0 -44
- data/www/lib/php/plugins/php-activerecord/lib/CallBack.php +0 -226
- data/www/lib/php/plugins/php-activerecord/lib/Column.php +0 -155
- data/www/lib/php/plugins/php-activerecord/lib/Config.php +0 -288
- data/www/lib/php/plugins/php-activerecord/lib/Connection.php +0 -456
- data/www/lib/php/plugins/php-activerecord/lib/ConnectionManager.php +0 -38
- data/www/lib/php/plugins/php-activerecord/lib/DateTime.php +0 -45
- data/www/lib/php/plugins/php-activerecord/lib/Exceptions.php +0 -137
- data/www/lib/php/plugins/php-activerecord/lib/Expressions.php +0 -183
- data/www/lib/php/plugins/php-activerecord/lib/Inflector.php +0 -115
- data/www/lib/php/plugins/php-activerecord/lib/Model.php +0 -1673
- data/www/lib/php/plugins/php-activerecord/lib/Reflections.php +0 -86
- data/www/lib/php/plugins/php-activerecord/lib/Relationship.php +0 -637
- data/www/lib/php/plugins/php-activerecord/lib/SQLBuilder.php +0 -396
- data/www/lib/php/plugins/php-activerecord/lib/Serialization.php +0 -302
- data/www/lib/php/plugins/php-activerecord/lib/Singleton.php +0 -57
- data/www/lib/php/plugins/php-activerecord/lib/Table.php +0 -547
- data/www/lib/php/plugins/php-activerecord/lib/Utils.php +0 -351
- data/www/lib/php/plugins/php-activerecord/lib/Validations.php +0 -833
- data/www/lib/php/plugins/php-activerecord/lib/adapters/MysqlAdapter.php +0 -73
- data/www/lib/php/plugins/php-activerecord/lib/adapters/OciAdapter.php +0 -121
- data/www/lib/php/plugins/php-activerecord/lib/adapters/PgsqlAdapter.php +0 -104
- data/www/lib/php/plugins/php-activerecord/lib/adapters/SqliteAdapter.php +0 -81
- data/www/lib/php/system/Config.php +0 -174
- data/www/lib/php/system/config.routes.php +0 -29
- data/www/lib/php/system/router.php +0 -220
- data/www/lib/php/template/footer.php +0 -59
- data/www/lib/php/template/header.php +0 -74
- data/www/lib/php/view/index.php +0 -5
- data/www/media/images/facebook_share.jpg +0 -0
- data/www/robots.txt +0 -5
- data/www/sandbox/readme.txt +0 -3
- data/www/sass/javascript.scss +0 -1
- data/www/sass/layout.scss +0 -128
- data/www/sass/master.scss +0 -4
- data/www/sass/reset.scss +0 -47
- data/www/sass/typography.scss +0 -24
- data/www/styles/javascript.css +0 -1
- data/www/styles/layout.css +0 -186
- data/www/styles/master.css +0 -4
- data/www/styles/reset.css +0 -60
- data/www/styles/typography.css +0 -24
@@ -1,396 +0,0 @@
|
|
1
|
-
<?php
|
2
|
-
/**
|
3
|
-
* @package ActiveRecord
|
4
|
-
*/
|
5
|
-
namespace ActiveRecord;
|
6
|
-
|
7
|
-
/**
|
8
|
-
* Helper class for building sql statements progmatically.
|
9
|
-
*
|
10
|
-
* @package ActiveRecord
|
11
|
-
*/
|
12
|
-
class SQLBuilder
|
13
|
-
{
|
14
|
-
private $connection;
|
15
|
-
private $operation = 'SELECT';
|
16
|
-
private $table;
|
17
|
-
private $select = '*';
|
18
|
-
private $joins;
|
19
|
-
private $order;
|
20
|
-
private $limit;
|
21
|
-
private $offset;
|
22
|
-
private $group;
|
23
|
-
private $having;
|
24
|
-
|
25
|
-
// for where
|
26
|
-
private $where;
|
27
|
-
private $where_values = array();
|
28
|
-
|
29
|
-
// for insert/update
|
30
|
-
private $data;
|
31
|
-
private $sequence;
|
32
|
-
|
33
|
-
/**
|
34
|
-
* Constructor.
|
35
|
-
*
|
36
|
-
* @param Connection $connection A database connection object
|
37
|
-
* @param string $table Name of a table
|
38
|
-
* @return SQLBuilder
|
39
|
-
* @throws ActiveRecordException if connection was invalid
|
40
|
-
*/
|
41
|
-
public function __construct($connection, $table)
|
42
|
-
{
|
43
|
-
if (!$connection)
|
44
|
-
throw new ActiveRecordException('A valid database connection is required.');
|
45
|
-
|
46
|
-
$this->connection = $connection;
|
47
|
-
$this->table = $table;
|
48
|
-
}
|
49
|
-
|
50
|
-
/**
|
51
|
-
* Returns the SQL string.
|
52
|
-
*
|
53
|
-
* @return string
|
54
|
-
*/
|
55
|
-
public function __toString()
|
56
|
-
{
|
57
|
-
return $this->to_s();
|
58
|
-
}
|
59
|
-
|
60
|
-
/**
|
61
|
-
* Returns the SQL string.
|
62
|
-
*
|
63
|
-
* @see __toString
|
64
|
-
* @return string
|
65
|
-
*/
|
66
|
-
public function to_s()
|
67
|
-
{
|
68
|
-
$func = 'build_' . strtolower($this->operation);
|
69
|
-
return $this->$func();
|
70
|
-
}
|
71
|
-
|
72
|
-
/**
|
73
|
-
* Returns the bind values.
|
74
|
-
*
|
75
|
-
* @return array
|
76
|
-
*/
|
77
|
-
public function bind_values()
|
78
|
-
{
|
79
|
-
$ret = array();
|
80
|
-
|
81
|
-
if ($this->data)
|
82
|
-
$ret = array_values($this->data);
|
83
|
-
|
84
|
-
if ($this->get_where_values())
|
85
|
-
$ret = array_merge($ret,$this->get_where_values());
|
86
|
-
|
87
|
-
return array_flatten($ret);
|
88
|
-
}
|
89
|
-
|
90
|
-
public function get_where_values()
|
91
|
-
{
|
92
|
-
return $this->where_values;
|
93
|
-
}
|
94
|
-
|
95
|
-
public function where(/* (conditions, values) || (hash) */)
|
96
|
-
{
|
97
|
-
$this->apply_where_conditions(func_get_args());
|
98
|
-
return $this;
|
99
|
-
}
|
100
|
-
|
101
|
-
public function order($order)
|
102
|
-
{
|
103
|
-
$this->order = $order;
|
104
|
-
return $this;
|
105
|
-
}
|
106
|
-
|
107
|
-
public function group($group)
|
108
|
-
{
|
109
|
-
$this->group = $group;
|
110
|
-
return $this;
|
111
|
-
}
|
112
|
-
|
113
|
-
public function having($having)
|
114
|
-
{
|
115
|
-
$this->having = $having;
|
116
|
-
return $this;
|
117
|
-
}
|
118
|
-
|
119
|
-
public function limit($limit)
|
120
|
-
{
|
121
|
-
$this->limit = intval($limit);
|
122
|
-
return $this;
|
123
|
-
}
|
124
|
-
|
125
|
-
public function offset($offset)
|
126
|
-
{
|
127
|
-
$this->offset = intval($offset);
|
128
|
-
return $this;
|
129
|
-
}
|
130
|
-
|
131
|
-
public function select($select)
|
132
|
-
{
|
133
|
-
$this->operation = 'SELECT';
|
134
|
-
$this->select = $select;
|
135
|
-
return $this;
|
136
|
-
}
|
137
|
-
|
138
|
-
public function joins($joins)
|
139
|
-
{
|
140
|
-
$this->joins = $joins;
|
141
|
-
return $this;
|
142
|
-
}
|
143
|
-
|
144
|
-
public function insert($hash, $pk=null, $sequence_name=null)
|
145
|
-
{
|
146
|
-
if (!is_hash($hash))
|
147
|
-
throw new ActiveRecordException('Inserting requires a hash.');
|
148
|
-
|
149
|
-
$this->operation = 'INSERT';
|
150
|
-
$this->data = $hash;
|
151
|
-
|
152
|
-
if ($pk && $sequence_name)
|
153
|
-
$this->sequence = array($pk,$sequence_name);
|
154
|
-
|
155
|
-
return $this;
|
156
|
-
}
|
157
|
-
|
158
|
-
public function update($hash)
|
159
|
-
{
|
160
|
-
if (!is_hash($hash))
|
161
|
-
throw new ActiveRecordException('Updating requires a hash.');
|
162
|
-
|
163
|
-
$this->operation = 'UPDATE';
|
164
|
-
$this->data = $hash;
|
165
|
-
return $this;
|
166
|
-
}
|
167
|
-
|
168
|
-
public function delete()
|
169
|
-
{
|
170
|
-
$this->operation = 'DELETE';
|
171
|
-
$this->apply_where_conditions(func_get_args());
|
172
|
-
return $this;
|
173
|
-
}
|
174
|
-
|
175
|
-
/**
|
176
|
-
* Reverses an order clause.
|
177
|
-
*/
|
178
|
-
public static function reverse_order($order)
|
179
|
-
{
|
180
|
-
if (!trim($order))
|
181
|
-
return $order;
|
182
|
-
|
183
|
-
$parts = explode(',',$order);
|
184
|
-
|
185
|
-
for ($i=0,$n=count($parts); $i<$n; ++$i)
|
186
|
-
{
|
187
|
-
$v = strtolower($parts[$i]);
|
188
|
-
|
189
|
-
if (strpos($v,' asc') !== false)
|
190
|
-
$parts[$i] = preg_replace('/asc/i','DESC',$parts[$i]);
|
191
|
-
elseif (strpos($v,' desc') !== false)
|
192
|
-
$parts[$i] = preg_replace('/desc/i','ASC',$parts[$i]);
|
193
|
-
else
|
194
|
-
$parts[$i] .= ' DESC';
|
195
|
-
}
|
196
|
-
return join(',',$parts);
|
197
|
-
}
|
198
|
-
|
199
|
-
/**
|
200
|
-
* Converts a string like "id_and_name_or_z" into a conditions value like array("id=? AND name=? OR z=?", values, ...).
|
201
|
-
*
|
202
|
-
* @param Connection $connection
|
203
|
-
* @param $name Underscored string
|
204
|
-
* @param $values Array of values for the field names. This is used
|
205
|
-
* to determine what kind of bind marker to use: =?, IN(?), IS NULL
|
206
|
-
* @param $map A hash of "mapped_column_name" => "real_column_name"
|
207
|
-
* @return A conditions array in the form array(sql_string, value1, value2,...)
|
208
|
-
*/
|
209
|
-
public static function create_conditions_from_underscored_string(Connection $connection, $name, &$values=array(), &$map=null)
|
210
|
-
{
|
211
|
-
if (!$name)
|
212
|
-
return null;
|
213
|
-
|
214
|
-
$parts = preg_split('/(_and_|_or_)/i',$name,-1,PREG_SPLIT_DELIM_CAPTURE);
|
215
|
-
$num_values = count($values);
|
216
|
-
$conditions = array('');
|
217
|
-
|
218
|
-
for ($i=0,$j=0,$n=count($parts); $i<$n; $i+=2,++$j)
|
219
|
-
{
|
220
|
-
if ($i >= 2)
|
221
|
-
$conditions[0] .= preg_replace(array('/_and_/i','/_or_/i'),array(' AND ',' OR '),$parts[$i-1]);
|
222
|
-
|
223
|
-
if ($j < $num_values)
|
224
|
-
{
|
225
|
-
if (!is_null($values[$j]))
|
226
|
-
{
|
227
|
-
$bind = is_array($values[$j]) ? ' IN(?)' : '=?';
|
228
|
-
$conditions[] = $values[$j];
|
229
|
-
}
|
230
|
-
else
|
231
|
-
$bind = ' IS NULL';
|
232
|
-
}
|
233
|
-
else
|
234
|
-
$bind = ' IS NULL';
|
235
|
-
|
236
|
-
// map to correct name if $map was supplied
|
237
|
-
$name = $map && isset($map[$parts[$i]]) ? $map[$parts[$i]] : $parts[$i];
|
238
|
-
|
239
|
-
$conditions[0] .= $connection->quote_name($name) . $bind;
|
240
|
-
}
|
241
|
-
return $conditions;
|
242
|
-
}
|
243
|
-
|
244
|
-
/**
|
245
|
-
* Like create_conditions_from_underscored_string but returns a hash of name => value array instead.
|
246
|
-
*
|
247
|
-
* @param string $name A string containing attribute names connected with _and_ or _or_
|
248
|
-
* @param $args Array of values for each attribute in $name
|
249
|
-
* @param $map A hash of "mapped_column_name" => "real_column_name"
|
250
|
-
* @return array A hash of array(name => value, ...)
|
251
|
-
*/
|
252
|
-
public static function create_hash_from_underscored_string($name, &$values=array(), &$map=null)
|
253
|
-
{
|
254
|
-
$parts = preg_split('/(_and_|_or_)/i',$name);
|
255
|
-
$hash = array();
|
256
|
-
|
257
|
-
for ($i=0,$n=count($parts); $i<$n; ++$i)
|
258
|
-
{
|
259
|
-
// map to correct name if $map was supplied
|
260
|
-
$name = $map && isset($map[$parts[$i]]) ? $map[$parts[$i]] : $parts[$i];
|
261
|
-
$hash[$name] = $values[$i];
|
262
|
-
}
|
263
|
-
return $hash;
|
264
|
-
}
|
265
|
-
|
266
|
-
/**
|
267
|
-
* prepends table name to hash of field names to get around ambiguous fields when SQL builder
|
268
|
-
* has joins
|
269
|
-
*
|
270
|
-
* @param array $hash
|
271
|
-
* @return array $new
|
272
|
-
*/
|
273
|
-
private function prepend_table_name_to_fields($hash=array())
|
274
|
-
{
|
275
|
-
$new = array();
|
276
|
-
$table = $this->connection->quote_name($this->table);
|
277
|
-
|
278
|
-
foreach ($hash as $key => $value)
|
279
|
-
{
|
280
|
-
$k = $this->connection->quote_name($key);
|
281
|
-
$new[$table.'.'.$k] = $value;
|
282
|
-
}
|
283
|
-
|
284
|
-
return $new;
|
285
|
-
}
|
286
|
-
|
287
|
-
private function apply_where_conditions($args)
|
288
|
-
{
|
289
|
-
require_once 'Expressions.php';
|
290
|
-
$num_args = count($args);
|
291
|
-
|
292
|
-
if ($num_args == 1 && is_hash($args[0]))
|
293
|
-
{
|
294
|
-
$hash = is_null($this->joins) ? $args[0] : $this->prepend_table_name_to_fields($args[0]);
|
295
|
-
$e = new Expressions($this->connection,$hash);
|
296
|
-
$this->where = $e->to_s();
|
297
|
-
$this->where_values = array_flatten($e->values());
|
298
|
-
}
|
299
|
-
elseif ($num_args > 0)
|
300
|
-
{
|
301
|
-
// if the values has a nested array then we'll need to use Expressions to expand the bind marker for us
|
302
|
-
$values = array_slice($args,1);
|
303
|
-
|
304
|
-
foreach ($values as $name => &$value)
|
305
|
-
{
|
306
|
-
if (is_array($value))
|
307
|
-
{
|
308
|
-
$e = new Expressions($this->connection,$args[0]);
|
309
|
-
$e->bind_values($values);
|
310
|
-
$this->where = $e->to_s();
|
311
|
-
$this->where_values = array_flatten($e->values());
|
312
|
-
return;
|
313
|
-
}
|
314
|
-
}
|
315
|
-
|
316
|
-
// no nested array so nothing special to do
|
317
|
-
$this->where = $args[0];
|
318
|
-
$this->where_values = &$values;
|
319
|
-
}
|
320
|
-
}
|
321
|
-
|
322
|
-
private function build_delete()
|
323
|
-
{
|
324
|
-
$sql = "DELETE FROM $this->table";
|
325
|
-
|
326
|
-
if ($this->where)
|
327
|
-
$sql .= " WHERE $this->where";
|
328
|
-
|
329
|
-
return $sql;
|
330
|
-
}
|
331
|
-
|
332
|
-
private function build_insert()
|
333
|
-
{
|
334
|
-
require_once 'Expressions.php';
|
335
|
-
$keys = join(',',$this->quoted_key_names());
|
336
|
-
|
337
|
-
if ($this->sequence)
|
338
|
-
{
|
339
|
-
$sql =
|
340
|
-
"INSERT INTO $this->table($keys," . $this->connection->quote_name($this->sequence[0]) .
|
341
|
-
") VALUES(?," . $this->connection->next_sequence_value($this->sequence[1]) . ")";
|
342
|
-
}
|
343
|
-
else
|
344
|
-
$sql = "INSERT INTO $this->table($keys) VALUES(?)";
|
345
|
-
|
346
|
-
$e = new Expressions($this->connection,$sql,array_values($this->data));
|
347
|
-
return $e->to_s();
|
348
|
-
}
|
349
|
-
|
350
|
-
private function build_select()
|
351
|
-
{
|
352
|
-
$sql = "SELECT $this->select FROM $this->table";
|
353
|
-
|
354
|
-
if ($this->joins)
|
355
|
-
$sql .= ' ' . $this->joins;
|
356
|
-
|
357
|
-
if ($this->where)
|
358
|
-
$sql .= " WHERE $this->where";
|
359
|
-
|
360
|
-
if ($this->group)
|
361
|
-
$sql .= " GROUP BY $this->group";
|
362
|
-
|
363
|
-
if ($this->having)
|
364
|
-
$sql .= " HAVING $this->having";
|
365
|
-
|
366
|
-
if ($this->order)
|
367
|
-
$sql .= " ORDER BY $this->order";
|
368
|
-
|
369
|
-
if ($this->limit || $this->offset)
|
370
|
-
$sql = $this->connection->limit($sql,$this->offset,$this->limit);
|
371
|
-
|
372
|
-
return $sql;
|
373
|
-
}
|
374
|
-
|
375
|
-
private function build_update()
|
376
|
-
{
|
377
|
-
$fields = $this->quoted_key_names();
|
378
|
-
$sql = "UPDATE $this->table SET " . join('=?, ',$fields) . '=?';
|
379
|
-
|
380
|
-
if ($this->where)
|
381
|
-
$sql .= " WHERE $this->where";
|
382
|
-
|
383
|
-
return $sql;
|
384
|
-
}
|
385
|
-
|
386
|
-
private function quoted_key_names()
|
387
|
-
{
|
388
|
-
$keys = array();
|
389
|
-
|
390
|
-
foreach ($this->data as $key => $value)
|
391
|
-
$keys[] = $this->connection->quote_name($key);
|
392
|
-
|
393
|
-
return $keys;
|
394
|
-
}
|
395
|
-
}
|
396
|
-
?>
|
@@ -1,302 +0,0 @@
|
|
1
|
-
<?php
|
2
|
-
/**
|
3
|
-
* @package ActiveRecord
|
4
|
-
*/
|
5
|
-
namespace ActiveRecord;
|
6
|
-
use XmlWriter;
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Base class for Model serializers.
|
10
|
-
*
|
11
|
-
* All serializers support the following options:
|
12
|
-
*
|
13
|
-
* <ul>
|
14
|
-
* <li><b>only:</b> a string or array of attributes to be included.</li>
|
15
|
-
* <li><b>exclude:</b> a string or array of attributes to be excluded.</li>
|
16
|
-
* <li><b>methods:</b> a string or array of methods to invoke. The method's name will be used as a key for the final attributes array
|
17
|
-
* along with the method's returned value</li>
|
18
|
-
* <li><b>include:</b> a string or array of associated models to include in the final serialized product.</li>
|
19
|
-
* <li><b>skip_instruct:</b> set to true to skip the <?xml ...?> declaration.</li>
|
20
|
-
* </ul>
|
21
|
-
*
|
22
|
-
* Example usage:
|
23
|
-
*
|
24
|
-
* <code>
|
25
|
-
* # include the attributes id and name
|
26
|
-
* # run $model->encoded_description() and include its return value
|
27
|
-
* # include the comments association
|
28
|
-
* # include posts association with its own options (nested)
|
29
|
-
* $model->to_json(array(
|
30
|
-
* 'only' => array('id','name', 'encoded_description'),
|
31
|
-
* 'methods' => array('encoded_description'),
|
32
|
-
* 'include' => array('comments', 'posts' => array('only' => 'id'))
|
33
|
-
* ));
|
34
|
-
*
|
35
|
-
* # exclude the password field from being included
|
36
|
-
* $model->to_xml(array('exclude' => 'password')));
|
37
|
-
* </code>
|
38
|
-
*
|
39
|
-
* @package ActiveRecord
|
40
|
-
* @link http://www.phpactiverecord.org/guides/utilities#topic-serialization
|
41
|
-
*/
|
42
|
-
abstract class Serialization
|
43
|
-
{
|
44
|
-
protected $model;
|
45
|
-
protected $options;
|
46
|
-
protected $attributes;
|
47
|
-
|
48
|
-
/**
|
49
|
-
* Set this to true if the serializer needs to create a nested array keyed
|
50
|
-
* on the name of the included classes such as for xml serialization.
|
51
|
-
*
|
52
|
-
* Setting this to true will produce the following attributes array when
|
53
|
-
* the include option was used:
|
54
|
-
*
|
55
|
-
* <code>
|
56
|
-
* $user = array('id' => 1, 'name' => 'Tito',
|
57
|
-
* 'permissions' => array(
|
58
|
-
* 'permission' => array(
|
59
|
-
* array('id' => 100, 'name' => 'admin'),
|
60
|
-
* array('id' => 101, 'name' => 'normal')
|
61
|
-
* )
|
62
|
-
* )
|
63
|
-
* );
|
64
|
-
* </code>
|
65
|
-
*
|
66
|
-
* Setting to false will produce this:
|
67
|
-
*
|
68
|
-
* <code>
|
69
|
-
* $user = array('id' => 1, 'name' => 'Tito',
|
70
|
-
* 'permissions' => array(
|
71
|
-
* array('id' => 100, 'name' => 'admin'),
|
72
|
-
* array('id' => 101, 'name' => 'normal')
|
73
|
-
* )
|
74
|
-
* );
|
75
|
-
* </code>
|
76
|
-
*
|
77
|
-
* @var boolean
|
78
|
-
*/
|
79
|
-
protected $includes_with_class_name_element = false;
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Constructs a {@link Serialization} object.
|
83
|
-
*
|
84
|
-
* @param Model $model The model to serialize
|
85
|
-
* @param array &$options Options for serialization
|
86
|
-
* @return Serialization
|
87
|
-
*/
|
88
|
-
public function __construct(Model $model, &$options)
|
89
|
-
{
|
90
|
-
$this->model = $model;
|
91
|
-
$this->options = $options;
|
92
|
-
$this->attributes = $model->attributes();
|
93
|
-
$this->parse_options();
|
94
|
-
}
|
95
|
-
|
96
|
-
private function parse_options()
|
97
|
-
{
|
98
|
-
$this->check_only();
|
99
|
-
$this->check_except();
|
100
|
-
$this->check_methods();
|
101
|
-
$this->check_include();
|
102
|
-
}
|
103
|
-
|
104
|
-
private function check_only()
|
105
|
-
{
|
106
|
-
if (isset($this->options['only']))
|
107
|
-
{
|
108
|
-
$this->options_to_a('only');
|
109
|
-
|
110
|
-
$exclude = array_diff(array_keys($this->attributes),$this->options['only']);
|
111
|
-
$this->attributes = array_diff_key($this->attributes,array_flip($exclude));
|
112
|
-
}
|
113
|
-
}
|
114
|
-
|
115
|
-
private function check_except()
|
116
|
-
{
|
117
|
-
if (isset($this->options['except']) && !isset($this->options['only']))
|
118
|
-
{
|
119
|
-
$this->options_to_a('except');
|
120
|
-
$this->attributes = array_diff_key($this->attributes,array_flip($this->options['except']));
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
private function check_methods()
|
125
|
-
{
|
126
|
-
if (isset($this->options['methods']))
|
127
|
-
{
|
128
|
-
$this->options_to_a('methods');
|
129
|
-
|
130
|
-
foreach ($this->options['methods'] as $method)
|
131
|
-
{
|
132
|
-
if (method_exists($this->model, $method))
|
133
|
-
$this->attributes[$method] = $this->model->$method();
|
134
|
-
}
|
135
|
-
}
|
136
|
-
}
|
137
|
-
|
138
|
-
private function check_include()
|
139
|
-
{
|
140
|
-
if (isset($this->options['include']))
|
141
|
-
{
|
142
|
-
$this->options_to_a('include');
|
143
|
-
|
144
|
-
$serializer_class = get_class($this);
|
145
|
-
|
146
|
-
foreach ($this->options['include'] as $association => $options)
|
147
|
-
{
|
148
|
-
if (!is_array($options))
|
149
|
-
{
|
150
|
-
$association = $options;
|
151
|
-
$options = array();
|
152
|
-
}
|
153
|
-
|
154
|
-
try {
|
155
|
-
$assoc = $this->model->$association;
|
156
|
-
|
157
|
-
if (!is_array($assoc))
|
158
|
-
{
|
159
|
-
$serialized = new $serializer_class($assoc, $options);
|
160
|
-
$this->attributes[$association] = $serialized->to_a();;
|
161
|
-
}
|
162
|
-
else
|
163
|
-
{
|
164
|
-
$includes = array();
|
165
|
-
|
166
|
-
foreach ($assoc as $a)
|
167
|
-
{
|
168
|
-
$serialized = new $serializer_class($a, $options);
|
169
|
-
|
170
|
-
if ($this->includes_with_class_name_element)
|
171
|
-
$includes[strtolower(get_class($a))][] = $serialized->to_a();
|
172
|
-
else
|
173
|
-
$includes[] = $serialized->to_a();
|
174
|
-
}
|
175
|
-
|
176
|
-
$this->attributes[$association] = $includes;
|
177
|
-
}
|
178
|
-
|
179
|
-
} catch (UndefinedPropertyException $e) {
|
180
|
-
;//move along
|
181
|
-
}
|
182
|
-
}
|
183
|
-
}
|
184
|
-
}
|
185
|
-
|
186
|
-
final protected function options_to_a($key)
|
187
|
-
{
|
188
|
-
if (!is_array($this->options[$key]))
|
189
|
-
$this->options[$key] = array($this->options[$key]);
|
190
|
-
}
|
191
|
-
|
192
|
-
/**
|
193
|
-
* Returns the attributes array.
|
194
|
-
* @return array
|
195
|
-
*/
|
196
|
-
final public function to_a()
|
197
|
-
{
|
198
|
-
$date_format = Config::instance()->get_date_format();
|
199
|
-
|
200
|
-
foreach ($this->attributes as &$value)
|
201
|
-
{
|
202
|
-
if ($value instanceof \DateTime)
|
203
|
-
$value = $value->format($date_format);
|
204
|
-
}
|
205
|
-
return $this->attributes;
|
206
|
-
}
|
207
|
-
|
208
|
-
/**
|
209
|
-
* Returns the serialized object as a string.
|
210
|
-
* @see to_s
|
211
|
-
* @return string
|
212
|
-
*/
|
213
|
-
final public function __toString()
|
214
|
-
{
|
215
|
-
return $this->to_s();
|
216
|
-
}
|
217
|
-
|
218
|
-
/**
|
219
|
-
* Performs the serialization.
|
220
|
-
* @return string
|
221
|
-
*/
|
222
|
-
abstract public function to_s();
|
223
|
-
};
|
224
|
-
|
225
|
-
/**
|
226
|
-
* JSON serializer.
|
227
|
-
*
|
228
|
-
* @package ActiveRecord
|
229
|
-
*/
|
230
|
-
class JsonSerializer extends Serialization
|
231
|
-
{
|
232
|
-
public static $include_root = false;
|
233
|
-
|
234
|
-
public function to_s()
|
235
|
-
{
|
236
|
-
return json_encode(self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a());
|
237
|
-
}
|
238
|
-
}
|
239
|
-
|
240
|
-
/**
|
241
|
-
* XML serializer.
|
242
|
-
*
|
243
|
-
* @package ActiveRecord
|
244
|
-
*/
|
245
|
-
class XmlSerializer extends Serialization
|
246
|
-
{
|
247
|
-
private $writer;
|
248
|
-
|
249
|
-
public function __construct(Model $model, &$options)
|
250
|
-
{
|
251
|
-
$this->includes_with_class_name_element = true;
|
252
|
-
parent::__construct($model,$options);
|
253
|
-
}
|
254
|
-
|
255
|
-
public function to_s()
|
256
|
-
{
|
257
|
-
return $this->xml_encode();
|
258
|
-
}
|
259
|
-
|
260
|
-
private function xml_encode()
|
261
|
-
{
|
262
|
-
$this->writer = new XmlWriter();
|
263
|
-
$this->writer->openMemory();
|
264
|
-
$this->writer->startDocument('1.0', 'UTF-8');
|
265
|
-
$this->writer->startElement(strtolower(denamespace(($this->model))));
|
266
|
-
$this->write($this->to_a());
|
267
|
-
$this->writer->endElement();
|
268
|
-
$this->writer->endDocument();
|
269
|
-
$xml = $this->writer->outputMemory(true);
|
270
|
-
|
271
|
-
if (@$this->options['skip_instruct'] == true)
|
272
|
-
$xml = preg_replace('/<\?xml version.*?\?>/','',$xml);
|
273
|
-
|
274
|
-
return $xml;
|
275
|
-
}
|
276
|
-
|
277
|
-
private function write($data, $tag=null)
|
278
|
-
{
|
279
|
-
foreach ($data as $attr => $value)
|
280
|
-
{
|
281
|
-
if ($tag != null)
|
282
|
-
$attr = $tag;
|
283
|
-
|
284
|
-
if (is_array($value) || is_object($value))
|
285
|
-
{
|
286
|
-
if (!is_int(key($value)))
|
287
|
-
{
|
288
|
-
$this->writer->startElement($attr);
|
289
|
-
$this->write($value);
|
290
|
-
$this->writer->endElement();
|
291
|
-
}
|
292
|
-
else
|
293
|
-
$this->write($value, $attr);
|
294
|
-
|
295
|
-
continue;
|
296
|
-
}
|
297
|
-
|
298
|
-
$this->writer->writeElement($attr, $value);
|
299
|
-
}
|
300
|
-
}
|
301
|
-
}
|
302
|
-
?>
|