Skip to content

Commit

Permalink
feat: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Mar 13, 2023
1 parent 41625da commit f7ae6ca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Core/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
* @method static \Core\Model\Query min(string $name)
* @method static \Core\Model\Query avg(string $name)
* @method static \Core\Model\Query sum(string $name)
* @method static \Core\Model\Query id(mixed $id, mixed $where = null)
* @method static \Core\Model\Query id(mixed $id, string|null $where = null)
* @method static \Core\Model\Model get()
* @method static \Core\Model\Model first()
* @method static \Core\Model\Model find(mixed $id, mixed $where = null)
* @method static \Core\Model\Model find(mixed $id, string|null $where = null)
* @method static \Core\Model\Model create(array $data)
* @method static bool destroy(int $id)
* @method static bool update(array $data)
Expand Down Expand Up @@ -103,7 +103,7 @@ public function setTable(string $name): Model
}

/**
* Hapus attribute dates.
* Hapus attribute dates dan primaryKey.
*
* @return Model
*/
Expand Down
18 changes: 7 additions & 11 deletions src/Core/Model/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function fullJoin(string $table, string $column, string $refers, string $
public function orderBy(string $name, string $order = 'ASC'): Query
{
$agr = str_contains($this->query, 'ORDER BY') ? ', ' : ' ORDER BY ';
$this->query = $this->query . $agr . $name . ' ' . strtoupper($order);
$this->query = $this->query . $agr . $name . ' ' . ($order === 'ASC' ? 'ASC' : 'DESC');

return $this;
}
Expand Down Expand Up @@ -491,10 +491,10 @@ public function destroy(int $id): bool
* Cari berdasarkan id.
*
* @param mixed $id
* @param mixed $where
* @param string|null $where
* @return Model
*/
public function find(mixed $id, mixed $where = null): Model
public function find(mixed $id, string|null $where = null): Model
{
return $this->id($id, $where)->limit(1)->first();
}
Expand Down Expand Up @@ -544,7 +544,7 @@ public function create(array $data): Model
'INSERT INTO %s (%s) VALUES (%s)',
$this->table,
implode(', ', $keys),
implode(', ', array_map(fn ($data) => ':' . $data, $keys))
implode(', ', array_map(fn ($field) => ':' . $field, $keys))
);

$this->bind($query, $data);
Expand Down Expand Up @@ -577,12 +577,10 @@ public function update(array $data): bool
}

$query = is_null($this->query) ? 'UPDATE ' . $this->table . ' WHERE' : str_replace('SELECT * FROM', 'UPDATE', $this->query);
$setQuery = 'SET ' . implode(', ', array_map(fn ($data) => $data . ' = :' . $data, array_keys($data))) . ($this->query ? ' WHERE' : '');
$setQuery = 'SET ' . implode(', ', array_map(fn ($field) => $field . ' = :' . $field, array_keys($data))) . ($this->query ? ' WHERE' : '');

$this->bind(str_replace('WHERE', $setQuery, $query), array_merge($data, $this->param ?? []));
$result = $this->db->execute();

return boolval($result);
return $this->db->execute();
}

