| @@ -1,11 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Booktics\Abstracts; |
| 4 | 4 | |
| 5 | +/** | |
| 6 | + * @method array get_virtual_attributes( object $row ) Child classes may implement this to provide virtual attributes | |
| 7 | + */ | |
| 5 | 8 | abstract class Model { |
| 6 | 9 | protected $table; |
| 7 | - protected $fillable = []; | |
| 10 | + protected $fillable = array(); | |
| 8 | 11 | |
| 9 | 12 | /** |
| 10 | 13 | * @var Booktics_Database |
| 11 | 14 | */ |
| @@ -11,24 +14,59 @@ | ||
| 11 | 14 | */ |
| 12 | 15 | protected $db; |
| 13 | 16 | |
| 14 | 17 | /** |
| 15 | - * Get total number of records based on conditions | |
| 18 | + * Constructor for BookticsModel | |
| 16 | 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 | |
| 17 | 39 | * @param array $conditions |
| 18 | 40 | * |
| 19 | - * @return int | |
| 41 | + * @return mixed | |
| 20 | 42 | */ |
| 21 | - public function count( array $conditions = [] ) { | |
| 22 | - $query = $this->db->table( $this->table ); | |
| 43 | + protected function apply_conditions( $query, array $conditions = array() ) { | |
| 23 | 44 | foreach ( $conditions as $field => $value ) { |
| 24 | 45 | if ( is_array( $value ) ) { |
| 25 | - $query->where( $field, $value[0] ?? '=', $value[1] ); | |
| 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 | + } | |
| 26 | 51 | } else { |
| 27 | 52 | $query->where( $field, '=', $value ); |
| 28 | 53 | } |
| 29 | 54 | } |
| 30 | 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 | + | |
| 31 | 69 | return $query->count(); |
| 32 | 70 | } |
| 33 | 71 | |
| 34 | 72 | /** |
| @@ -38,31 +76,15 @@ | ||
| 38 | 76 | * @param array $conditions |
| 39 | 77 | * |
| 40 | 78 | * @return float |
| 41 | 79 | */ |
| 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 | - } | |
| 80 | + public function sum( string $field, array $conditions = array() ) { | |
| 81 | + $query = $this->apply_conditions( $this->db->table( $this->table ), $conditions ); | |
| 51 | 82 | |
| 52 | 83 | return (float) $query->sum( $field ); |
| 53 | 84 | } |
| 54 | 85 | |
| 55 | 86 | /** |
| 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 | 87 | * Create a new record in the database |
| 66 | 88 | * |
| 67 | 89 | * @param array $data |
| 68 | 90 | * |
| @@ -69,16 +91,11 @@ | ||
| 69 | 91 | * @return mixed|false |
| 70 | 92 | */ |
| 71 | 93 | public function create( array $data ) { |
| 72 | 94 | $filtered = array_intersect_key( $data, array_flip( $this->fillable ) ); |
| 95 | + $result = $this->db->table( $this->table )->insert( $filtered ); | |
| 73 | 96 | |
| 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; | |
| 97 | + return $result > 0 ? $this->find( array( 'id' => $result ) ) : false; | |
| 81 | 98 | } |
| 82 | 99 | |
| 83 | 100 | /** |
| 84 | 101 | * Update an existing record in the database |
| @@ -110,34 +127,29 @@ | ||
| 110 | 127 | * |
| 111 | 128 | * @param array $conditions |
| 112 | 129 | * @param int $page Current page number |
| 113 | 130 | * @param int $per_page Items per page |
| 131 | + * @param array $order_by | |
| 114 | 132 | * |
| 115 | 133 | * @return array |
| 116 | 134 | */ |
| 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 | - | |
| 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 ); | |
| 127 | 137 | $total = $query->count(); |
| 128 | 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 | + } | |
| 129 | 144 | |
| 130 | - $data = $query->limit( $per_page )->offset( $offset ) | |
| 131 | - ->order_by( $order_by )->get(); | |
| 132 | - | |
| 133 | - return [ | |
| 145 | + return array( | |
| 134 | 146 | 'items' => $data, |
| 135 | 147 | 'current_page' => $page, |
| 136 | 148 | 'per_page' => $per_page, |
| 137 | 149 | 'total' => $total, |
| 138 | - 'total_pages' => ceil( $total / $per_page ) | |
| 139 | - ]; | |
| 150 | + 'total_pages' => ceil( $total / $per_page ), | |
| 151 | + ); | |
| 140 | 152 | } |
| 141 | 153 | |
| 142 | 154 | /** |
| 143 | 155 | * Find a single record from the database |
| @@ -145,21 +157,40 @@ | ||
| 145 | 157 | * @param array $conditions |
| 146 | 158 | * |
| 147 | 159 | * @return mixed |
| 148 | 160 | */ |
| 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 | - } | |
| 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; | |
| 157 | 166 | } |
| 158 | 167 | |
| 159 | - return $query->first(); | |
| 168 | + return $this->with_virtual_attributes( $row ); | |
| 160 | 169 | } |
| 161 | 170 | |
| 171 | + /** | |
| 172 | + * Get the table name for the model | |
| 173 | + * | |
| 174 | + * @return string | |
| 175 | + */ | |
| 162 | 176 | public function get_table_name() { |
| 163 | 177 | return $this->table; |
| 164 | 178 | } |
| 165 | -} | |
| 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 | +} | |