This commit is contained in:
Dave Mc Nicoll committed 2020-10-06 11:39:01 -04:00
commit 47953a6165
17 files changed
+576 -261

No files matched your search

+60 -10
View File
@@ -6,6 +6,8 @@ use Generator;
class EntityCollection extends \ArrayObject {
public ? string $entityClass = null;
public function filters(Callable $callback) : Generator
{
$idx = 0;
@@ -29,6 +31,17 @@ class EntityCollection extends \ArrayObject {
return $collection;
}
public function iterate(Callable $callback) : array
{
$results = [];
foreach($this as $item) {
$results[] = $callback($item);
}
return $results;
}
public function removeOne($value, string $field, bool $strict = true) : ? object
{
@@ -72,6 +85,17 @@ class EntityCollection extends \ArrayObject {
}
}
public function column($field) : array
{
$list = [];
foreach($this as $item) {
$list[] = $item->$field;
}
return $list;
}
public function buildArray(string $keyColumn, /* string|callable|null */ $value = null) : array
{
$list = [];
@@ -98,16 +122,6 @@ class EntityCollection extends \ArrayObject {
return $list;
}
public function toArray(bool $includeRelations = false) : array {
$list = [];
foreach($this as $entity) {
$list[] = $entity->toArray($includeRelations);
}
return $list;
}
public function implode(string $glue, ? Callable $callback = null) : string
{
$values = [];
@@ -118,4 +132,40 @@ class EntityCollection extends \ArrayObject {
return implode($glue, $values);
}
public function toArray(bool $includeRelations = false) : array {
$list = [];
foreach($this as $entity) {
$list[] = $entity->toArray($includeRelations);
}
return $list;
}
public function fromArray(array $datasets, ? string $entityClass = null) : self
{
if ( ! ($this->entityClass || $entityClass) ) {
throw new \Exception("An entity class name must be provided to be instanciated and populated before insertion into this collection.");
}
$className = $entityClass ?: $this->entityClass;
foreach($datasets as $dataset) {
$this->append( (new $className() )->fromArray($dataset) );
}
return $this;
}
public function mergeWith( /*array|EntityCollection*/ $datasets ) : self
{
if ( is_object($datasets) ) {
$datasets = $datasets->getArrayCopy();
}
$this->exchangeArray( array_merge( $this->getArrayCopy(), $datasets ) );
return $this;
}
}