From f1a2af9c9d5383eb84880e50111e8024cf24dd57 Mon Sep 17 00:00:00 2001 From: Abdussamed <46815237+abdussamedulutas@users.noreply.github.com> Date: Sun, 2 Apr 2023 18:07:52 +0300 Subject: [PATCH] ORM Stage 3-1 --- Core/Core.php | 3 + Core/DatabaseConnection.php | 111 ++++++++++++++ Core/DatabaseORM.php | 289 ++++++++++++++++++++++++++++++++++++ Core/Helpers.php | 274 ++++++++++++++++++++++++++++++++++ Core/Reflection.php | 16 +- Core/Response.php | 9 +- 6 files changed, 686 insertions(+), 16 deletions(-) create mode 100644 Core/DatabaseConnection.php create mode 100644 Core/DatabaseORM.php create mode 100644 Core/Helpers.php diff --git a/Core/Core.php b/Core/Core.php index 9941014..916da78 100644 --- a/Core/Core.php +++ b/Core/Core.php @@ -2,6 +2,9 @@ include_once "Request.php"; include_once "Response.php"; include_once "Session.php"; + include_once "Helpers.php"; + include_once "DatabaseConnection.php"; + include_once "DatabaseORM.php"; define("request", new Request(), false); define("response", new Response(), false); diff --git a/Core/DatabaseConnection.php b/Core/DatabaseConnection.php new file mode 100644 index 0000000..ba5d64c --- /dev/null +++ b/Core/DatabaseConnection.php @@ -0,0 +1,111 @@ +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); + } + }; \ No newline at end of file diff --git a/Core/DatabaseORM.php b/Core/DatabaseORM.php new file mode 100644 index 0000000..bc9ad50 --- /dev/null +++ b/Core/DatabaseORM.php @@ -0,0 +1,289 @@ +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); \ No newline at end of file diff --git a/Core/Helpers.php b/Core/Helpers.php new file mode 100644 index 0000000..34b43bd --- /dev/null +++ b/Core/Helpers.php @@ -0,0 +1,274 @@ +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(""); + die(); + } \ No newline at end of file diff --git a/Core/Reflection.php b/Core/Reflection.php index 8829703..14b25ce 100644 --- a/Core/Reflection.php +++ b/Core/Reflection.php @@ -12,20 +12,6 @@ { 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) { @@ -48,7 +34,7 @@ $formarly = ucfirst($name); if(method_exists(__CLASS__, "get" . $formarly . "Attribute")) { - return $this->{$formarly}; + return $this->{"get" . $formarly . "Attribute"}(); } else if(method_exists(__CLASS__, "getAttribute")) { diff --git a/Core/Response.php b/Core/Response.php index 79f643f..55a8ae2 100644 --- a/Core/Response.php +++ b/Core/Response.php @@ -57,7 +57,14 @@ Response::Text($content); break; } - case "object": + case "object":{ + if(method_exists($content, "__toString")) + { + return Response::Text($content->__toString()); + }else{ + return Response::Json($content); + } + } case "array":{ Response::Json($content); break;