Iniciando collections
This commit is contained in:
@@ -0,0 +1,131 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ORM;
|
||||||
|
|
||||||
|
// https://stitcher.io/blog/typed-properties-in-php-74
|
||||||
|
// https://dev.to/chemaclass/typed-arrays-in-php-hoo
|
||||||
|
class Collection implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||||
|
{
|
||||||
|
|
||||||
|
private Array $items;
|
||||||
|
|
||||||
|
function __construct(array $items = array())
|
||||||
|
{
|
||||||
|
$this->items = $items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addItem(Entity $item)
|
||||||
|
{
|
||||||
|
$this->items[] = $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.php.net/manual/en/class.countable.php
|
||||||
|
public function count()
|
||||||
|
{
|
||||||
|
return count($this->items);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.php.net/manual/en/class.iteratoraggregate.php
|
||||||
|
public function getIterator()
|
||||||
|
{
|
||||||
|
return (function () {
|
||||||
|
while (list($key, $val) = each($this->items)) {
|
||||||
|
yield $key => $val;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.php.net/manual/en/class.arrayaccess.php
|
||||||
|
public function offsetSet($offset, $value)
|
||||||
|
{
|
||||||
|
if (is_null($offset)) {
|
||||||
|
$this->items[] = $value;
|
||||||
|
} else {
|
||||||
|
$this->items[$offset] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists($offset)
|
||||||
|
{
|
||||||
|
return isset($this->items[$offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset($offset)
|
||||||
|
{
|
||||||
|
unset($this->items[$offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet($offset)
|
||||||
|
{
|
||||||
|
return isset($this->items[$offset]) ? $this->items[$offset] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// -----------------------------------
|
||||||
|
// Magic methods
|
||||||
|
//
|
||||||
|
|
||||||
|
public function __call($name, $arguments)
|
||||||
|
{
|
||||||
|
if (sizeof($this->items) == 1) {
|
||||||
|
return $this->items[0]->$name();
|
||||||
|
} else {
|
||||||
|
throw new \Exception('Undefined method or Multiple values on set');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// -----------------------------------
|
||||||
|
// Collection specific methods
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
function get($forceCollection = false)
|
||||||
|
{
|
||||||
|
if (sizeof($this->items) == 1 && !$forceCollection) {
|
||||||
|
return $this->items[0];
|
||||||
|
} else {
|
||||||
|
return $this->items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function with(array $connections = array())
|
||||||
|
{
|
||||||
|
|
||||||
|
$searchs = array();
|
||||||
|
|
||||||
|
foreach ($this->items as $key => $item) {
|
||||||
|
foreach ($connections as $connection) {
|
||||||
|
$relation = $item->$connection();
|
||||||
|
|
||||||
|
switch (get_class($relation)) {
|
||||||
|
case 'ORM\HasMany':
|
||||||
|
break;
|
||||||
|
case 'ORM\HasOne':
|
||||||
|
break;
|
||||||
|
case 'ORM\BelongsTo':
|
||||||
|
$searchs['belongsTo'][] = [];
|
||||||
|
break;
|
||||||
|
case 'ORM\BelongsToMany':
|
||||||
|
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
|
||||||
|
break;
|
||||||
|
case 'ORM\BelongsToManyExtended':
|
||||||
|
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->items;
|
||||||
|
}
|
||||||
|
|
||||||
|
function first()
|
||||||
|
{
|
||||||
|
if (isset($this->items[0])) {
|
||||||
|
return $this->items[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
-48
@@ -40,14 +40,14 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
$this->$property = $value;
|
$this->$property = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($key){
|
public function __get($key) {
|
||||||
if(!isset($this->$key)){
|
if (!isset($this->$key)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return $this->toArray()[$key];
|
return $this->toArray()[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString() {
|
public function __toString() {
|
||||||
if (isset($this->name)) {
|
if (isset($this->name)) {
|
||||||
return $this->name;
|
return $this->name;
|
||||||
@@ -58,6 +58,13 @@ abstract class Entity {
|
|||||||
return static::class;
|
return static::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function charge($payload) {
|
||||||
|
foreach (static::_properties as $key => $property) {
|
||||||
|
$this->$property = $payload[$property];
|
||||||
|
}
|
||||||
|
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) {
|
||||||
@@ -110,13 +117,13 @@ abstract class Entity {
|
|||||||
//Get the actual max value of the key
|
//Get the actual max value of the key
|
||||||
$maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::_tableName . "}");
|
$maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::_tableName . "}");
|
||||||
$maxVal = ($maxVal) ? $maxVal : 0;
|
$maxVal = ($maxVal) ? $maxVal : 0;
|
||||||
|
|
||||||
// If the next val is smaller than the minumum value
|
// If the next val is smaller than the minumum value
|
||||||
if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id+1) {
|
if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id+1) {
|
||||||
// The id value shpuld assume the min value
|
// The id value shpuld assume the min value
|
||||||
$this->id = static::_idPolice['min'];
|
$this->id = static::_idPolice['min'];
|
||||||
} else {
|
} else {
|
||||||
// The id is bigger than the min or min is not set
|
// The id is bigger than the min or min is not set
|
||||||
if (isset(static::_idPolice['step'])) {
|
if (isset(static::_idPolice['step'])) {
|
||||||
//If sted is defined, increment by step multiples
|
//If sted is defined, increment by step multiples
|
||||||
$this->id = $maxVal->id + static::_idPolice['step'];
|
$this->id = $maxVal->id + static::_idPolice['step'];
|
||||||
@@ -130,10 +137,10 @@ abstract class Entity {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
|
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
|
||||||
$obj = (array) $this;
|
$obj = (array) $this;
|
||||||
|
|
||||||
$first_argument = '';
|
$first_argument = '';
|
||||||
$second_argument = '';
|
$second_argument = '';
|
||||||
$insert_data = Array();
|
$insert_data = Array();
|
||||||
@@ -149,10 +156,10 @@ abstract class Entity {
|
|||||||
$second_argument = rtrim($second_argument, ', ');
|
$second_argument = rtrim($second_argument, ', ');
|
||||||
|
|
||||||
$sql = "INSERT INTO {" . static::_tableName . "} ($first_argument) VALUES ($second_argument)";
|
$sql = "INSERT INTO {" . static::_tableName . "} ($first_argument) VALUES ($second_argument)";
|
||||||
|
|
||||||
DBInstance::execute($sql, $insert_data, static::_connectionName);
|
DBInstance::execute($sql, $insert_data, static::_connectionName);
|
||||||
// If there is a id pollice applied, do not get the inserted id
|
// If there is a id pollice applied, do not get the inserted id
|
||||||
if ( empty(static::_idPolice) ){
|
if (empty(static::_idPolice)) {
|
||||||
$this->id = DBInstance::lastInsert();
|
$this->id = DBInstance::lastInsert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,10 +198,10 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find All
|
* Find All
|
||||||
*
|
*
|
||||||
* This method will load all instances from the class.
|
* This method will load all instances from the class.
|
||||||
* Limits are avaliable for paginations.
|
* Limits are avaliable for paginations.
|
||||||
*
|
*
|
||||||
* @param Array $select Columns to select. Array('id', 'name')
|
* @param Array $select Columns to select. Array('id', 'name')
|
||||||
* @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?
|
||||||
@@ -233,13 +240,13 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Count
|
* Count
|
||||||
*
|
*
|
||||||
* Count how many records there is on the database
|
* Count how many records there is on the database
|
||||||
* in relation with this class with the parameters filter
|
* in relation with this class with the parameters filter
|
||||||
*
|
*
|
||||||
* @param Array $criterias Criteras for WHERE Array('id' => Array('=', 10) )
|
* @param Array $criterias Criteras for WHERE Array('id' => Array('=', 10) )
|
||||||
* @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 = "";
|
||||||
@@ -275,7 +282,7 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find One
|
* Find One
|
||||||
*
|
*
|
||||||
* @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30)))
|
* @param Array/Int $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 Boolan $trashed true, false
|
||||||
@@ -346,12 +353,12 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find Many
|
* Find Many
|
||||||
*
|
*
|
||||||
* @param Array $criterias Description
|
* @param Array $criterias Description
|
||||||
* @param Array $select Description
|
* @param Array $select Description
|
||||||
* @param Array $limits Description
|
* @param Array $limits Description
|
||||||
* @param boolean $trashed Description
|
* @param boolean $trashed Description
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
|
public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
|
||||||
$select_sql = "";
|
$select_sql = "";
|
||||||
@@ -405,23 +412,23 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Has One Local
|
* Has One Local
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
*
|
*
|
||||||
* Table user = (id, name)
|
* Table user = (id, name)
|
||||||
* Table phone = (id, number, userid)
|
* Table phone = (id, number, userid)
|
||||||
*
|
*
|
||||||
* 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');
|
* @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) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
@@ -430,20 +437,20 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Has Many
|
* Has Many
|
||||||
*
|
*
|
||||||
* Defines that, this object has many instances of other Object
|
* Defines that, this object has many instances of other Object
|
||||||
*
|
*
|
||||||
* Table Post = (id, name, content)
|
* Table Post = (id, name, content)
|
||||||
* Table Comment = (id, name, content, postid)
|
* Table Comment = (id, name, content, postid)
|
||||||
*
|
*
|
||||||
* 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');
|
* @ctag $this->hasMany('foreign_object', 'field_in_foreign');
|
||||||
*
|
*
|
||||||
* @param type $foreign_object Instance of a remote class
|
* @param type $foreign_object Instance of a remote class
|
||||||
* @param (int, string) $field_in_foreign The field to match the local id
|
* @param (int, string) $field_in_foreign The field to match the local id
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected function hasMany($foreign_object, $field_in_foreign) {
|
protected function hasMany($foreign_object, $field_in_foreign) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
@@ -452,22 +459,22 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Belongs To
|
* Belongs To
|
||||||
*
|
*
|
||||||
* Defines that, this object is part of an other class.
|
* Defines that, this object is part of an other class.
|
||||||
* The local field must match the id of an other class.
|
* The local field must match the id of an other class.
|
||||||
*
|
*
|
||||||
* Table Post = (id, name, content)
|
* Table Post = (id, name, content)
|
||||||
* Table Comment = (id, name, content, postid)
|
* Table Comment = (id, name, content, postid)
|
||||||
*
|
*
|
||||||
* 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');
|
* @ctag $this->belongsTo('foreign_object', 'local_field');
|
||||||
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
|
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
|
||||||
*
|
*
|
||||||
* @param Object $foreign_object Instance of a remote class
|
* @param Object $foreign_object Instance of a remote class
|
||||||
* @param (int, String) $local_field Remote field relate to local Object
|
* @param (int, String) $local_field Remote field relate to local Object
|
||||||
* @param string $remote_field
|
* @param string $remote_field
|
||||||
*/
|
*/
|
||||||
protected function belongsTo($foreign_object, $local_field, $remote_field = 'id') {
|
protected function belongsTo($foreign_object, $local_field, $remote_field = 'id') {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
@@ -476,20 +483,20 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Belongs to Many
|
* Belongs to Many
|
||||||
*
|
*
|
||||||
* Defines that this object is related to many other instances
|
* Defines that this object is related to many other instances
|
||||||
* of other classes throw a pivot table.
|
* of other classes throw a pivot table.
|
||||||
*
|
*
|
||||||
* Table Post = (id, name, content)
|
* Table Post = (id, name, content)
|
||||||
* Table Tag = (id, name)
|
* Table Tag = (id, name)
|
||||||
* Table Post_Tag = (id, postid, tagid)
|
* Table Post_Tag = (id, postid, tagid)
|
||||||
*
|
*
|
||||||
* 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');
|
* @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());
|
||||||
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $remote_limit = 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 Object $foreign_object Instance of the remote class
|
||||||
* @param string $pivot_table Name of the pivot table
|
* @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 $local_in_pivot Name of field on the pivot in relation of the local class
|
||||||
@@ -519,21 +526,21 @@ abstract class Entity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Belongs to Many Extended
|
* Belongs to Many Extended
|
||||||
*
|
*
|
||||||
* Defines that this object is related to many other instances
|
* Defines that this object is related to many other instances
|
||||||
* of other classes throw a pivot table.
|
* of other classes throw a pivot table.
|
||||||
* This relation will bring the pivot table with the elements inside
|
* This relation will bring the pivot table with the elements inside
|
||||||
*
|
*
|
||||||
* Table Post = (id, name, content)
|
* Table Post = (id, name, content)
|
||||||
* 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');
|
||||||
* @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());
|
||||||
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $pivot_limits = 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 $foreign_object Instance of the remote class
|
||||||
* @param string $pivot_table Name of the pivot table
|
* @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 $local_in_pivot Name of field on the pivot in relation of the local class
|
||||||
|
|||||||
Reference in New Issue
Block a user