Skip to content

Commit fc8a1fc

Browse files
committed
adding exception invalid args for all required params, set tu require_once
1 parent 56cbf20 commit fc8a1fc

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

application/models/Model_app.php

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
33

4-
require APPPATH.'/traits/BaseQueryBuilder.php';
4+
require_once APPPATH . '/traits/BaseQueryBuilder.php';
5+
require_once APPPATH . '/traits/ExceptionThrow.php';
56

67
class Model_app extends CI_Model {
78

8-
use BaseQueryBuilder;
9+
use BaseQueryBuilder, ExceptionThrow;
910
/**
1011
* Table's name inside databsae for each model
1112
* @var [type]
@@ -50,6 +51,8 @@ class Model_app extends CI_Model {
5051
*/
5152
function insert ( $arrayData, $table = false ) {
5253

54+
if ( empty( $arrayData ) ) $this->InvalidArgExceptionThrow( 1 );
55+
5356
if ( !$table )
5457
$this->db->insert( $this->table, $arrayData );
5558
else{
@@ -91,6 +94,8 @@ function insert ( $arrayData, $table = false ) {
9194
*/
9295
function insert_batch ( $arrayData, $table = false ) {
9396

97+
if ( empty( $arrayData ) ) $this->InvalidArgExceptionThrow( 1 );
98+
9499
if ( !$table )
95100
$insert = $this->db->insert_batch( $this->table, $arrayData );
96101
else
@@ -126,6 +131,8 @@ function insert_batch ( $arrayData, $table = false ) {
126131
*/
127132
public function delete ( $where, $table = false ) {
128133

134+
if ( empty( $where ) ) $this->InvalidArgExceptionThrow( 1 );
135+
129136
if ( !$table )
130137
$this->db->delete( $this->table );
131138
else{
@@ -210,6 +217,8 @@ function truncate ( $table = false ) {
210217
*/
211218
function get_last_data( $fieldToOrder, $where = false, $fieldToSelect = false, $table = false ) {
212219

220+
if ( empty( $fieldToOrder ) ) $this->InvalidArgExceptionThrow( 1 );
221+
213222
if ( $fieldToSelect ) {
214223
$column = $fieldToSelect;
215224
}
@@ -588,7 +597,10 @@ private function complexQueries ( $where = false, $fields = false, $table = fals
588597
*/
589598
function update ( $columnToUpdate, $usingCondition, $tableToUpdate = false )
590599
{
591-
600+
601+
if ( empty( $columnToUpdate ) && empty( $usingCondition ) ) $this->InvalidArgExceptionThrow( 2 );
602+
else if ( empty( $columnToUpdate ) || empty( $usingCondition ) ) $this->InvalidArgExceptionThrow( 1 );
603+
592604
$this->db->where( $usingCondition );
593605

594606
if ( !$tableToUpdate )
@@ -636,6 +648,9 @@ function update ( $columnToUpdate, $usingCondition, $tableToUpdate = false )
636648
*/
637649
function update_batch ( $columnToUpdate, $usingCondition, $tableToUpdate = false ) {
638650

651+
if ( empty( $columnToUpdate ) && empty( $usingCondition ) ) $this->InvalidArgExceptionThrow( 2 );
652+
else if ( empty( $columnToUpdate ) || empty( $usingCondition ) ) $this->InvalidArgExceptionThrow( 1 );
653+
639654
if ( !$tableToUpdate )
640655
$update = $this->db->update_batch( $this->table, $columnToUpdate, $usingCondition );
641656
else {
@@ -670,6 +685,8 @@ function update_batch ( $columnToUpdate, $usingCondition, $tableToUpdate = fals
670685
*/
671686
function replace ( $data, $table = false ) {
672687

688+
if ( empty( $data ) ) $this->InvalidArgExceptionThrow( 1 );
689+
673690
if ( !$table )
674691
$query = $this->db->replace( $this->table, $data);
675692
else {
@@ -740,6 +757,8 @@ private function joinTable ( $join ) {
740757
*/
741758
function max ( $fields, $where = false, $table = false, $join = false ) {
742759

760+
if ( empty( $fields ) ) $this->InvalidArgExceptionThrow( 1 );
761+
743762
$this->db->select_max( $fields );
744763

745764
if ( $where )
@@ -775,6 +794,8 @@ function max ( $fields, $where = false, $table = false, $join = false ) {
775794
*/
776795
function min ( $fields, $where = false, $table = false, $join = false ) {
777796

797+
if ( empty( $fields ) ) $this->InvalidArgExceptionThrow( 1 );
798+
778799
$this->db->select_min( $fields );
779800

780801
if ( $where )
@@ -810,6 +831,8 @@ function min ( $fields, $where = false, $table = false, $join = false ) {
810831
*/
811832
function avg ( $fields, $where = false, $table = false, $join = false ) {
812833

834+
if ( empty( $fields ) ) $this->InvalidArgExceptionThrow( 1 );
835+
813836
$this->db->select_avg( $fields );
814837

815838
if ( $where )
@@ -845,6 +868,8 @@ function avg ( $fields, $where = false, $table = false, $join = false ) {
845868
*/
846869
function sum ( $fields, $where = false, $table = false, $join = false ) {
847870

871+
if ( empty( $fields ) ) $this->InvalidArgExceptionThrow( 1 );
872+
848873
$this->db->select_sum( $fields );
849874

850875
if ( $where )
@@ -909,7 +934,9 @@ function count ( $where = false, $table = false, $join = false ) {
909934
* The join implementation are same with @get_all_rows && @get_specified_row() method
910935
*/
911936
function where ( $arrValue, $table = false, $join = false ) {
912-
937+
938+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
939+
913940
if ( $join ) $this->joinTable( $join );
914941

915942
$this->db->where( $arrValue );
@@ -938,6 +965,8 @@ function where ( $arrValue, $table = false, $join = false ) {
938965
*/
939966
function or_where ( $arrValue, $table = false, $join = false ) {
940967

968+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
969+
941970
if ( $join ) $this->joinTable( $join );
942971

943972
$this->db->or_where( $arrValue );
@@ -965,7 +994,9 @@ function or_where ( $arrValue, $table = false, $join = false ) {
965994
* The join implementation are same with @get_all_rows && @get_specified_row() method
966995
*/
967996
function having ( $arrValue, $table = false, $join = false ) {
968-
997+
998+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
999+
9691000
if ( $join ) $this->joinTable( $join );
9701001

9711002
$this->db->having( $arrValue );
@@ -994,6 +1025,8 @@ function having ( $arrValue, $table = false, $join = false ) {
9941025
*/
9951026
function or_having ( $arrValue, $table = false, $join = false ) {
9961027

1028+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1029+
9971030
if ( $join ) $this->joinTable( $join );
9981031

9991032
$this->db->or_having( $arrValue );
@@ -1022,6 +1055,8 @@ function or_having ( $arrValue, $table = false, $join = false ) {
10221055
*/
10231056
function where_in ( $arrValue, $table = false, $join = false ) {
10241057

1058+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1059+
10251060
if ( $join ) $this->joinTable( $join );
10261061

10271062
foreach( $arrValue as $key => $value ) {
@@ -1052,6 +1087,8 @@ function where_in ( $arrValue, $table = false, $join = false ) {
10521087
*/
10531088
function or_where_in ( $arrValue, $table = false, $join = false ) {
10541089

1090+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1091+
10551092
if ( $join ) $this->joinTable( $join );
10561093

10571094
foreach( $arrValue as $key => $value ) {
@@ -1082,6 +1119,8 @@ function or_where_in ( $arrValue, $table = false, $join = false ) {
10821119
*/
10831120
function where_not_in ( $arrValue, $table = false, $join = false ) {
10841121

1122+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1123+
10851124
if ( $join ) $this->joinTable( $join );
10861125

10871126
foreach( $arrValue as $key => $value ) {
@@ -1112,6 +1151,8 @@ function where_not_in ( $arrValue, $table = false, $join = false ) {
11121151
*/
11131152
function or_where_not_in ( $arrValue, $table = false, $join = false ) {
11141153

1154+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1155+
11151156
if ( $join ) $this->joinTable( $join );
11161157

11171158
foreach( $arrValue as $key => $value ) {
@@ -1143,6 +1184,8 @@ function or_where_not_in ( $arrValue, $table = false, $join = false ) {
11431184
*/
11441185
function like ( $arrValue, $table = false, $join = false ) {
11451186

1187+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1188+
11461189
if ( $join ) $this->joinTable( $join );
11471190

11481191
foreach( $arrValue as $key => $value ) {
@@ -1174,6 +1217,8 @@ function like ( $arrValue, $table = false, $join = false ) {
11741217
*/
11751218
function like_before ( $arrValue, $table = false, $join = false ) {
11761219

1220+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1221+
11771222
if ( $join ) $this->joinTable( $join );
11781223

11791224
foreach( $arrValue as $key => $value ) {
@@ -1205,6 +1250,8 @@ function like_before ( $arrValue, $table = false, $join = false ) {
12051250
*/
12061251
function like_after ( $arrValue, $table = false, $join = false ) {
12071252

1253+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1254+
12081255
if ( $join ) $this->joinTable( $join );
12091256

12101257
foreach( $arrValue as $key => $value ) {
@@ -1236,6 +1283,8 @@ function like_after ( $arrValue, $table = false, $join = false ) {
12361283
*/
12371284
function or_like ( $arrValue, $table = false, $join = false ) {
12381285

1286+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1287+
12391288
if ( $join ) $this->joinTable( $join );
12401289

12411290
foreach( $arrValue as $key => $value ) {
@@ -1267,6 +1316,8 @@ function or_like ( $arrValue, $table = false, $join = false ) {
12671316
*/
12681317
function or_like_before ( $arrValue, $table = false, $join = false ) {
12691318

1319+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1320+
12701321
if ( $join ) $this->joinTable( $join );
12711322

12721323
foreach( $arrValue as $key => $value ) {
@@ -1298,6 +1349,8 @@ function or_like_before ( $arrValue, $table = false, $join = false ) {
12981349
*/
12991350
function or_like_after ( $arrValue, $table = false, $join = false ) {
13001351

1352+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1353+
13011354
if ( $join ) $this->joinTable( $join );
13021355

13031356
foreach( $arrValue as $key => $value ) {
@@ -1329,6 +1382,8 @@ function or_like_after ( $arrValue, $table = false, $join = false ) {
13291382
*/
13301383
function not_like ( $arrValue, $table = false, $join = false ) {
13311384

1385+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1386+
13321387
if ( $join ) $this->joinTable( $join );
13331388

13341389
foreach( $arrValue as $key => $value ) {
@@ -1360,6 +1415,8 @@ function not_like ( $arrValue, $table = false, $join = false ) {
13601415
*/
13611416
function not_like_before ( $arrValue, $table = false, $join = false ) {
13621417

1418+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1419+
13631420
if ( $join ) $this->joinTable( $join );
13641421

13651422
foreach( $arrValue as $key => $value ) {
@@ -1391,6 +1448,8 @@ function not_like_before ( $arrValue, $table = false, $join = false ) {
13911448
*/
13921449
function not_like_after ( $arrValue, $table = false, $join = false ) {
13931450

1451+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1452+
13941453
if ( $join ) $this->joinTable( $join );
13951454

13961455
foreach( $arrValue as $key => $value ) {
@@ -1422,6 +1481,8 @@ function not_like_after ( $arrValue, $table = false, $join = false ) {
14221481
*/
14231482
function or_not_like ( $arrValue, $table = false, $join = false ) {
14241483

1484+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1485+
14251486
if ( $join ) $this->joinTable( $join );
14261487

14271488
foreach( $arrValue as $key => $value ) {
@@ -1453,6 +1514,8 @@ function or_not_like ( $arrValue, $table = false, $join = false ) {
14531514
*/
14541515
function or_not_like_before ( $arrValue, $table = false, $join = false ) {
14551516

1517+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1518+
14561519
if ( $join ) $this->joinTable( $join );
14571520

14581521
foreach( $arrValue as $key => $value ) {
@@ -1484,6 +1547,8 @@ function or_not_like_before ( $arrValue, $table = false, $join = false ) {
14841547
*/
14851548
function or_not_like_after ( $arrValue, $table = false, $join = false ) {
14861549

1550+
if ( empty( $arrValue ) ) $this->InvalidArgExceptionThrow( 1 );
1551+
14871552
if ( $join ) $this->joinTable( $join );
14881553

14891554
foreach( $arrValue as $key => $value ) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
trait ExceptionThrow {
4+
5+
function InvalidArgExceptionThrow ( $noOfArgs ) {
6+
7+
$is_are = $noOfArgs > 1 ? 'are' : 'is';
8+
throw new InvalidArgumentException( $noOfArgs . ' required ' . ( $noOfArgs > 1 ? 'arguments ' : 'argument ' ) . $is_are . ' missing' );
9+
10+
}
11+
}

0 commit comments

Comments
 (0)