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