/**
Expand All @@ -595,8 +593,6 @@ public function delete(): bool
$query = is_null($this->query) ? 'DELETE FROM ' . $this->table : str_replace('SELECT *', 'DELETE', $this->query);

$this->bind($query, $this->param ?? []);
$result = $this->db->execute();

return boolval($result);
return $this->db->execute();
}
}
15 changes: 13 additions & 2 deletions src/Core/Support/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Core\Support;

use Core\Database\Generator;
use Core\Database\Migration;
use Core\Routing\Route;
use Core\Valid\Hash;
use Exception;

/**
* Saya console untuk mempermudah develop app.
Expand Down Expand Up @@ -56,7 +59,7 @@ function __construct(array $argv = [])
$this->command = $argv[0] ?? null;
$this->options = $argv[1] ?? null;

print($this->createColor('green', "Kamu PHP Framework v1.1.6\n"));
print($this->createColor('green', "Kamu PHP Framework v1.1.25\n"));
print($this->createColor('yellow', "Saya Console\n\n"));
}

Expand Down Expand Up @@ -127,7 +130,7 @@ private function exception(string $message, bool $fail = true, mixed $options =
private function executeTime(): string
{
$now = microtime(true);
$result = diffTime($this->timenow, $now);
$result = strval(diffTime($this->timenow, $now));
$this->timenow = $now;

return $this->createColor('cyan', '(' . $result . ' ms)');
Expand All @@ -148,6 +151,10 @@ private function migrasi(bool $up): void

foreach ($files as $file) {
$arg = require $baseFile . $file;
if (!($arg instanceof Migration)) {
throw new Exception('File ' . $file . ' bukan migrasi !');
}

($up) ? $arg->up() : $arg->down();
$info = ($up) ? $this->createColor('green', ' Migrasi ') : $this->createColor('yellow', ' Migrasi kembali ');
print("\n" . $file . $info . $this->executeTime());
Expand All @@ -162,6 +169,10 @@ private function migrasi(bool $up): void
private function generator(): void
{
$arg = require basepath() . '/database/generator.php';
if (!($arg instanceof Generator)) {
throw new Exception('File ' . $file . ' bukan generator !');
}

$arg->run();
print("\nGenerator" . $this->createColor('green', ' berhasil ') . $this->executeTime());
}
Expand Down
18 changes: 7 additions & 11 deletions src/Core/Valid/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Core\Valid;

use Core\Facades\App;

/**
* Validasi sebuah nilai.
*
Expand Down Expand Up @@ -100,8 +98,7 @@ private function validateRequest(string $param, mixed $value, string $rule): voi
break;

case $rule == 'dns':
$domain = explode('@', $value);
if (!checkdnsrr($domain[1])) {
if (!checkdnsrr(explode('@', $value)[1])) {
$this->setError($param, 'ilegal atau tidak sah !');
}
break;
Expand Down Expand Up @@ -148,8 +145,7 @@ private function validateRequest(string $param, mixed $value, string $rule): voi

case $rule == 'safe':
$bad = array_merge(array_map('chr', range(0, 31)), ['\\', '/', ':', '*', '?', '"', '<', '>', '|']);
$filename = str_replace($bad, '', $value);
$this->__set($param, $filename);
$this->__set($param, str_replace($bad, '', $value));
break;

case $rule == 'hash':
Expand All @@ -161,14 +157,14 @@ private function validateRequest(string $param, mixed $value, string $rule): voi
break;

case str_contains($rule, 'min'):
$min = explode(':', $rule)[1];
$min = intval(explode(':', $rule)[1]);
if (strlen($value) < $min) {
$this->setError($param, 'panjang minimal', $min);
}
break;

case str_contains($rule, 'max'):
$max = explode(':', $rule)[1];
$max = intval(explode(':', $rule)[1]);
if (strlen($value) > $max) {
$this->setError($param, 'panjang maximal', $max);
}
Expand All @@ -187,7 +183,7 @@ private function validateRequest(string $param, mixed $value, string $rule): voi
$model = 'App\Models\\' . (empty($command[1]) ? 'User' : ucfirst($command[1]));
$column = $command[2] ?? $param;

$data = App::get()->singleton($model)->find($value, $column);
$data = (new $model)->find($value, $column);
if ($data->{$column}) {
$this->setError($param, 'sudah ada !');
}
Expand Down Expand Up @@ -233,15 +229,15 @@ private function validateFile(string $param, array $value, string $rule): void
break;

case str_contains($rule, 'min'):
$min = explode(':', $rule)[1] * 1024;
$min = intval(explode(':', $rule)[1]) * 1024;
if ($value['size'] < $min) {
@unlink($value['tmp_name']);
$this->setError($param, 'ukuran minimal', formatBytes($min));
}
break;

case str_contains($rule, 'max'):
$max = explode(':', $rule)[1] * 1024;
$max = intval(explode(':', $rule)[1]) * 1024;
if ($value['size'] > $max) {
@unlink($value['tmp_name']);
$this->setError($param, 'ukuran maximal', formatBytes($max));
Expand Down

0 comments on commit f7ae6ca

Please sign in to comment.