Files
orm/src/ORM/Entity.php
T
2022-04-05 20:03:21 +00:00

631 lines
21 KiB
PHP
Executable File

<?php
namespace ORM;
abstract class Entity {
const _tableName = '';
const _connectionName = 'default';
const _softdelete = false;
const _ignore = Array(
'classname',
'_properties',
'_ignore',
'_connectionName',
'_timestamps',
'_softdelete',
'_tableName',
'_idPolice',
'_ignore'
);
const _properties = Array();
/*
* [type] = nextval| manual | empty is the default
* [max] = maximum value allowed
* [min] = minumul value allowed
* [step] = increment by n each id
*/
const _idPolice = Array();
function __construct($assignment = '') {
@$this->_ignore = array_merge($this->_ignore, self::_ignore);
if (isset($this->_timestamps) && $this->_timestamps) {
$this->created_at = date('Y-m-d h:m:s');
$this->updated_at = date('Y-m-d h:m:s');
}
// Mass Assign
if(is_array($assignment)){
$this->charge($assignment);
}
}
public function __set($property, $value) {
if (in_array($property, static::_ignore)) {
return;
}
$this->$property = $value;
}
public function __get($key) {
if (!isset($this->$key)) {
return 0;
}
return $this->$key;
}
public function __toString() {
if (isset($this->name)) {
return $this->name;
}
if (isset($this->nome)) {
return $this->nome;
}
return static::class;
}
/*public function __call($name, $arguments){
dd($name);
}*/
public function charge($payload) {
$properties = [];
foreach( get_object_vars($this) as $key => $property){
if(substr($key, 0, 1) != '_'){
$properties[] = $key;
}
}
$elements = array_diff(array_merge($properties, static::_properties), self::_ignore);
foreach ($elements as $key => $property) {
if ($property == 'id' && !isset($payload[$property])) {
continue;
}
if(empty($payload[$property])){
continue;
}
$this->$property = $payload[$property];
}
return [];
}
public static function get_properties() {
$properties = Array();
foreach (static::_properties as $propertie) {
$obj = new \stdClass();
$obj->data = $propertie;
$properties[] = $obj;
}
return json_encode($properties);
}
public function save() {
if (isset($this->id)) {
$this->update();
} else {
$this->create();
}
}
private function update() {
//First check if the record exist. If not, we might have a idPolice
if (!DBInstance::queryOne("SELECT id FROM {" . static::_tableName . "} WHERE id = ?", Array($this->id))) {
$this->create();
return;
}
$updatekeys = static::_ignore;
$updatekeys[] = 'id';
$columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
$obj = (array) $this;
$update_string = '';
$update_data = Array();
$update_data['_update_id'] = $this->id;
if (isset($this->_timestamps) && $this->_timestamps) {
$this->updated_at = date('Y-m-d h:m:s');
}
foreach ($columns as $column) {
$update_string .= $column . " = :$column, ";
$update_data[$column] = $obj[$column];
}
$update_string = rtrim($update_string, ', ');
$sql = "UPDATE {" . static::_tableName . "} SET $update_string WHERE id = :_update_id";
DBInstance::execute($sql, $update_data, static::_connectionName);
}
private function create() {
//First, let's verify id polices
//Manual and empty don't need special treatments
if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') {
//Get the actual max value of the key
$maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::_tableName . "}");
$maxVal = ($maxVal) ? $maxVal : 0;
// If the next val is smaller than the minumum value
if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id + 1) {
// The id value shpuld assume the min value
$this->id = static::_idPolice['min'];
} else {
// The id is bigger than the min or min is not set
if (isset(static::_idPolice['step'])) {
//If sted is defined, increment by step multiples
$this->id = $maxVal->id + static::_idPolice['step'];
} else {
$this->id = $maxVal->id + 1;
}
}
//If the id is bigger than the max value. There is problems
if (isset(static::_idPolice['max']) && $maxVal->id > static::_idPolice['max']) {
//Do not store the register
return false;
}
}
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
$obj = (array) $this;
$first_argument = '';
$second_argument = '';
$insert_data = Array();
foreach ($columns as $column) {
$first_argument .= $column . ', ';
$insert_data[$column] = $obj[$column];
$second_argument .= ":$column, ";
}
$first_argument = rtrim($first_argument, ', ');
$second_argument = rtrim($second_argument, ', ');
$sql = "INSERT INTO {" . static::_tableName . "} ($first_argument) VALUES ($second_argument)";
DBInstance::execute($sql, $insert_data, static::_connectionName);
// If there is a id pollice applied, do not get the inserted id
if (empty(static::_idPolice)) {
$this->id = DBInstance::lastInsert();
}
}
public function load($id = false) {
if (!$id) {
return;
}
$sql = "SELECT * FROM {" . static::_tableName . "} WHERE id = ?";
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]);
}
public function delete() {
if (isset($this->_softdelete) && $this->_softdelete) {
$this->deleted_at = date('Y-m-d h:m:s');
$this->update();
}else{
$this->purge();
}
}
public function purge() {
$sql = "DELETE FROM {" . static::_tableName . "} WHERE id = :id";
DBInstance::execute($sql, Array('id' => $this->id), static::_connectionName);
}
public function restore(){
if (isset($this->_softdelete) && $this->_softdelete) {
$this->deleted_at = null;
$this->update();
}else{
throw new \Exception('Not soft deleteble');
}
}
private function fill($data) {
$data = (array) $data;
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
/**
* Find All
*
* This method will load all instances from the class.
* Limits are avaliable for paginations.
*
* @param Array $select Columns to select. Array('id', 'name')
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
* @param boolean $trashed Bring trashed elements?
*/
public static function findAll($select = Array('*'), $limits = Array(), $trashed = false) : Collection {
$criteria = '';
$limits_sql = '';
foreach ($select as $value) {
$criteria .= $value . ', ';
}
$criteria = rtrim($criteria, ', ');
foreach ($limits as $key => $value) {
$limits_sql .= "$key $value ";
}
if (static::_softdelete && !$trashed) {
$sql = "SELECT $criteria FROM {" . static::_tableName . "} WHERE deleted_at is null $limits_sql";
} else {
$sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
}
$results = DBInstance::query($sql, Array(), static::_connectionName);
$objects = new Collection;
foreach ($results as $value) {
$static = static::class;
$object = new $static;
$object->fill($value);
$objects->addItem($object);
}
return $objects;
}
/**
* Count
*
* Count how many records there is on the database
* in relation with this class with the parameters filter
*
* @param Array $criterias Criteras for WHERE Array('id' => Array('=', 10) )
* @param boolean $trashed Bring trashed registers?
*
*/
public static function count($criterias = Array(), $trashed = false) {
$criteria_sql = "";
$criteria_data = Array();
foreach ($criterias as $key => $criteria) {
if ($criteria_sql != "") {
$criteria_sql .= " AND ";
} else {
$criteria_sql .= " WHERE ";
}
if (is_array($criteria[1])) {
$crit_temp = '';
foreach ($criteria[1] as $crit) {
$crit_temp .= '?, ';
$criteria_data[] = $crit;
}
$crit_temp = rtrim($crit_temp, ', ');
$criteria_sql .= "$key $criteria[0] ($crit_temp)";
} else {
$criteria_sql .= "$key $criteria[0] (?) ";
$criteria_data[] = $criteria[1];
}
}
if (static::_softdelete && !$trashed) {
$sql = "SELECT COUNT(*) FROM {" . static::_tableName . "} $criteria_sql AND deleted_at is null";
} else {
$sql = "SELECT COUNT(*) FROM {" . static::_tableName . "} $criteria_sql";
}
$results = DBInstance::queryOne($sql, $criteria_data, static::_connectionName);
return $results->count;
}
/**
* Find One
*
* @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30)))
* @param Array $select Array(id, fullname)
* @param Boolan $trashed true, false
*/
public static function findOne($criterias = Array(), $select = Array('*'), $trashed = false) {
if(empty($criterias)){
$criterias = Array(1 => ['=', 1]);
}
$select_sql = "";
$criteria_sql = "";
$limits_sql = "";
$criteria_data = Array();
$limits = Array("LIMIT" => 1);
if (is_numeric($criterias)) {
$criteria_sql = "WHERE id = ? ";
$criteria_data[] = $criterias;
} else {
foreach ($criterias as $key => $criteria) {
if ($criteria_sql != "") {
$criteria_sql .= " AND ";
} else if ($criteria_sql == "") {
$criteria_sql .= " WHERE ";
} else {
$criteria_sql .= " ";
}
if (is_array($criteria[1])) {
$crit_temp = '';
foreach ($criteria[1] as $crit) {
$crit_temp .= '?, ';
$criteria_data[] = $crit;
}
$crit_temp = rtrim($crit_temp, ', ');
$criteria_sql .= "$key $criteria[0] ($crit_temp)";
} else {
$criteria_sql .= "$key $criteria[0] (?) ";
$criteria_data[] = $criteria[1];
}
}
}
foreach ($select as $value) {
$select_sql .= $value . ', ';
}
$select_sql = rtrim($select_sql, ', ');
foreach ($limits as $key => $value) {
$limits_sql .= "$key $value ";
}
if (static::_softdelete && !$trashed) {
$sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql AND deleted_at is null $limits_sql";
} else {
$sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql";
}
$results = DBInstance::query($sql, $criteria_data, static::_connectionName);
$objects = new Collection();
foreach ($results as $value) {
$class = static::class;
$object = new $class;
$object->fill($value);
$objects->addItem($object);
}
if (empty($objects)) {
return false; //new $this->classname;
}
return $objects[0];
}
/**
* Find Many
*
* @param Array $criterias Description
* @param Array $select Description
* @param Array $limits Description
* @param boolean $trashed Description
*
*/
public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
$select_sql = "";
$criteria_sql = "";
$limits_sql = "";
foreach ($limits as $key => $value) {
$limits_sql .= "$key $value ";
}
foreach ($criterias as $key => $criteria) {
if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') {
$criteria_sql .= " ";
} else if ($criteria_sql != "" ) {
$criteria_sql .= " ";
} else if ($criteria_sql != "" ) {
$criteria_sql .= " AND ";
} else if ($criteria_sql == "") {
$criteria_sql .= " WHERE ";
} else {
$criteria_sql .= " ";
}
if (substr($criteria[0], 0, 2) == "OR") {
$criteria[0] = str_replace('OR', '', $criteria[0]);
$criteria_sql .= "OR $key $criteria[0] ('%$criteria[1]%')";
} else if (is_string($criteria[1])) {
$criteria_sql .= "$key $criteria[0] '$criteria[1]'";
} else if (is_array($criteria[1])) {
$criteria_sql .= "$key $criteria[0] (" . implode(', ', $criteria[1]) . ")";
} else {
$criteria_sql .= "$key $criteria[0] $criteria[1]";
}
}
$criteria_sql = str_replace('WHERE OR', 'WHERE ', $criteria_sql);
foreach ($select as $value) {
$select_sql .= $value . ', ';
}
$select_sql = rtrim($select_sql, ', ');
if (static::_softdelete && !$trashed) {
$sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql AND deleted_at is null $limits_sql";
} else {
$sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql";
}
$results = DBInstance::query($sql, Array(), static::_connectionName);
$objects = new Collection();
foreach ($results as $value) {
$class = static::class;
$object = new $class;
$object->fill($value);
$objects->addItem($object);
}
if (empty($objects)) {
return Array();
}
return $objects;
}
/**
* Has One Local
*
* Defines that, this object has a child an only one,
* object in other class.
*
* Table user = (id, name)
* Table phone = (id, number, userid)
*
* This relation will be created on the User class
* making reference to the Phone class
*
* @ctag $this->hasOne('foreign_object', 'field_in_remote');
*
* @param Object $foreign_object Class instance from the remote object
* @param (int, string) $field_in_remote Field in local object matchin the remote id
*
* @return Object Instance of the remote class
*
*/
protected function hasOne($foreign_object, $field_in_remote) {
return new HasOne($foreign_object, $field_in_remote, $this);
}
/**
* Has Many
*
* Defines that, this object has many instances of other Object
*
* Table Post = (id, name, content)
* Table Comment = (id, name, content, postid)
*
* This relation will be created on the Post class
* Making reference to the Comment class
*
* @ctag $this->hasMany('foreign_object', 'field_in_foreign');
*
* @param type $foreign_object Instance of a remote class
* @param (int, string) $field_in_foreign The field to match the local id
*
*/
protected function hasMany($foreign_object, $field_in_foreign) {
return new HasMany($foreign_object, $field_in_foreign, $this);
}
/**
* Belongs To
*
* Defines that, this object is part of an other class.
* The local field must match the id of an other class.
*
* Table Post = (id, name, content)
* Table Comment = (id, name, content, postid)
*
* This relation will be created on the comment class
* Making reference to the post class
*
* @ctag $this->belongsTo('foreign_object', 'local_field');
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
*
* @param Object $foreign_object Instance of a remote class
* @param (int, String) $local_field Remote field relate to local Object
* @param string $remote_field
*/
protected function belongsTo($foreignObject, $localField, $remoteField = 'id') {
return new BelongsTo($foreignObject, $localField, $remoteField, $this);
}
/**
* Belongs to Many
*
* Defines that this object is related to many other instances
* of other classes throw a pivot table.
*
* Table Post = (id, name, content)
* Table Tag = (id, name)
* Table Post_Tag = (id, postid, tagid)
*
* This relation will be created in both classes
*
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array());
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $remote_limit = Array());
*
* @param Object $foreign_object Instance of the remote class
* @param string $pivot_table Name of the pivot table
* @param int $local_in_pivot Name of field on the pivot in relation of the local class
* @param int $remote_in_pivot Name of field on the pivot in relation of the remote class
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
* @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 )
*/
protected function belongsToMany($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
return new BelongsToMany($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $remote_limit);
}
/**
* Belongs to Many Extended
*
* Defines that this object is related to many other instances
* of other classes throw a pivot table.
* This relation will bring the pivot table with the elements inside
*
* Table Post = (id, name, content)
* Table Tag = (id, name)
* Table Post_Tag = (id, postid, tagid)
*
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array());
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $pivot_limits = Array());
*
* This relation will be created in both classes
*
* @param Object $foreign_object Instance of the remote class
* @param string $pivot_table Name of the pivot table
* @param int $local_in_pivot Name of field on the pivot in relation of the local class
* @param int $remote_in_pivot Name of field on the pivot in relation of the remote class
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
* @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 )
*/
protected function belongsToManyExtended($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limit = Array()) {
return new BelongsToManyExtended($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $pivot_limit);
}
/**
* With
*
* Eager load relations on this entity
*
**/
public function with($relations = null, $propagate = null){
if(is_string($relations)){
$relations = [$relations];
}
if(!$relations){
return $this;
}
$possibleRelations = get_class_methods(static::class);
foreach($relations as $relation){
if(in_array($relation, $possibleRelations)){
$this->$relation = $this->$relation()->get();
}
if($this->$relation){
$this->$relation->with($propagate);
}
}
return $this;
}
}