| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?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);
- }
- }
- }
|