ORM/src/Abstracts/AbstractCollection.php

61 lines
1.1 KiB
PHP
Raw Normal View History

2021-05-06 20:15:01 +00:00
<?php
2024-04-14 16:11:25 +00:00
declare(strict_types=1);
2021-05-06 20:15:01 +00:00
namespace Benzine\ORM\Abstracts;
abstract class AbstractCollection
{
protected array $contained = [];
2022-06-19 01:19:07 +00:00
public function __toArray(): array
{
return $this->contained;
}
2023-01-04 20:14:42 +00:00
public function first()
{
return reset($this->contained);
}
2021-05-06 20:15:01 +00:00
public function getIterator()
{
return new \ArrayIterator($this->contained);
}
public function offsetExists($offset): bool
2021-05-06 20:15:01 +00:00
{
return isset($this->contained[$offset]);
}
public function offsetUnset($offset): void
2021-05-06 20:15:01 +00:00
{
$this->unset($this->contained[$offset]);
}
public function serialize(): string
2021-05-06 20:15:01 +00:00
{
return serialize($this->contained);
}
public function unserialize($data): void
2021-05-06 20:15:01 +00:00
{
$this->contained = unserialize($data);
}
public function count(): int
2021-05-06 20:15:01 +00:00
{
return count($this->contained);
}
2022-07-10 09:29:33 +00:00
public function call($functionName)
{
$return = [];
foreach ($this->contained as $index => $contained) {
$return[$index] = $contained->{$functionName}();
}
return $return;
}
}