- Work to add compatibility of PHP 8.0

This commit is contained in:
mcndave committed 2021-01-06 20:06:20 +00:00
commit b76f6ff9d4
8 files changed
+148 -36

No files matched your search

+48 -17
View File
@@ -25,12 +25,12 @@ 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);
}
@@ -77,8 +77,15 @@ class EntityCollection extends \ArrayObject {
return $removed;
}
public function search($value, string $field, bool $strict = true) : Generator
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) {
yield $key => $item;
@@ -105,7 +112,12 @@ 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 = [];
@@ -117,9 +129,9 @@ class EntityCollection extends \ArrayObject {
else {
$value = $item->$field;
}
if ($unique && in_array($value, $list)) {
break;
if ($unique && in_array($value, $list, true)) {
continue;
}
$list[] = $value;
@@ -249,15 +261,22 @@ class EntityCollection extends \ArrayObject {
parent::append($value);
}
}
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()));;
}
}