Skip to content

Commit ec74459

Browse files
committed
sync system
1 parent e65841d commit ec74459

4 files changed

Lines changed: 280 additions & 6 deletions

File tree

system/database/facile/model.php

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ abstract class Model
117117
*/
118118
public static $perpage = 20;
119119

120+
/**
121+
* Berisi global scopes yang diterapkan ke setiap query.
122+
*
123+
* @var array
124+
*/
125+
protected static $global_scopes = [];
126+
120127
/**
121128
* Berisi array nama field dan rules untuk kebutuhan validasi data model.
122129
* Anda bisa melakukan validasi data inputan menggunakan method ini.
@@ -972,18 +979,66 @@ public function query()
972979
}
973980

974981
/**
975-
* Apply global scopes to query.
982+
* Terapkan global scopes ke query.
976983
*
977984
* @param Query $query
978985
*
979986
* @return Query
980987
*/
981988
protected function apply_scopes($query)
982989
{
983-
// Apply segala global scopes disini jika ada
990+
foreach (static::$global_scopes as $scope) {
991+
if ($scope instanceof \Closure) {
992+
$scope($query);
993+
} elseif (is_object($scope) && method_exists($scope, 'apply')) {
994+
$scope->apply($query, $this);
995+
}
996+
}
997+
984998
return $query;
985999
}
9861000

1001+
/**
1002+
* Tambahkan global scope ke model.
1003+
*
1004+
* @param string|\Closure|object $scope
1005+
* @param \Closure|object $implementation
1006+
*
1007+
* @return $this
1008+
*/
1009+
public static function add_global_scope($scope, $implementation = null)
1010+
{
1011+
if (is_string($scope) && !is_null($implementation)) {
1012+
static::$global_scopes[$scope] = $implementation;
1013+
} elseif ($scope instanceof \Closure) {
1014+
static::$global_scopes[spl_object_hash($scope)] = $scope;
1015+
} elseif (is_object($scope)) {
1016+
static::$global_scopes[get_class($scope)] = $scope;
1017+
}
1018+
}
1019+
1020+
/**
1021+
* Hapus global scope dari model.
1022+
*
1023+
* @param string $scope
1024+
*
1025+
* @return $this
1026+
*/
1027+
public static function remove_global_scope($scope)
1028+
{
1029+
unset(static::$global_scopes[$scope]);
1030+
}
1031+
1032+
/**
1033+
* Ambil global scopes yang diterapkan ke model.
1034+
*
1035+
* @return array
1036+
*/
1037+
public static function get_global_scopes()
1038+
{
1039+
return static::$global_scopes;
1040+
}
1041+
9871042
/**
9881043
* Ambil instance query builder baru.
9891044
*
@@ -1003,7 +1058,7 @@ protected function _query()
10031058
}
10041059

10051060
/**
1006-
* Handle static method calls.
1061+
* Handle pemanggilan static method.
10071062
*
10081063
* @param string $method
10091064
* @param array $parameters
@@ -1012,11 +1067,19 @@ protected function _query()
10121067
*/
10131068
public static function __callStatic($method, array $parameters)
10141069
{
1015-
return call_user_func_array([(new static())->query(), $method], $parameters);
1070+
$instance = new static();
1071+
$query = $instance->query();
1072+
$scope = 'scope_' . $method;
1073+
1074+
if (method_exists($instance, $scope)) {
1075+
return call_user_func_array([$instance, $scope], array_merge([$query], $parameters));
1076+
}
1077+
1078+
return call_user_func_array([$query, $method], $parameters);
10161079
}
10171080

