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];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,13 @@ abstract class Entity {
|
||||
return static::class;
|
||||
}
|
||||
|
||||
public function charge($payload) {
|
||||
foreach (static::_properties as $key => $property) {
|
||||
$this->$property = $payload[$property];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function get_properties() {
|
||||
$properties = Array();
|
||||
foreach (static::_properties as $propertie) {
|
||||
|
||||
Reference in New Issue
Block a user