Some identation, documentation and minor changes

This commit is contained in:
2026-08-19 10:58:35 -03:00
parent ac04cd02f4
commit 64bce1da47
+167 -124
View File
@@ -2,12 +2,19 @@
namespace ORM; namespace ORM;
abstract class Entity { use ORM\Relations\HasOne;
use ORM\Relations\HasMany;
use ORM\Relations\BelongsTo;
use ORM\Relations\BelongsToMany;
use ORM\Relations\BelongsToManyExtended;
abstract class Entity
{
const _tableName = ''; const _tableName = '';
const _connectionName = 'default'; const _connectionName = 'default';
const _softdelete = false; const _softdelete = false;
const _ignore = Array( const _ignore = array(
'classname', 'classname',
'_properties', '_properties',
'_ignore', '_ignore',
@@ -18,7 +25,7 @@ abstract class Entity {
'_idPolice', '_idPolice',
'_ignore' '_ignore'
); );
const _properties = Array(); const _properties = array();
/* /*
* [type] = nextval| manual | empty is the default * [type] = nextval| manual | empty is the default
@@ -26,9 +33,10 @@ abstract class Entity {
* [min] = minumul value allowed * [min] = minumul value allowed
* [step] = increment by n each id * [step] = increment by n each id
*/ */
const _idPolice = Array(); const _idPolice = array();
function __construct($assignment = '') { function __construct($assignment = '')
{
@$this->_ignore = array_merge((array) $this->_ignore, self::_ignore); @$this->_ignore = array_merge((array) $this->_ignore, self::_ignore);
@@ -38,21 +46,23 @@ abstract class Entity {
} }
// Mass Assign // Mass Assign
if(is_array($assignment)){ if (is_array($assignment)) {
$this->charge($assignment); $this->charge($assignment);
} }
} }
public function __set($property, $value) { public function __set($property, $value)
{
if (in_array($property, static::_ignore)) { if (in_array($property, static::_ignore)) {
return; return;
} }
$this->$property = $value; $this->$property = $value;
} }
public function __get($key) { public function __get($key)
{
// The __get might be a connection. So return it // The __get might be a connection. So return it
if(method_exists($this, $key)){ if (method_exists($this, $key)) {
return $this->$key(); return $this->$key();
} }
@@ -65,7 +75,8 @@ abstract class Entity {
return $this->$key; return $this->$key;
} }
public function __toString() { public function __toString()
{
if (isset($this->name)) { if (isset($this->name)) {
return $this->name; return $this->name;
} }
@@ -75,16 +86,15 @@ abstract class Entity {
return static::class; return static::class;
} }
/*public function __call($name, $arguments){ // public function __call($name, $arguments){ }
dd($name);
}*/
public function charge($payload) { public function charge($payload)
{
$properties = []; $properties = [];
foreach( get_object_vars($this) as $key => $property){ foreach (get_object_vars($this) as $key => $property) {
if(substr($key, 0, 1) != '_'){ if (substr($key, 0, 1) != '_') {
$properties[] = $key; $properties[] = $key;
} }
} }
@@ -95,7 +105,7 @@ abstract class Entity {
if ($property == 'id' && !isset($payload[$property])) { if ($property == 'id' && !isset($payload[$property])) {
continue; continue;
} }
if(empty($payload[$property])){ if (empty($payload[$property])) {
continue; continue;
} }
$this->$property = $payload[$property]; $this->$property = $payload[$property];
@@ -103,8 +113,9 @@ abstract class Entity {
return []; return [];
} }
public static function get_properties() { public static function get_properties()
$properties = Array(); {
$properties = array();
foreach (static::_properties as $propertie) { foreach (static::_properties as $propertie) {
$obj = new \stdClass(); $obj = new \stdClass();
$obj->data = $propertie; $obj->data = $propertie;
@@ -113,7 +124,8 @@ abstract class Entity {
return json_encode($properties); return json_encode($properties);
} }
public function save() { public function save()
{
if (isset($this->id)) { if (isset($this->id)) {
$this->update(); $this->update();
} else { } else {
@@ -121,9 +133,10 @@ abstract class Entity {
} }
} }
private function update() { private function update()
{
//First check if the record exist. If not, we might have a idPolice //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))) { if (!DBInstance::queryOne("SELECT id FROM {" . static::_tableName . "} WHERE id = ?", array($this->id))) {
$this->create(); $this->create();
return; return;
} }
@@ -134,7 +147,7 @@ abstract class Entity {
$obj = (array) $this; $obj = (array) $this;
$update_string = ''; $update_string = '';
$update_data = Array(); $update_data = array();
$update_data['_update_id'] = $this->id; $update_data['_update_id'] = $this->id;
if (isset($this->_timestamps) && $this->_timestamps) { if (isset($this->_timestamps) && $this->_timestamps) {
@@ -152,7 +165,8 @@ abstract class Entity {
DBInstance::execute($sql, $update_data, static::_connectionName); DBInstance::execute($sql, $update_data, static::_connectionName);
} }
private function create() { private function create()
{
//First, let's verify id polices //First, let's verify id polices
//Manual and empty don't need special treatments //Manual and empty don't need special treatments
if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') { if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') {
@@ -185,7 +199,7 @@ abstract class Entity {
$first_argument = ''; $first_argument = '';
$second_argument = ''; $second_argument = '';
$insert_data = Array(); $insert_data = array();
foreach ($columns as $column) { foreach ($columns as $column) {
$first_argument .= $column . ', '; $first_argument .= $column . ', ';
@@ -207,40 +221,45 @@ abstract class Entity {
} }
public function load($id = false) { public function load($id = false)
{
if (!$id) { if (!$id) {
return; return;
} }
$sql = "SELECT * FROM {" . static::_tableName . "} WHERE id = ?"; $sql = "SELECT * FROM {" . static::_tableName . "} WHERE id = ?";
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]); $this->fill(DBInstance::queryPrepare($sql, array($id), static::_connectionName)[0]);
} }
public function delete() { public function delete()
{
if (static::_softdelete) { if (static::_softdelete) {
$this->deleted_at = date('Y-m-d h:m:s'); $this->deleted_at = date('Y-m-d h:m:s');
$this->update(); $this->update();
}else{ } else {
$this->purge(); $this->purge();
} }
} }
public function purge() { public function purge()
{
$sql = "DELETE FROM {" . static::_tableName . "} WHERE id = :id"; $sql = "DELETE FROM {" . static::_tableName . "} WHERE id = :id";
DBInstance::execute($sql, Array('id' => $this->id), static::_connectionName); DBInstance::execute($sql, array('id' => $this->id), static::_connectionName);
} }
public function restore(){ public function restore()
{
if (isset($this->_softdelete) && $this->_softdelete) { if (isset($this->_softdelete) && $this->_softdelete) {
$this->deleted_at = null; $this->deleted_at = null;
$this->update(); $this->update();
}else{ } else {
throw new \Exception('Not soft deleteble'); throw new \Exception('Not soft deleteble');
} }
} }
private function fill($data) { private function fill($data)
{
$data = (array) $data; $data = (array) $data;
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
@@ -258,7 +277,8 @@ abstract class Entity {
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 ) * @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
* @param boolean $trashed Bring trashed elements? * @param boolean $trashed Bring trashed elements?
*/ */
public static function findAll($select = Array('*'), $limits = Array(), $trashed = false) : Collection { public static function findAll($select = array('*'), $limits = array(), $trashed = false): Collection
{
$criteria = ''; $criteria = '';
$limits_sql = ''; $limits_sql = '';
@@ -278,7 +298,7 @@ abstract class Entity {
$sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql"; $sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
} }
$results = DBInstance::query($sql, Array(), static::_connectionName); $results = DBInstance::query($sql, array(), static::_connectionName);
$objects = new Collection; $objects = new Collection;
foreach ($results as $value) { foreach ($results as $value) {
@@ -300,9 +320,10 @@ abstract class Entity {
* @param boolean $trashed Bring trashed registers? * @param boolean $trashed Bring trashed registers?
* *
*/ */
public static function count($criterias = Array(), $trashed = false) { public static function count($criterias = array(), $trashed = false)
{
$criteria_sql = ""; $criteria_sql = "";
$criteria_data = Array(); $criteria_data = array();
foreach ($criterias as $key => $criteria) { foreach ($criterias as $key => $criteria) {
if ($criteria_sql != "") { if ($criteria_sql != "") {
$criteria_sql .= " AND "; $criteria_sql .= " AND ";
@@ -335,48 +356,58 @@ abstract class Entity {
/** /**
* Find One * Find One
* *
* @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30))) * @param Int|String|Array $criterias Array('id' => Array('in', Array(10, 20, 30)))
* @param Array $select Array(id, fullname) * @param Array $select Array(id, fullname)
* @param Boolan $trashed true, false * @param bool $trashed true, false
*/ */
public static function findOne($criterias = Array(), $select = Array('*'), $trashed = false) { public static function findOne(
if(empty($criterias)){ int|string|array $criterias = array(),
$criterias = Array(1 => ['=', 1]); array $select = array('*'),
bool $trashed = false
) {
if (empty($criterias)) {
$criterias = array(1 => ['=', 1]);
} }
$select_sql = ""; $select_sql = "";
$criteria_sql = ""; $criteria_sql = "";
$limits_sql = ""; $limits_sql = "";
$criteria_data = Array(); $criteria_data = array();
$limits = Array("LIMIT" => 1); $limits = array("LIMIT" => 1);
if (is_numeric($criterias)) { if (!is_array($criterias)) {
$criteria_sql = "WHERE id = ? "; $criteria_sql = "WHERE id = ? ";
$criteria_data[] = $criterias; $criteria_data[] = $criterias;
} else { } else {
foreach ($criterias as $key => $criteria) { if (!array_is_list($criterias)) {
if ($criteria_sql != "") { foreach ($criterias as $key => $criteria) {
$criteria_sql .= " AND "; if ($criteria_sql != "") {
} else if ($criteria_sql == "") { $criteria_sql .= " AND ";
$criteria_sql .= " WHERE "; } else if ($criteria_sql == "") {
} else { $criteria_sql .= " WHERE ";
$criteria_sql .= " "; } else {
} $criteria_sql .= " ";
if (is_array($criteria[1])) { }
$crit_temp = ''; if (is_array($criteria[1])) {
foreach ($criteria[1] as $crit) { $crit_temp = '';
$crit_temp .= '?, '; foreach ($criteria[1] as $crit) {
$criteria_data[] = $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];
} }
$crit_temp = rtrim($crit_temp, ', ');
$criteria_sql .= "$key $criteria[0] ($crit_temp)";
} else {
$criteria_sql .= "$key $criteria[0] (?) ";
$criteria_data[] = $criteria[1];
} }
} else {
$criteria_sql = "WHERE {$criterias[0]} = ? ";
$criteria_data[] = $criterias[1];
} }
} }
foreach ($select as $value) { foreach ($select as $value) {
$select_sql .= $value . ', '; $select_sql .= $value . ', ';
} }
@@ -418,10 +449,13 @@ abstract class Entity {
* @param boolean $trashed Description * @param boolean $trashed Description
* *
*/ */
public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) { public static function findMany(
$select_sql = ""; $criterias = array(),
$criteria_sql = ""; $select = array('*'),
$limits_sql = ""; $limits = array(),
$trashed = false
) {
$select_sql = $criteria_sql = $limits_sql = "";
foreach ($limits as $key => $value) { foreach ($limits as $key => $value) {
$limits_sql .= "$key $value "; $limits_sql .= "$key $value ";
@@ -430,9 +464,9 @@ abstract class Entity {
foreach ($criterias as $key => $criteria) { foreach ($criterias as $key => $criteria) {
if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') { if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') {
$criteria_sql .= " "; $criteria_sql .= " ";
} else if ($criteria_sql != "" ) { } else if ($criteria_sql != "") {
$criteria_sql .= " "; $criteria_sql .= " ";
} else if ($criteria_sql != "" ) { } else if ($criteria_sql != "") {
$criteria_sql .= " AND "; $criteria_sql .= " AND ";
} else if ($criteria_sql == "") { } else if ($criteria_sql == "") {
$criteria_sql .= " WHERE "; $criteria_sql .= " WHERE ";
@@ -466,7 +500,7 @@ abstract class Entity {
$sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql"; $sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql";
} }
$results = DBInstance::query($sql, Array(), static::_connectionName); $results = DBInstance::query($sql, array(), static::_connectionName);
$objects = new Collection(); $objects = new Collection();
foreach ($results as $value) { foreach ($results as $value) {
@@ -477,14 +511,14 @@ abstract class Entity {
} }
if (empty($objects)) { if (empty($objects)) {
return Array(); return array();
} }
return $objects; return $objects;
} }
/** /**
* Has One Local * Has One
* *
* Defines that, this object has a child an only one, * Defines that, this object has a child an only one,
* object in other class. * object in other class.
@@ -495,15 +529,14 @@ abstract class Entity {
* This relation will be created on the User class * This relation will be created on the User class
* making reference to the Phone 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 object $foreign_object Class instance from the remote object
* @param (int, string) $field_in_remote Field in local object matchin the remote id * @param (int, string) $field_in_remote Field in local object matchin the remote id
* *
* @return object Instance of the remote class * @return object Instance of the remote class
* *
*/ */
protected function hasOne($foreign_object, $field_in_remote) { protected function hasOne($foreign_object, $field_in_remote)
{
return new HasOne($foreign_object, $field_in_remote, $this); return new HasOne($foreign_object, $field_in_remote, $this);
} }
@@ -518,14 +551,13 @@ abstract class Entity {
* This relation will be created on the Post class * This relation will be created on the Post class
* Making reference to the Comment class * Making reference to the Comment class
* *
* @ctag $this->hasMany('foreign_object', 'field_in_foreign'); * @param string $foreignObject Instance of a remote class
* * @param int|string $fieldInForeign The field to match the local id
* @param Entity $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) { protected function hasMany(string $foreignObject, int|string $fieldInForeign)
return new HasMany($foreign_object, $field_in_foreign, $this); {
return new HasMany($foreignObject, $fieldInForeign, $this);
} }
/** /**
@@ -540,14 +572,12 @@ abstract class Entity {
* This relation will be created on the comment class * This relation will be created on the comment class
* Making reference to the post class * Making reference to the post class
* *
* @ctag $this->belongsTo('foreign_object', 'local_field'); * @param string $foreignObject Instance of a remote class
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id'); * @param (int, String) $localField Remote field relate to local Object
* * @param string $remoteField
* @param Entity $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') { protected function belongsTo(string $foreignObject, $localField, $remoteField = 'id')
{
return new BelongsTo($foreignObject, $localField, $remoteField, $this); return new BelongsTo($foreignObject, $localField, $remoteField, $this);
} }
@@ -563,19 +593,22 @@ abstract class Entity {
* *
* This relation will be created in both classes * This relation will be created in both classes
* *
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot'); * @param Object $foreignObject Instance of the remote class
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array()); * @param string $pivotTable Name of the pivot table
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $remote_limit = Array()); * @param string $localInPivot Name of field on the pivot in relation of the local class
* * @param int|string $remoteInPivot Name of field on the pivot in relation of the remote class
* @param Object $foreign_object Instance of the remote class * @param Array $remoteFilter Filters to the remote Array('id', Array('>', 50) )
* @param string $pivot_table Name of the pivot table * @param Array $remoteLimit Array( 'offset'=> 10, 'limit' => 10 )
* @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()) { protected function belongsToMany(
return new BelongsToMany($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $remote_limit); string $foreignObject,
string $pivotTable,
string $localInPivot,
int|string $remoteInPivot,
array $remoteFilter = array(),
array $remoteLimit = array()
) {
return new BelongsToMany($this, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter, $remoteLimit);
} }
/** /**
@@ -589,45 +622,55 @@ abstract class Entity {
* Table Tag = (id, name) * Table Tag = (id, name)
* Table Post_Tag = (id, postid, tagid) * 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 * This relation will be created in both classes
* *
* @param Object $foreign_object Instance of the remote class * @param Object $foreignObject Instance of the remote class
* @param string $pivot_table Name of the pivot table * @param String $pivotTable Name of the pivot table
* @param int $local_in_pivot Name of field on the pivot in relation of the local class * @param String $localInPivot 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 String $remoteInPivot 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 $remoteFilter Filters to the remote Array('id', Array('>', 50) )
* @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 ) * @param Array $pivotLimit Array( 'offset'=> 10, 'limit' => 10 )
* @param bool $softDelete
*/ */
protected function belongsToManyExtended($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limit = Array(), $softDelete = true) { protected function belongsToManyExtended(
return new BelongsToManyExtended($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $pivot_limit, $softDelete); string $foreignObject,
string $pivotTable,
string $localInPivot,
string $remoteInPivot,
array $remoteFilter = array(),
array $pivotLimit = array(),
$softDelete = true
) {
return new BelongsToManyExtended($this, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter, $pivotLimit, $softDelete);
} }
/** /**
* With
*
* Eager load relations on this entity * Eager load relations on this entity
* *
**/ * @param null|array|string $relations
public function with($relations = null, $propagate = null){ * @param null|array|string $propagate
if(is_string($relations)){ *
$relations = [$relations]; * @return Entity
*/
public function with(
null|array|string $relations = null,
null|array|string $propagate = null
) {
if (!$relations) {
return $this;
} }
if(!$relations){ if (is_string($relations)) {
return $this; $relations = [$relations];
} }
$possibleRelations = get_class_methods(static::class); $possibleRelations = get_class_methods(static::class);
foreach($relations as $relation){ foreach ($relations as $relation) {
if(in_array($relation, $possibleRelations)){ if (in_array($relation, $possibleRelations)) {
$this->$relation = $this->$relation()->get(); $this->$relation = $this->$relation()->get();
} }
if($this->$relation){ if ($this->$relation) {
$this->$relation->with($propagate); $this->$relation->with($propagate);
} }
} }