10181081
/**
1019-
* Handle dynamic property access for getting attributes.
1082+
* Handle akses property dinamis untuk mengambil attriv=butes.
10201083
*
10211084
* @param string $key
10221085
*
@@ -1028,7 +1091,7 @@ public function __get($key)
10281091
}
10291092

10301093
/**
1031-
* Handle dynamic property access for setting attributes.
1094+
* Handle akses property dinamis untuk mengatur attributes.
10321095
*
10331096
* @param string $key
10341097
* @param mixed $value

system/database/query.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,204 @@ public function order_by($column, $direction = 'asc')
11271127
return $this;
11281128
}
11291129

1130+
/**
1131+
* Tambahkan klausa WHERE untuk tanggal.
1132+
*
1133+
* @param string $column
1134+
* @param string $operator
1135+
* @param mixed $value
1136+
* @param string $connector
1137+
*
1138+
* @return Query
1139+
*/
1140+
public function where_date($column, $operator, $value, $connector = 'AND')
1141+
{
1142+
return $this->where($this->raw('DATE(' . $column . ')'), $operator, $value, $connector);
1143+
}
1144+
1145+
/**
1146+
* Tambahkan klausa WHERE untuk bulan.
1147+
*
1148+
* @param string $column
1149+
* @param string $operator
1150+
* @param mixed $value
1151+
* @param string $connector
1152+
*
1153+
* @return Query
1154+
*/
1155+
public function where_month($column, $operator, $value, $connector = 'AND')
1156+
{
1157+
return $this->where($this->raw('MONTH(' . $column . ')'), $operator, $value, $connector);
1158+
}
1159+
1160+
/**
1161+
* Tambahkan klausa WHERE untuk hari.
1162+
*
1163+
* @param string $column
1164+
* @param string $operator
1165+
* @param mixed $value
1166+
* @param string $connector
1167+
*
1168+
* @return Query
1169+
*/
1170+
public function where_day($column, $operator, $value, $connector = 'AND')
1171+
{
1172+
return $this->where($this->raw('DAY(' . $column . ')'), $operator, $value, $connector);
1173+
}
1174+
1175+
/**
1176+
* Tambahkan klausa WHERE untuk tahun.
1177+
*
1178+
* @param string $column
1179+
* @param string $operator
1180+
* @param mixed $value
1181+
* @param string $connector
1182+
*
1183+
* @return Query
1184+
*/
1185+
public function where_year($column, $operator, $value, $connector = 'AND')
1186+
{
1187+
return $this->where($this->raw('YEAR(' . $column . ')'), $operator, $value, $connector);
1188+
}
1189+
1190+
/**
1191+
* Tambahkan klausa WHERE untuk waktu.
1192+
*
1193+
* @param string $column
1194+
* @param string $operator
1195+
* @param mixed $value
1196+
* @param string $connector
1197+
*
1198+
* @return Query
1199+
*/
1200+
public function where_time($column, $operator, $value, $connector = 'AND')
1201+
{
1202+
return $this->where($this->raw('TIME(' . $column . ')'), $operator, $value, $connector);
1203+
}
1204+
1205+
/**
1206+
* Tambahkan klausa WHERE untuk membandingkan dua kolom.
1207+
*
1208+
* @param string $column1
1209+
* @param string $operator
1210+
* @param string $column2
1211+
* @param string $connector
1212+
*
1213+
* @return Query
1214+
*/
1215+
public function where_column($column1, $operator, $column2, $connector = 'AND')
1216+
{
1217+
$this->wheres[] = [
1218+
'type' => 'where_column',
1219+
'column1' => $column1,
1220+
'operator' => $operator,
1221+
'column2' => $column2,
1222+
'connector' => $connector,
1223+
];
1224+
return $this;
1225+
}
1226+
1227+
/**
1228+
* Tambahkan ORDER BY untuk record terbaru.
1229+
*
1230+
* @param string $column
1231+
*
1232+
* @return Query
1233+
*/
1234+
public function latest($column = 'created_at')
1235+
{
1236+
return $this->order_by($column, 'desc');
1237+
}
1238+
1239+
/**
1240+
* Tambahkan ORDER BY untuk record tertua.
1241+
*
1242+
* @param string $column
1243+
*
1244+
* @return Query
1245+
*/
1246+
public function oldest($column = 'created_at')
1247+
{
1248+
return $this->order_by($column, 'asc');
1249+
}
1250+
1251+
/**
1252+
* Check apakah query memiliki hasil.
1253+
*
1254+
* @return bool
1255+
*/
1256+
public function exists()
1257+
{
1258+
$query = $this->copy();
1259+
$query->selects = ['*'];
1260+
$query->limit = 1;
1261+
$sql = $query->grammar->select($query);
1262+
$result = $query->connection->query($sql, $query->bindings);
1263+
1264+
return count($result) > 0;
1265+
}
1266+
1267+
/**
1268+
* Check apakah query tidak memiliki hasil.
1269+
*
1270+
* @return bool
1271+
*/
1272+
public function doesnt_exist()
1273+
{
1274+
return !$this->exists();
1275+
}
1276+
1277+
/**
1278+
* Lakukan chunk berdasarkan ID.
1279+
*
1280+
* @param int $count
1281+
* @param callable $callback
1282+
* @param string $column
1283+
* @param string $alias
1284+
*
1285+
* @return bool
1286+
*/
1287+
public function chunk_by_id($count, callable $callback, $column = 'id', $alias = null)
1288+
{
1289+
$count = (int) $count;
1290+
$alias = $alias ?: $column;
1291+
$last_id = null;
1292+
1293+
do {
1294+
$clone = $this->copy();
1295+
1296+
if (!is_null($last_id)) {
1297+
$clone->where($column, '>', $last_id);
1298+
}
1299+
1300+
$clone->order_by($column, 'asc')->take($count);
1301+
$results = $clone->get();
1302+
$counts = count($results);
1303+
1304+
if ($counts === 0) {
1305+
break;
1306+
}
1307+
1308+
if ($callback($results) === false) {
1309+
return false;
1310+
}
1311+
1312+
$last_id = $results[$counts - 1]->$alias;
1313+
} while ($counts === $count);
1314+
1315+
return true;
1316+
}
1317+
1318+
/**
1319+
* Dump query dan die.
1320+
*
1321+
* @return void
1322+
*/
1323+
public function dd()
1324+
{
1325+
dd($this->debug());
1326+
}
1327+
11301328
/**
11311329
* Tangani pemanggilan method secara dinamis.
11321330
* Seperti fungsi agregasi dan where.

system/database/query/grammars/grammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ protected function where_not_null($where)
341341
return $this->wrap($where['column']) . ' IS NOT NULL';
342342
}
343343

344+
/**
345+
* Compile klausa WHERE COLUMN.
346+
*
347+
* @param array $where
348+
*
349+
* @return string
350+
*/
351+
protected function where_column($where)
352+
{
353+
return $this->wrap($where['column1']) . ' ' . $where['operator'] . ' ' . $this->wrap($where['column2']);
354+
}
355+
344356
/**
345357
* Compile klausa GROUP BY.
346358
*

system/macroable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static function mixin($mixin, $replace = true)
4141

4242
foreach ($methods as $method) {
4343
if ($replace || !static::has_macro($method->name)) {
44+
/** @disregard */
4445
$method->setAccessible(true);
4546
static::macro($method->name, $method->invoke($mixin));
4647
}

0 commit comments

Comments
 (0)