144 lines
3.8 KiB
PHP
144 lines
3.8 KiB
PHP
<?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(): \Generator
|
|
{
|
|
foreach ($this->items as $key => $val) {
|
|
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':
|
|
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
|
|
break;
|
|
case 'ORM\HasOne':
|
|
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
|
|
break;
|
|
case 'ORM\BelongsTo':
|
|
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
|
|
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;
|
|
}
|
|
|
|
function first()
|
|
{
|
|
if (isset($this->items[0])) {
|
|
return $this->items[0];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Iterate over the colletion
|
|
*
|
|
* @param mixed $function
|
|
* @return void
|
|
*/
|
|
function each($function)
|
|
{
|
|
foreach($this->items as $item){
|
|
$function($item);
|
|
}
|
|
}
|
|
}
|