ORM Stage 3-1

This commit is contained in:
Abdussamed 2023-04-02 18:07:52 +03:00
parent 7dda364127
commit f1a2af9c9d
6 changed files with 686 additions and 16 deletions

View File

@ -2,6 +2,9 @@
include_once "Request.php"; include_once "Request.php";
include_once "Response.php"; include_once "Response.php";
include_once "Session.php"; include_once "Session.php";
include_once "Helpers.php";
include_once "DatabaseConnection.php";
include_once "DatabaseORM.php";
define("request", new Request(), false); define("request", new Request(), false);
define("response", new Response(), false); define("response", new Response(), false);

111
Core/DatabaseConnection.php Normal file
View File

@ -0,0 +1,111 @@
<?php
include_once "Helpers.php";
class Connection
{
public $client = "mysql";
public $user = "root";
public $pass = "";
public $host = "127.0.0.1";
public $port = 3306;
public $database = "";
public $charset = "utf8mb4";
public $file = "";
public $connectionString = null;
public $db = null;
public $ready = false;
public function __construct(
$client,
$user,
$pass,
$host = "127.0.0.1",
$port = "3306",
$database = "",
$charset = ""
)
{
$this->client = $client;
$this->user = $user;
$this->pass = $pass;
$this->host = $host;
$this->port = $port;
$this->database = $database;
$this->charset = $charset;
}
public function connect()
{
if($this->db == null)
{
$this->db = new PDO(
$this->createConnectionString(),
$this->user,
$this->pass,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
)
);
$this->ready = true;
}
}
public function assertConnection()
{
if($this->db == null)
{
$this->connect();
if($this->ready == false)
{
throw new Exception("Connection not established");
}
};
}
public function createConnectionString()
{
if($this->connectionString)
{
return $this->connectionString;
}else{
$str = new tstring($this->client);
return $str
->concat(":", "host=", $this->host, ";")
->concat("port=", $this->port, ";")
->concat("database=", $this->database, ";")
->concat("host=", $this->host, ";");
}
}
public function exec($sql)
{
$this->assertConnection();
$effected = $this->db->exec($sql);
if($effected === false)
{
throw new Exception($this->db->errorInfo());
}else{
return $effected;
}
}
public function lastinsertid()
{
$this->assertConnection();
return $this->db->lastinsertid();
}
public function prepare($sql, $params = [])
{
$this->assertConnection();
$prepare = $this->db->prepare($sql);
if($prepare->execute($params))
{
return $prepare;
}else{
throw new Exception($this->db->errorInfo());
}
}
public function fetch($sql, $params = [])
{
$this->assertConnection();
return $this->prepare($sql, $params)->fetch(PDO::FETCH_ASSOC);
}
public function fetchAll($sql, $params = [])
{
$this->assertConnection();
return $this->prepare($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
}
};

289
Core/DatabaseORM.php Normal file
View File

