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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-3
@@ -41,8 +41,8 @@ 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];
|
||||||
@@ -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) {
|
||||||
@@ -152,7 +159,7 @@ abstract class Entity {
|
|||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user