| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Abstracts; |
| 4 |
|
| 5 |
/** |
| 6 |
* @method array get_virtual_attributes( object $row ) Child classes may implement this to provide virtual attributes |
| 7 |
*/ |
| 8 |
abstract class Model { |
| 9 |
protected $table; |
| 10 |
protected $fillable = array(); |
| 11 |
|
| 12 |
/** |
| 13 |
* @var Booktics_Database |
| 14 |
*/ |
| 15 |
protected $db; |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor for BookticsModel |
| 19 |
* |
| 20 |
* @param Booktics_Database $db |
| 21 |
*/ |
| 22 |
public function __construct( Booktics_Database $db ) { |
| 23 |
$this->db = $db; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get a query builder instance for the model's table |
| 28 |
* |
| 29 |
* @return mixed |
| 30 |
*/ |
| 31 |
public function query() { |
| 32 |
return $this->db->table( $this->table ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Apply conditions to a query builder instance |
| 37 |
* |
| 38 |
* @param mixed $query |
| 39 |
* @param array $conditions |
| 40 |
* |
| 41 |
* @return mixed |
| 42 |
*/ |
| 43 |
protected function apply_conditions( $query, array $conditions = array() ) { |
| 44 |
foreach ( $conditions as $field => $value ) { |
| 45 |
if ( is_array( $value ) ) { |
| 46 |
if ( strtolower( $value[0] ) === 'in' && is_array( $value[1] ) ) { |
| 47 |
$query->where_in( $field, $value[1] ); |
| 48 |
} else { |
| 49 |
$query->where( $field, $value[0] ?? '=', $value[1] ); |
| 50 |
} |
| 51 |
} else { |
| 52 |
$query->where( $field, '=', $value ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $query; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get total number of records based on conditions |
| 61 |
* |
| 62 |
* @param array $conditions |
| 63 |
* |
| 64 |
* @return int |
| 65 |
*/ |
| 66 |
public function count( array $conditions = array() ) { |
| 67 |
$query = $this->apply_conditions( $this->db->table( $this->table ), $conditions ); |
| 68 |
|
| 69 |
return $query->count(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get sum of a field based on conditions |
| 74 |
* |
| 75 |
* @param string $field |
| 76 |
* @param array $conditions |
| 77 |
* |
| 78 |
* @return float |
| 79 |
*/ |
| 80 |
public function sum( string $field, array $conditions = array() ) { |
| 81 |
$query = $this->apply_conditions( $this->db->table( $this->table ), $conditions ); |
| 82 |
|
| 83 |
return (float) $query->sum( $field ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Create a new record in the database |
| 88 |
* |
| 89 |
* @param array $data |
| 90 |
* |
| 91 |
* @return mixed|false |
| 92 |
*/ |
| 93 |
public function create( array $data ) { |
| 94 |
$filtered = array_intersect_key( $data, array_flip( $this->fillable ) ); |
| 95 |
$result = $this->db->table( $this->table )->insert( $filtered ); |
| 96 |
|
| 97 |
return $result > 0 ? $this->find( array( 'id' => $result ) ) : false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Update an existing record in the database |
| 102 |
* |
| 103 |
* @param int $id |
| 104 |
* @param array $data |
| 105 |
* |
| 106 |
* @return mixed |
| 107 |
*/ |
| 108 |
public function update( int $id, array $data ) { |
| 109 |
$filtered = array_intersect_key( $data, array_flip( $this->fillable ) ); |
| 110 |
|
| 111 |
return $this->db->table( $this->table )->where( 'id', '=', $id )->update( $filtered ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Delete a record from the database |
| 116 |
* |
| 117 |
* @param int $id |
| 118 |
* |
| 119 |
* @return mixed |
| 120 |
*/ |
| 121 |
public function delete( int $id ) { |
| 122 |
return $this->db->table( $this->table )->where( 'id', '=', $id )->delete(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get multiple records from the database with pagination |
| 127 |
* |
| 128 |
* @param array $conditions |
| 129 |
* @param int $page Current page number |
| 130 |
* @param int $per_page Items per page |
| 131 |
* @param array $order_by |
| 132 |
* |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
public function get( array $conditions = array(), int $page = 1, int $per_page = 10, $order_by = array() ) { |
| 136 |
$query = $this->apply_conditions( $this->db->table( $this->table ), $conditions ); |
| 137 |
$total = $query->count(); |
| 138 |
$offset = ( $page - 1 ) * $per_page; |
| 139 |
$data = $query->limit( $per_page )->offset( $offset ) |
| 140 |
->order_by( $order_by )->get(); |
| 141 |
foreach ( $data as &$row ) { |
| 142 |
$row = $this->with_virtual_attributes( $row ); |
| 143 |
} |
| 144 |
|
| 145 |
return array( |
| 146 |
'items' => $data, |
| 147 |
'current_page' => $page, |
| 148 |
'per_page' => $per_page, |
| 149 |
'total' => $total, |
| 150 |
'total_pages' => ceil( $total / $per_page ), |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Find a single record from the database |
| 156 |
* |
| 157 |
* @param array $conditions |
| 158 |
* |
| 159 |
* @return mixed |
| 160 |
*/ |
| 161 |
public function find( array $conditions = array() ) { |
| 162 |
$query = $this->apply_conditions( $this->db->table( $this->table ), $conditions ); |
| 163 |
$row = $query->first(); |
| 164 |
if ( is_wp_error( $row ) || $row === null ) { |
| 165 |
return $row; |
| 166 |
} |
| 167 |
|
| 168 |
return $this->with_virtual_attributes( $row ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the table name for the model |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
public function get_table_name() { |
| 177 |
return $this->table; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get the virtual attributes for the model |
| 182 |
* |
| 183 |
* @param object $row |
| 184 |
* |
| 185 |
* @return object |
| 186 |
*/ |
| 187 |
public function with_virtual_attributes( $row ) { |
| 188 |
if ( method_exists( $this, 'get_virtual_attributes' ) && $row ) { |
| 189 |
foreach ( $this->get_virtual_attributes( $row ) as $key => $value ) { |
| 190 |
$row->$key = $value; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $row; |
| 195 |
} |
| 196 |
} |
| 197 |
|