-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.php
879 lines (762 loc) · 18.2 KB
/
crud.php
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
<?php
/**
* Part of the Crud bundle for Laravel.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file. It is also available at
* the following URL: http://www.opensource.org/licenses/BSD-3-Clause
*
* @package Sentry
* @version 1.0
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011 - 2012, Cartalyst LLC
* @link http://cartalyst.com
*/
namespace Crud;
use ArrayAccess;
use Closure;
use DB;
use Event;
use Exception;
use Str;
use Validator;
/**
* Crud model class.
*
* @author Daniel Petrie, Ben Corlett
*/
class Crud implements ArrayAccess
{
/**
* The primary key for the model on the database table.
*
* @var string
*/
protected static $_key = 'id';
/**
* The name of the table associated with the model.
* If left null, the table name will become the the plural of
* the class name: user => users
*
* @var string
*/
protected static $_table = null;
/**
* The name of the database connection that should be used for the model.
*
* @var string
*/
protected static $_connection = null;
/**
* The name of the sequence associated with the model.
*
* @var string
*/
protected static $_sequence = null;
/**
* Indicates if the model has update and creation timestamps.
*
* @var bool
*/
protected static $_timestamps = false;
/**
* The timestamp format for the model. Only available if the
* timestamps are set to true.
*
* SQL timestamps are converted to the correct format based
* on the connection used in the model (which are defined in
* each Laravel driver's Grammar class).
*
* Possible values:
* - 'sql'
* - 'unix'
*
* If the persist format is left NULL then 'sql' is used.
* If the instance format is left NULL then the persiste
* format is used.
*
* @var string
*/
protected static $_timestamp_persist_format = 'sql';
protected static $_timestamp_instance_format = 'unix';
/**
* Indicates if the model should use events
*
* @var bool
*/
protected static $_events = true;
/**
* Validation rules for model attributes.
*
* @var array
*/
protected static $_rules = array();
/**
* Indicates if the model is new or not (insert vs update).
*
* @var bool
*/
protected $_is_new;
/**
* @var validation object
*/
protected $_validation;
/*
|--------------------------------------------------------------------------
| Object Usage
|--------------------------------------------------------------------------
*/
/**
* Create a new Crud model instance.
*
* @param array $attributes
* @param bool $is_new
* @return void
*/
public function __construct($attributes = array(), $is_new = null)
{
// Hydrate our model
$this->fill((array) $attributes);
// Set the $is_new flag
$this->_is_new = ($is_new === null) ? ( ! array_key_exists(static::key(), $attributes)) : (bool) $is_new;
}
/**
* Save the model instance to the database.
*
* @return int
*/
public function save($events = array('before', 'after'))
{
// first check if we want timestamps as this will append to attributes
if (static::$_timestamps === true)
{
$this->timestamp();
}
// now we grab the attributes
$attributes = $this->attributes();
// run - if rules are set
if ( ! empty(static::$_rules))
{
$validated = $this->run_validation($attributes, static::$_rules);
if ( ! $validated)
{
return false;
}
}
// prep attribute values after validation is done
$attributes = $this->prep_attributes($attributes);
// If the model is not new, we only need to update it in the database, and the update
// will be considered successful if there is one affected row returned from the
// fluent query instance. We'll set the where condition automatically.
if ( ! $this->is_new())
{
// make sure a key is set then grab and remove it from the attributes array
if ( ! isset($attributes[static::key()]) or empty($attributes[static::key()]))
{
// the key is not set or empty, throw an exception
throw new Exception('A primary key is required to update.');
}
$key = $attributes[static::key()];
unset($attributes[static::key()]);
$query = $this->query()->where(static::table().'.'.static::key(), '=', $key);
if (in_array('before', $events))
{
list($query, $attributes) = $this->before_update($query, $attributes);
}
$result = $query->update($attributes);
if (in_array('after', $events))
{
$result = $this->after_update($result);
}
if (static::$_events)
{
// fire update event
Event::fire(static::event().'.update', array($this));
}
}
// If the model is new, we will insert the record and retrieve the last
// insert ID that is associated with the model. If the ID returned is numeric
// then we can consider the insert successful.
else
{
$query = $this->query();
if (in_array('before', $events))
{
list($query, $attributes) = $this->before_insert($query, $attributes);
}
$key = $this->query()->insert_get_id($attributes, static::$_sequence);
if (in_array('after', $events))
{
$key = $this->after_insert($key);
}
// Workaound for PDO connections not returning
// the key upon insert.
if (isset($this->{static::key()}) and $key === 0)
{
$key = $this->{static::key()};
$this->is_new(false);
}
else
{
// If we didn't already have a primary
// key, assign what is returned from
// the database insert
if ( ! isset($this->{static::key()}))
{
$this->{static::key()} = $key;
}
$this->is_new( ! (bool) $key);
}
$this->fill($attributes);
if (static::$_events)
{
// fire create event
Event::fire(static::event().'.create', array($this));
}
}
return $key;
}
/**
* Delete a model from the datatabase
*
* @return int
*/
public function delete($events = array('before', 'after'))
{
// make sure a key is set then grab and remove it from the attributes array
if ( ! isset($this->{static::key()}) or empty($this->{static::key()}))
{
// the key is not set or empty, throw an exception
throw new Exception('A primary key is required to delete.');
}
$query = $this->query()->where(static::table().'.'.static::key(), '=', $this->{static::key()});
if (in_array('before', $events))
{
$query = $this->before_delete($query);
}
$result = $query->delete();
if (in_array('after', $events))
{
$result = $this->after_delete($result);
}
if (static::$_events)
{
// fire delete event
Event::fire(static::event().'.delete', array($this));
}
return $result;
}
/**
* Hydrate the model with an array of attributes.
*
* @param array $attributes
* @return Crud
*/
public function fill($attributes = array())
{
foreach ($attributes as $key => $value)
{
$this->{$key} = $value;
}
return $this;
}
/**
* Get all the attributes of the model.
*
* @return array
*/
public function attributes()
{
return get_object_public_vars($this);
}
/**
* Returns the a validation object for the model.
*
* @return Validation
*/
public function validation()
{
return $this->_validation;
}
/**
* Dynamically set the value of an attribute.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this->{$key} = $value;
}
/**
* Dynamically check if an attribute is set.
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
return isset($this->{$key});
}
/**
* Dynamically unset an attribute.
*
* @param string $key
* @return void
*/
public function __unset($key)
{
unset($this->{$key});
}
/**
* Set or Determine if the model is new or not
*
* @param bool $is_new
* @return bool|Crud
*/
public function is_new($is_new = null)
{
if ($is_new === null)
{
return $this->_is_new;
}
$this->_is_new = (bool) $is_new;
return $this;
}
/**
* Set the update and creation timestamps on the model.
*
* @return void
*/
protected function timestamp()
{
// Get the timestamp
$timestamp = time();
// Default the timestamp format
$format = static::$_timestamp_persist_format ?: 'sql';
// Using SQL timestamps?
if ($format === 'sql')
{
$timestamp = date(static::connection()->grammar()->grammar->datetime, $timestamp);
}
$this->updated_at = $timestamp;
if ($this->is_new())
{
$this->created_at = $this->updated_at;
}
}
/**
* Run validation
*
* @param array $attributes
* @param array $rultes
* @return bool
*/
protected function run_validation($attributes, $rules)
{
list($attributes, $rules) = $this->before_validation($attributes, $rules);
$this->_validation = Validator::make($attributes, $rules);
$result = $this->after_validation($this->_validation->fails());
return ( ! (bool) $result);
}
/**
* Gets called before the validation is ran.
*
* @param array $data
* @param array $rults
* @return array
*/
protected function before_validation($data, $rules)
{
return array($data, $rules);
}
/**
* Called right after the validation is ran.
*
* @param bool $result
* @return bool
*/
protected function after_validation($result)
{
return $result;
}
/**
* Called right after validation before inserting/updating to the database
*
* @param array $attributes
* @return array
*/
protected function prep_attributes($attributes)
{
return $attributes;
}
/**
* Gets called before insert() is executed to modify the query
* Must return an array of the query object and columns array($query, $columns)
*
* @param Query $query
* @param array $columns
* @return array
*/
protected function before_insert($query, $columns)
{
return array($query, $columns);
}
/**
* Gets call after the insert() query is exectuted to modify the result
* Must return a proper result
*
* @param array $result
* @return array $result
*/
protected function after_insert($result)
{
return $result;
}
/**
* Gets called before update() is executed to modify the query
* Must return an array of the query object and columns array($query, $columns)
*
* @param Query $query
* @param array $columns
* @return array
*/
protected function before_update($query, $columns)
{
return array($query, $columns);
}
/**
* Gets call after the update() query is exectuted to modify the result
* Must return a proper result
*
* @return array
*/
protected function after_update($result)
{
return $result;
}
/**
* Gets called before delete() is executed to modify the query
* Must return an array of the query object and columns array($query, $columns)
*
* @param Query $query
* @return Query $query
*/
protected function before_delete($query)
{
return $query;
}
/**
* Gets call after the delete() query is exectuted to modify the result
* Must return a proper result
*
* @param int $result
* @return int $result
*/
protected function after_delete($result)
{
return $result;
}
/**
* Gets called before find() is executed to modify the query
* Must return an array of the query object and columns array($query, $columns)
*
* @param Query $query
* @param array $columns
* @return array
*/
protected function before_find($query, $columns)
{
return array($query, $columns);
}
/**
* Gets call after the find() query is exectuted to modify the result
* Must return a proper result
*
* @param Query $query
* @param array $columns
* @return array
*/
protected function after_find($result)
{
return $result;
}
/*
|--------------------------------------------------------------------------
| ArrayAccess Implementation
|--------------------------------------------------------------------------
*/
/**
* Sets the value of the given offset (class property).
*
* @param string $key
* @param string $value
* @return void
*/
public function offsetSet($key, $value)
{
$this->{$key} = $value;
}
/**
* Checks if the given offset (class property) exists.
*
* @param string $key
* @return bool
*/
public function offsetExists($key)
{
return isset($this->{$key});
}
/**
* Unsets the given offset (class property).
*
* @param string $key
* @return void
*/
public function offsetUnset($key)
{
unset($this->{$key});
}
/**
* Gets the value of the given offset (class property).
*
* @param string $key
* @return mixed
*/
public function offsetGet($key)
{
if (isset($this->{$key}))
{
return $this->{$key};
}
throw new Exception('Property "'.$key.'" not found for '.get_called_class().'.');
}
/*
|--------------------------------------------------------------------------
| Static Usage
|--------------------------------------------------------------------------
*/
/**
* Get the key of the table
*
* @return string
*/
public static function key()
{
return static::$_key;
}
/**
* Get the name of the table associated with the model.
*
* @return string
*/
public static function table()
{
return static::$_table ?: Str::lower(Str::plural(class_basename(new static)));
}
/**
* Get the event name associated with the model
*
* @return string
*/
public static function event()
{
return Str::lower(class_basename(new static));
}
/**
* Get the database connection.
*
* @return Laravel\Database\Connection
*/
public static function connection()
{
return DB::connection(static::$_connection);
}
/**
* Find a model by either it's primary key
* or a condition that modifies the query object.
*
* @param string $condition
* @param array $columns
* @return Crud
*/
public static function find($condition = 'first', $columns = array('*'), $events = array('before', 'after'))
{
$model = new static;
$query = $model->query();
// User has a closure of the query
// as the condition
if ($condition instanceof Closure)
{
$query = $condition($query);
}
elseif ($condition == 'first')
{
$query->order_by(static::table().'.'.static::key(), 'asc');
}
// After last result
elseif ($condition == 'last')
{
$query->order_by(static::table().'.'.static::key(), 'desc');
}
// Providing either an int or string for
// the primary key
else
{
$query = $query->where(static::table().'.'.static::key(), '=', $condition);
}
if (in_array('before', $events))
{
list($query, $columns) = $model->before_find($query, $columns);
}
$result = $query->first($columns);
// Flag for convert timestamps
$convert_timestamps = (static::$_timestamps == true and ! is_null(static::$_timestamp_instance_format) and static::$_timestamp_instance_format !== static::$_timestamp_persist_format) ? static::$_timestamp_instance_format : false;
if ($convert_timestamps)
{
if (isset($result->created_at))
{
if ($convert_timestamps === 'unix')
{
$result->created_at = strtotime($result->created_at);
}
}
if (isset($result->updated_at))
{
if ($convert_timestamps === 'unix')
{
$result->updated_at = strtotime($result->updated_at);
}
}
}
if (in_array('after', $events))
{
$result = $model->after_find($result);
}
if (count($result) > 0)
{
return $model->is_new(false)
->fill($result);
}
return null;
}
/**
* Get all of the models in the database.
*
* @param Closure $condition
* @param String|Array columns to select
* @return array
*/
public static function all($condition = null, $columns = '*', $events = array('before', 'after'))
{
$query = with(new static)->query();
if ($condition instanceof Closure)
{
$query = $condition($query, $columns);
}
if (in_array('before', $events))
{
list($query, $columns) = static::before_all($query, $columns);
}
$results = $query->get($columns);
if (in_array('after', $events))
{
$results = static::after_all($results);
}
$models = array();
// Flag for convert timestamps
$convert_timestamps = (static::$_timestamps == true and ! is_null(static::$_timestamp_instance_format) and static::$_timestamp_instance_format !== static::$_timestamp_persist_format) ? static::$_timestamp_instance_format : false;
foreach ($results as $result)
{
if ($convert_timestamps)
{
if (isset($result->created_at))
{
if ($convert_timestamps === 'unix')
{
$result->created_at = strtotime($result->created_at);
}
}
if (isset($result->updated_at))
{
if ($convert_timestamps === 'unix')
{
$result->updated_at = strtotime($result->updated_at);
}
}
}
$models[] = new static($result);
}
return $models;
}
/**
* Returns the number of records in the table.
*
* @param string $column
* @param Closure $condition
* @return int
*/
public static function count($column = null, $condition = null)
{
$query = static::query();
if ($condition instanceof Closure)
{
$query = $condition($query);
}
return $query->count($column ?: static::key());
}
/**
* Returns the number of distinct records in the table.
*
* @param string $column
* @param Closure $condition
* @return int
*/
public static function count_distinct($column = null, $condition = null)
{
$query = static::query();
if ($condition instanceof Closure)
{
$query = $condition($query);
}
return $query->distinct()->count($column ?: static::key());
}
/**
* Get a new fluent query builder instance for the model.
*
* @return Query
*/
public static function query()
{
return static::connection()->table(static::table());
}
/**
* Gets called before all() is executed to modify the query
* Must return an array of the query object and columns array($query, $columns)
*
* @param Query $query
* @param array $columns
* @return array
*/
protected static function before_all($query, $columns)
{
return array($query, $columns);
}
/**
* Gets called after the all() query is exectuted to modify the result
* Must return a proper result
*
* @param array $results
* @return array $results
*/
protected static function after_all($results)
{
return $results;
}
}