@ -0,0 +1,289 @@
<?php
include_once "Reflection.php";
include_once "Helpers.php";
function name($name)
{
return (
new tstring($name)
)->wrap(".", ".", function($item){
return "`" . $item->match("/^[\"\']?(.*?)[\"\']?$/")[1] . "`";
});
}
function isString($data)
{
return gettype($data) == "string" || $data instanceof tstring;
}
class ORM
{
public $mode = "select";
public $database = "";
public $table = "";
public $select = [];
public $where = [];
public $groupBy = [];
public $having = [];
public $orderby = [];
public $limit = [];
public $union = [];
public $set = [];
public $join = [];
public $alter = [];
public static function name($args)
{
return name($args);
}
public function from($orm)
{
foreach (get_class_vars($orm) as $key => $value) {
$this->{$key} = $value;
}
}
public function table($table)
{
$this->table = $table;
return $this;
}
public function database($database)
{
$this->database = $database;
return $this;
}
public function select(...$args)
{
$this->select = array_merge($this->select, array_map(
function($item){
return new tstring($item);
},
$args
));
return $this;
}
public function update(...$arguments)
{
$this->mode = "update";
$this->set($arguments);
}
public function delete()
{
$this->mode = "delete";
}
public function insert(...$arguments)
{
$this->mode = "insert";
$this->set($arguments);
}
public function renameTable($from, $to)
{
$this->alter[] = ["renametable", $from, $to];
}
public function renameColumn($from, $to)
{
$this->alter[] = ["renamecolumn", $from, $to];
}
public function modifyColumn($from, $to)
{
$this->alter[] = ["modifyColumn", $from, $to];
}
public function addColumn($from)
{
$this->alter[] = ["addColumn", $from, $to];
}
public function removeColumn($from)
{
$this->alter[] = ["addColumn", $from, $to];
}
public function addWhere($statement, $name, $operator, $value)
{
$this->where = array_merge(
$this->where,
[
$statement,
name($name),
$operator,
$value
]
);
}
public function where(...$statements)
{
if(
(count($statements) == 3 && isString($statements[0]) && isString($statements[1]))
)
{
$this->addWhere("and",...$statements);
}
else if(
(count($statements) == 2 && isString($statements[0]))
)
{
$this->addWhere("and",$statements[0],"=",$statements[1]);
}
else if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("and",...$value);
}else{
$this->addWhere("and",$key,"=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("and",$statements[0],"IS","null");
};
return $this;
}
public function whereNot(...$statements)
{
if(
(count($statements) == 3 && isString($statements[0]) && isString($statements[1]))
)
{
$this->addWhere("and",...$statements);
}
else if(
(count($statements) == 2 && isString($statements[0]))
)
{
$this->addWhere("and",$statements[0],"!=",$statements[1]);
}
else if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("and",...$value);
}else{
$this->addWhere("and",$key,"!=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("and",$statements[0],"IS","null");
};
return $this;
}
public function orWhere(...$statements)
{
if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("or",...$value);
}else{
$this->addWhere("or",$key,"=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("or",$statements[0],"IS","null");
}
else if(count($statements) == 3 && gettype($statements[0]) == "string" && gettype($statements[1]) == "string")
{
$this->addWhere("or",...$statements);
}
return $this;
}
public function orWhereNot(...$statements)
{
if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("or",...$value);
}else{
$this->addWhere("or",$key,"!=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("or",$statements[0],"IS NOT","null");
}
else if(count($statements) == 3 && gettype($statements[0]) == "string" && gettype($statements[1]) == "string")
{
$this->addWhere("or",...$statements);
}
return $this;
}
public function whereIn($name, $arr)
{
$this->where(name($name), "IN", '('. implode(', ',$arr) .')');
return $this;
}
public function orWhereIn($name, $arr)
{
$this->orwhere(name($name), "IN", '('. implode(', ',$arr) .')');
return $this;
}
public function whereNotIn($name, $arr)
{
$this->where(name($name), "NOT IN", '('. implode(', ',$arr) .')');
return $this;
}
public function orWhereNotIn($name, $arr)
{
$this->orwhere(name($name), "NOT IN", '('. implode(', ',$arr) .')');
return $this;
}
public function between($name, $state1, $state2)
{
$this->orwhere(name($name), "BETWEEN", $state1 . " AND " . $state2);
return $this;
}
public function groupBy(...$name)
{
$this->groupBy = array_merge(
$this->groupBy,
...$name
);
return $this;
}
public function having(...$name)
{
$this->having = array_merge(
$this->having,
...$name
);
return $this;
}
public function orderBy($name,$mode = "ASC")
{
$this->orderBy[] = [$name,$mode];
return $this;
}
public function set($name,$value = false)
{
if(is_array($name))
{
foreach ($name as $key => $value) {
$this->set[] = [
name($key),
$value
];
}
}else{
$this->set[] = [
name($name),
$value
];
}
return $this;
}
};
$t = new ORM();
$t->table("argist");
$t->whereIn("name",[1,2,3]);
$t->orWhere([
"name"=>"value"
]);
dd($t);

274
Core/Helpers.php Normal file
View File

