-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMModel.php
94 lines (75 loc) · 2.03 KB
/
KMModel.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
<?php
/**
* @author kofimokome
*/
if ( ! class_exists( 'KMModel' ) ) {
#[AllowDynamicProperties]
class KMModel {
protected $timestamps = false;
protected $table_name = '';
protected $soft_delete = false;
public $id = 0;
private $context;
function __construct( string $context = '' ) {
if ( $context == '' ) {
$t = debug_backtrace();
$context = $t[0]['file'];
}
$this->context = $context;
// do something here
}
/**
* @author kofmokome
* Checks if a model has timestamps enabled
*/
public function hasTimeStamps() {
return $this->timestamps;
}
/**
* @author kofmokome
* Checks if a model has soft delete enabled
*/
public function isSoftDelete() {
return $this->soft_delete;
}
public function __call( $method, $arguments ) {
$builder = new KMBuilder( $this->getTableName(), $this, $this->context );
return $builder->$method( ...$arguments );
}
public static function __callStatic( $method, $arguments ) {
$called = get_called_class();
$t = debug_backtrace();
$context = $t[0]['file'];
$model = new $called( $context );
$query = new KMBuilder( $model->getTableName(), $model, $context );
return $query->$method( ...$arguments );
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function getTableName(): string {
global $wpdb;
$env = ( new KMEnv( $this->context ) )->getEnv();
$table_name = $this->table_name;
if ( $table_name == '' ) {
$model = get_called_class();
$table_name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $model ) );
if ( sizeof( $names = explode( '\\', $table_name ) ) > 0 ) {
$table_name = $names[1] ?? $names[0];
}
$table_name = ltrim( $table_name, '_' );
$table_name = Plural( $table_name );
$table_name = $wpdb->prefix . trim( $env['TABLE_PREFIX'] ) . $table_name;
}
return $table_name;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function setTableName( $table_name ) {
$this->table_name = $table_name;
}
}
}