Last active
October 12, 2022 21:40
-
-
Save mhmtsnmzkanly/8874ae6f79bf73ba4692d727ad725aa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Table | |
{ | |
/* Değişkenler */ | |
public $name; | |
public $self; | |
public $fields; | |
/* Metodlar */ | |
public function __construct() | |
{ | |
$this->init_self(); | |
try { | |
if ($this->exists()) // Var mı? | |
$this->set_self(); // Varsa ekle | |
} catch (Lazer\Classes\LazerException $e) { | |
$this->create(); // Oluştur | |
$this->set_self(); // Varsa ekle | |
$this->generate_data(); // Temel veriyi ekle. | |
} | |
} | |
public function __destruct() | |
{ | |
$this->destroy_self(); | |
} | |
public function s() | |
{ | |
return $this->self; | |
} | |
public function set_self() | |
{ | |
$this->self = Lazer\Classes\Database::table($this->name); | |
} | |
public function create() | |
{ | |
Lazer\Classes\Database::create($this->name, $this->fields); | |
} | |
public function remove() | |
{ | |
return Lazer\Classes\Database::remove($this->name); | |
} | |
public function exists(): bool | |
{ | |
return Lazer\Classes\Helpers\Validate::table($this->name)->exists(); | |
} | |
public function return_data(): array | |
{ | |
return $this->self->findAll()->asArray(); | |
} | |
public function where(string $key, string $condition, string $input) | |
{ | |
return $this->self->where($key, $condition, $input); | |
} | |
// Sınıf oluşturulduğunda ilk çağırılacak fonksiyon. | |
abstract public function init_self(); | |
// Sınıf yok edildiğinde ilk çağırılacak fonksiyon. | |
abstract public function destroy_self(); | |
// Eğer tablonun sahip olması gereken belirli veriler varsa otomatik ekler. | |
abstract public function generate_data(); | |
// Tabloya satır ekler | |
public function auto_add_row($input) | |
{ | |
// Girilen değerini doğrula | |
$input = $this->validate_row($input); | |
// Girilen değerin kontrolü | |
if (empty($input)) | |
throw new Exception('Invalid Input: Row is Empty.'); | |
// Alanlar ile uygun hale getir. | |
$input = $this->clean_row($input); | |
// Yeni satır | |
$new_row = $this->self; | |
// Satırın değişkene döndürülmesi | |
foreach ($input as $k => $v) { | |
$new_row->setField($k, $v); | |
unset($k, $v); | |
} | |
// Satırı kaydet | |
$new_row->save(); | |
// verileri sil. | |
unset($input, $new_row); | |
} | |
public function generate_row() | |
{ | |
$new_row_object = new stdClass(); | |
$new_row_object->_name = $this->name; // Tablo adı | |
foreach ($this->fields as $key => $value) { | |
$new_row_object->{$key} = $value == 'string' ? "Default Data" : 0; | |
$new_row_object->{"set_" . $key} = function ($input) use ($key) { | |
$this->{$key} = $input; | |
return $this; | |
}; | |
} | |
return $new_row_object; | |
} | |
public function validate_row($input): array | |
{ | |
// Girilen değer boş mu kontrolü | |
if (empty($input)) | |
throw new Exception('Invalid Input: Row is empty'); | |
// Obje mi? | |
if (is_object($input)) $input = (array)$input; | |
// Veri bu tablo için mi oluşturulmuş? | |
if (empty($input['_name']) || $input['_name'] != $this->name) | |
throw new Exception('Invalid Input: Row type are Different.'); | |
return $input; | |
} | |
public function clean_row(array $input): array | |
{ | |
$diff_keys = array_keys(array_diff_key($input, $this->fields)); | |
foreach ($diff_keys as $key) | |
unset($input[$key]); | |
return $input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment