Skip to content

Commit

Permalink
Added features to ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
andrevanzuydam committed Jan 16, 2020
1 parent ee6cccb commit e9795d6
Show file tree
Hide file tree
Showing 12 changed files with 2,645 additions and 277 deletions.
63 changes: 49 additions & 14 deletions Tina4/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

namespace Tina4;


/**
* Class ORM
* A very simple ORM for reading and writing data to a database or just for a simple NO SQL solution
* @package Tina4
*/
class ORM
class ORM implements \JsonSerializable
{
/**
* @var string The primary key fields in the table, can have more than one separated by comma e.g. store_id,company_id
Expand Down Expand Up @@ -52,6 +51,20 @@ class ORM
*/
function __construct($request = null, $tableName = "", $fieldMapping = "", $primaryKey = "", $tableFilter = "", $DBA = null)
{
$this->create ($request = null, $tableName = "", $fieldMapping = "", $primaryKey = "", $tableFilter = "", $DBA = null);
}

/**
* Allows ORM's to be created
* @param null $request
* @param string $tableName
* @param string $fieldMapping
* @param string $primaryKey
* @param string $tableFilter
* @param null $DBA
* @throws \Exception
*/
function create($request = null, $tableName = "", $fieldMapping = "", $primaryKey = "", $tableFilter = "", $DBA = null) {
if (!empty($request) && !is_object($request) && !is_array($request) && !json_decode($request)) {
throw new \Exception("Input is not an array or object");
}
Expand Down Expand Up @@ -84,7 +97,7 @@ function __construct($request = null, $tableName = "", $fieldMapping = "", $prim
}

if ($request) {
if (json_decode($request)) {
if (!is_array($request) && !is_object($request) && json_decode($request)) {
$request = json_decode($request);
foreach ($request as $key => $value) {
$this->{$key} = $value;
Expand Down Expand Up @@ -116,10 +129,10 @@ function getFieldName($name, $fieldMapping = [])
if (property_exists($this, $name)) return $name;
$fieldName = "";
for ($i = 0; $i < strlen($name); $i++) {
if (ctype_upper($name{$i})) {
$fieldName .= "_" . $name{$i};
if (ctype_upper($name[$i])) {
$fieldName .= "_" . $name[$i];
} else {
$fieldName .= $name{$i};
$fieldName .= $name[$i];
}
}
return strtolower($fieldName);
Expand All @@ -145,19 +158,19 @@ function getObjectName($name)
if (strpos($name, "_") !== false) {
$name = strtolower($name);
for ($i = 0; $i < strlen($name); $i++) {
if ($name{$i} === "_") {
if ($name[$i] === "_") {
$i++;
$fieldName .= strtoupper($name{$i});
$fieldName .= strtoupper($name[$i]);
} else {
$fieldName .= $name{$i};
$fieldName .= $name[$i];
}
}
} else {
for ($i = 0; $i < strlen($name); $i++) {
if ($name{$i} !== strtolower($name{$i})) {
$fieldName .= "_" . strtolower($name{$i});
if ($name[$i] !== strtolower($name[$i])) {
$fieldName .= "_" . strtolower($name[$i]);
} else {
$fieldName .= $name{$i};
$fieldName .= $name[$i];
}
}
}
Expand Down Expand Up @@ -256,14 +269,13 @@ function generateDeleteSQL($filter, $tableName = "")
*/
function getTableData($fieldMapping = [])
{
$data = json_decode(json_encode($this));
$tableData = [];
if (!empty($this->fieldMapping) && empty($fieldMapping)) {
$fieldMapping = $this->fieldMapping;
}

$protectedFields = ["primaryKey", "tableFilter", "DBA", "tableName", "fieldMapping"];
foreach ($data as $fieldName => $value) {
foreach ($this as $fieldName => $value) {
if (!in_array($fieldName, $protectedFields)) {
if (empty($this->primaryKey)) { //use first field as primary if not specified
$this->primaryKey = $this->getFieldName($fieldName, $fieldMapping);
Expand Down Expand Up @@ -534,5 +546,28 @@ function __toString()
return json_encode($this->getTableData());
}

/**
* Makes a neat JSON response
*/
public function jsonSerialize() {
return $this->getTableData();
}

/**
* Selects a data set of records
* @param string $fields
* @param int $limit
* @return SQL
*/
public function select($fields="*", $limit=10) {
return (new \Tina4\SQL($this))->select($fields, $limit)->from($this->tableName);
}

public function generateForm($columns=1, $ignoreFields=null, $namePrefix="", $groupClass="form-group", $inputClass="form-control") {
$html = "coming";

return $html;
}


}
3 changes: 2 additions & 1 deletion Tina4/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ function getSwagger($title = "Tina4", $description = "Swagger Documentation", $v
$queryParams = explode(",", $matches[2]);
} else
if ($matches[1] === "@example") {
eval(' if (class_exists("' . trim(str_replace("\n", "", $matches[2])) . '")) { $example = (new ' . trim(str_replace("\n", "", $matches[2])) . '()); if (method_exists($example, "getTableData")) { $example = (object)$example->getTableData(); } else { $example = json_decode (json_encode($example)); } } else {$example = (object)[];} ');
eval(' if (class_exists("' . trim(str_replace("\n", "", $matches[2])) . '")) { $example = (new ' . trim(str_replace("\n", "", $matches[2])) . '()); if (method_exists($example, "getTableData")) { $example = (object)$example->getTableData(); } else { $example = json_decode (json_encode($example)); } } else { $example = (object)[];} ');

} else
if ($matches[1] === "@secure") {
$addParams[] = (object)["name" => "Authorization", "in" => "header", "required" => false];
Expand Down
155 changes: 155 additions & 0 deletions Tina4/SQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Tina4;

class SQL implements \JsonSerializable
{
public $ORM;
public $fields;
public $tableName;
public $join;
public $filter;
public $groupBy;
public $having;
public $orderBy;
public $nextAnd="join";
public $limit;

public function __construct($ORM)
{
$this->ORM = clone $ORM;
}

public function select($fields="*", $limit=10)
{
if (is_array($fields)) {
$this->fields = $fields;
} else {
$this->fields = explode(",", $fields);
}
$this->limit = $limit;
return $this;
}

function from($tableName) {
$this->tableName = $tableName;
return $this;
}

function where ($filter) {
$this->nextAnd = "where";
$this->filter[] = ["where", $filter];
return $this;
}

function and ($filter) {
if ($this->nextAnd == "join") {
$this->join[] = ["and", $filter];
}else if ($this->nextAnd == "having") {
$this->having[] = ["and", $filter];
}
else {
$this->filter[] = ["and", $filter];
}
return $this;
}

function or ($filter) {
$this->filter[] = ["or", $filter];
return $this;
}

function join ($tableName) {
$this->nextAnd = "join";
$this->join[] = ["join", tableName];
return $this;
}

function leftJoin ($tableName) {
$this->nextAnd = "join";
$this->join[] = ["left join", tableName];
return $this;
}

function on ($filter) {
$this->join[] = ["on", $filter];
return $this;
}

function groupBy ($fields) {
if (is_array($fields)) {
$this->groupBy = $fields;
} else {
$this->groupBy = explode(",", $fields);
}
return $this;
}

function having ($filter) {
$this->nextAnd = "having";
$this->having[] = ["having", $filter];
return $this;
}

function orderBy ($fields) {
if (is_array($fields)) {
$this->orderBy = $fields;
} else {
$this->orderBy = explode(",", $fields);
}
return $this;
}

function generateSQLStatement() {
$sql = "select\t".join(",\n\t", $this->fields)."\nfrom\t{$this->tableName}\n";
if (!empty($this->join) && is_array($this->join)) {
foreach ($this->join as $join) {
$sql .= $join[0]. " ".$join[1];
}
}

if (!empty($this->filter) && is_array($this->filter)) {
foreach ($this->filter as $filter) {
$sql .= "".$filter[0]. "\t".$filter[1]."\n";
}
}

if (!empty($this->groupBy) && is_array($this->groupBy)) {
$sql .= "group by ".join (",", $this->groupBy)."\n";
}

if (!empty($this->having) && is_array($this->having)) {
foreach ($this->having as $filter) {
$sql .= $filter[0]. "\t".$filter[1]."\n";
}
}

return $sql;
}

/**
* Makes a neat JSON response
*/
public function jsonSerialize() {
//run the query
$sqlStatement = $this->generateSQLStatement();
if (!empty($this->ORM) && !empty($this->ORM->DBA)) {
$result = $this->ORM->DBA->fetch ($sqlStatement, $this->limit)->records();
$records = [];
//transform the records into an array of the ORM
foreach ($result as $id => $data) {
$record = clone $this->ORM;
$record->create($data);
$record->id = $id;
$records[] = $record;
}
} else {
$records = ["error" => "No database connection or ORM specified"];
}


return $records;
}


}
Loading

0 comments on commit e9795d6

Please sign in to comment.