@ -0,0 +1,274 @@
<?php
include_once "Reflection.php";
function isRegex($string)
{
$mathes = [];
$execute = preg_match(
"/^\/.*\/[imsxADSUXJu]+$/",
$string,
$mathes
);
return $execute;
}
class tstring
{
use ReflectionHook;
protected $data = "";
static function isstring($data)
{
if(gettype($data) == "string")
{
return true;
}
if(gettype($data) == "object")
{
return method_exists($data, "__toString");
}
return false;
}
public function __construct($data = "")
{
$this->data = $data;
}
public function getLengthAttribute()
{
return strlen($this->data);
}
public function at($index) : string
{
return $this->data[$index] ?? "";
}
public function charAt($index) : string
{
return $this->at($index);
}
public function concat(...$string) : tstring
{
return new tstring(
implode('',[
$this->data,
...$string
])
);
}
public function endsWith($string) : bool
{
if(PHP_MAJOR_VERSION >= 8)
{
return str_ends_with($this->data, $string);
};
$size = strlen($string);
if(substr($this->data, -$size) == $string)
{
return true;
}else{
return false;
}
}
public function startsWith($string) : bool
{
if(PHP_MAJOR_VERSION >= 8)
{
return str_starts_with($this->data, $string);
};
$size = strlen($string);
if(substr($this->data, 0, $size) == $string)
{
return true;
}else{
return false;
}
}
public function match($pattern) : array
{
$mathes = [];
preg_match(
$pattern,
$this->data,
$mathes
);
return $mathes;
}
public function matchAll($pattern) : array
{
$mathes = [];
preg_match_all(
$pattern,
$this->data,
$mathes
);
return $mathes;
}
public function padStart($length, $padString = " ")
{
return new tstring(
str_pad(
$this->data,
$length,
$padString,
STR_PAD_RIGHT
)
);
}
public function padEnd($length, $padString = " ")
{
return new tstring(
str_pad(
$this->data,
$length,
$padString,
STR_PAD_LEFT
)
);
}
public function repeat($count)
{
return new tstring(
str_repeat(
$this->data,
$count
)
);
}
public function replace($pattern, $subject = "")
{
if(gettype($pattern) == "array")
{
$a = [];
$b = [];
foreach ($pattern as $key => $value) {
$a[] = $key;
$b[] = $value ?? "";
};
return new tstring(
str_replace($a, $b, $this->data)
);
}else if(gettype($pattern) == "string" && isRegex($pattern))
{
return new tstring(
preg_replace(
$pattern,
$subject,
$this->data
)
);
}else{
return new tstring(
str_replace(
$pattern,
$subject,
$this->data
)
);
}
}
public function ireplace($pattern, $subject = "")
{
if(gettype($pattern) == "array")
{
$a = [];
$b = [];
foreach ($pattern as $key => $value) {
$a[] = $key;
$b[] = $value ?? "";
};
return new tstring(
str_ireplace($a, $b, $this->data)
);
}else if(gettype($pattern) == "string" && isRegex($pattern))
{
return new tstring(
preg_replace(
$pattern,
$subject,
$this->data
)
);
}else{
return new tstring(
str_ireplace(
$pattern,
$subject,
$this->data
)
);
}
}
public function search($string)
{
return strpos($this->data, $string);
}
public function isearch($string)
{
return stripos($this->data, $string);
}
public function slice($start = 0, $end = null)
{
return new tstring(
substr(
$this->data,
$start,
$end
)
);
}
public function split($delimiter)
{
return array_map(
function($e){
return new tstring($e);
},
explode($delimiter, $this->data)
);
}
public function splitjoin($delimiter, $glue)
{
return new tstring(
implode(
$glue,
explode($delimiter, $this->data)
)
);
}
public function wrap($delimiter, $glue, $callback)
{
return new tstring(
implode(
$glue,
array_map(
function($item) use($callback)
{
return $callback(new tstring($item));
},
explode($delimiter, $this->data)
)
)
);
}
public function toLowerCase()
{
return new tstring(
strtolower($this->data)
);
}
public function toUpperCase()
{
return new tstring(
strtoupper($this->data)
);
}
public function trim()
{
return new tstring(
trim($this->data)
);
}
public function __toString()
{
return $this->data;
}
}
function dd($data){
highlight_string("<?=\n\n" . var_export($data, true) . "\n\n?>");
die();
}

View File

@ -12,20 +12,6 @@
{ {
return (new static)->{"call$name"}($name, ...$arguments); return (new static)->{"call$name"}($name, ...$arguments);
} }
else
{
if(method_exists(__CLASS__, $name))
{
var_dump($callback);
exit;
}
try{
return static::{$name}(...$arguments);
}catch(Exception $e){
}
return null;
}
} }
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
@ -48,7 +34,7 @@
$formarly = ucfirst($name); $formarly = ucfirst($name);
if(method_exists(__CLASS__, "get" . $formarly . "Attribute")) if(method_exists(__CLASS__, "get" . $formarly . "Attribute"))
{ {
return $this->{$formarly}; return $this->{"get" . $formarly . "Attribute"}();
} }
else if(method_exists(__CLASS__, "getAttribute")) else if(method_exists(__CLASS__, "getAttribute"))
{ {

View File

@ -57,7 +57,14 @@
Response::Text($content); Response::Text($content);
break; break;
} }
case "object": case "object":{
if(method_exists($content, "__toString"))
{
return Response::Text($content->__toString());
}else{
return Response::Json($content);
}
}
case "array":{ case "array":{
Response::Json($content); Response::Json($content);
break; break;