- Added a LoadRelation for the repository.

- Added a new @Filter() annotation to the relation; adding it to JOIN is also coming.
- Some bugfixes done within the Where and ConditionTrait.
This commit is contained in:
mcndave committed 2021-01-06 19:35:45 +00:00
1 parent 9e84bd3536
commit 7dd64abf29
6 files changed
+136 -24

No files matched your search

+39 -8
View File
@@ -26,11 +26,11 @@ class EntityCollection extends \ArrayObject {
}
}
public function filtersCollection(Callable $callback, bool $yieldValueOnly = false, bool $replaceCollection = false) : self
public function filtersCollection(Callable $callback, bool $replaceCollection = false) : self
{
$collection = new static();
foreach($this->filters($callback, $yieldValueOnly) as $item) {
foreach($this->filters($callback, true) as $item) {
$collection->append($item);
}
@@ -78,6 +78,13 @@ class EntityCollection extends \ArrayObject {
return $removed;
}
public function clear() : self
{
$this->exchangeArray([]);
return $this;
}
public function search($value, string $field, bool $strict = true) : Generator
{
foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) {
@@ -106,6 +113,11 @@ class EntityCollection extends \ArrayObject {
return $obj;
}
public function searchInstances(string $className) : self
{
return $this->filtersCollection(fn($obj) => is_a($obj, $className));
}
public function column($field, bool $unique = false) : array
{
$list = [];
@@ -118,8 +130,8 @@ class EntityCollection extends \ArrayObject {
$value = $item->$field;
}
if ($unique && in_array($value, $list)) {
break;
if ($unique && in_array($value, $list, true)) {
continue;
}
$list[] = $value;
@@ -250,13 +262,20 @@ class EntityCollection extends \ArrayObject {
}
}
public function mergeWith( /*array|EntityCollection*/ $datasets ) : self
public function mergeWith(... $datasets) : self
{
if ( is_object($datasets) ) {
$datasets = $datasets->getArrayCopy();
$list = [];
foreach($datasets as $dataset) {
if ( is_object($dataset) ) {
$list = array_merge($dataset->getArrayCopy(), $list);
}
else {
$list = array_merge($dataset, $list);
}
}
$this->exchangeArray( array_merge( $this->getArrayCopy(), $datasets ) );
$this->exchangeArray( array_merge( $this->getArrayCopy(), $list ) );
return $this;
}
@@ -287,4 +306,16 @@ class EntityCollection extends \ArrayObject {
return $this;
}
public function rsort(callable $callback, $function = "uasort") : self
{
$this->sort(...func_get_args());
return $this->reverse();
}
public function reverse() : self
{
return $this->replaceWith(array_reverse($this->getArrayCopy()));;
}
}