- Added some new method to EntityCollection (replaceWith, mergeWith, randomize, sort)
- WIP on virtual fields - Some minor bugfixes
This commit is contained in:
1 parent
c8397484e6
commit
30a0a80cbe
9 files changed
+213
-47
No files matched your search
+119
-16
@@ -8,7 +8,7 @@ class EntityCollection extends \ArrayObject {
|
||||
|
||||
public ? string $entityClass = null;
|
||||
|
||||
public function filters(Callable $callback) : Generator
|
||||
public function filters(Callable $callback, bool $yieldValueOnly = false) : Generator
|
||||
{
|
||||
$idx = 0;
|
||||
|
||||
@@ -16,31 +16,41 @@ class EntityCollection extends \ArrayObject {
|
||||
if ( $callback($item, $key, $idx) ) {
|
||||
$idx++;
|
||||
|
||||
yield $key => $item;
|
||||
if ( $yieldValueOnly ) {
|
||||
yield $item;
|
||||
}
|
||||
else {
|
||||
yield $key => $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function filtersCollection(Callable $callback) : self
|
||||
public function filtersCollection(Callable $callback, bool $yieldValueOnly = false, bool $replaceCollection = false) : self
|
||||
{
|
||||
$collection = new static();
|
||||
|
||||
foreach($this->filters($callback) as $item) {
|
||||
foreach($this->filters($callback, $yieldValueOnly) as $item) {
|
||||
$collection->append($item);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
if ($replaceCollection) {
|
||||
$this->exchangeArray(array_values($collection->getArrayCopy()));
|
||||
|
||||
return $this;
|
||||
}
|
||||
else {
|
||||
return $collection;
|
||||
}
|
||||
}
|
||||
|
||||
public function iterate(Callable $callback) : array
|
||||
public function iterate(Callable $callback) : self
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach($this as $item) {
|
||||
$results[] = $callback($item);
|
||||
$callback($item);
|
||||
}
|
||||
|
||||
return $results;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeOne($value, string $field, bool $strict = true) : ? object
|
||||
@@ -68,6 +78,13 @@ class EntityCollection extends \ArrayObject {
|
||||
return $removed;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public function searchOne($value, string $field, bool $strict = true) : ? object
|
||||
{
|
||||
# Returning first value only
|
||||
@@ -78,24 +95,83 @@ class EntityCollection extends \ArrayObject {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function search($value, string $field, bool $strict = true) : Generator
|
||||
public function searchAll($value, string $field, bool $strict = true) : self
|
||||
{
|
||||
foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) {
|
||||
yield $key => $item;
|
||||
$obj = new static();
|
||||
|
||||
foreach($this->search($value, $field, $strict) as $item) {
|
||||
$obj->append($item);
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function column($field) : array
|
||||
public function column($field, bool $unique = false) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach($this as $item) {
|
||||
$list[] = $item->$field;
|
||||
if ( is_callable($field) ) {
|
||||
$value = call_user_func_array($field, [ $item ]);
|
||||
}
|
||||
else {
|
||||
$value = $item->$field;
|
||||
}
|
||||
|
||||
if ($unique && in_array($value, $list)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$list[] = $value;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function unique(/*stringable|callable */ $field, bool $strict = false) : self
|
||||
{
|
||||
$list = [];
|
||||
$obj = new static();
|
||||
|
||||
foreach($this as $item) {
|
||||
if ( $field === null) {
|
||||
$value = $this;
|
||||
}
|
||||
if ( is_callable($field) ) {
|
||||
$value = call_user_func_array($field, [ $item ]);
|
||||
}
|
||||
else {
|
||||
$value = $item->$field;
|
||||
}
|
||||
|
||||
if ( ! in_array($value, $list, $strict) ) {
|
||||
$list[] = $value;
|
||||
|
||||
$obj->append($item);
|
||||
}
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function first() : ? object
|
||||
{
|
||||
foreach($this as $item) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function last() : ? object
|
||||
{
|
||||
foreach($this as $item) {
|
||||
$return = $item;
|
||||
}
|
||||
|
||||
return $return ?? null;
|
||||
}
|
||||
|
||||
public function buildArray(string $keyColumn, /* string|callable|null */ $value = null) : array
|
||||
{
|
||||
$list = [];
|
||||
@@ -143,7 +219,7 @@ class EntityCollection extends \ArrayObject {
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function fromArray(array $datasets, ? string $entityClass = null) : self
|
||||
public function fromArray(array $datasets, ? string /*stringable*/ $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.");
|
||||
@@ -168,4 +244,31 @@ class EntityCollection extends \ArrayObject {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function replaceWith( /*array|EntityCollection*/ $datasets ) : self
|
||||
{
|
||||
if ( is_object($datasets) ) {
|
||||
$datasets = $datasets->getArrayCopy();
|
||||
}
|
||||
|
||||
$this->exchangeArray( $datasets );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function randomize() : self
|
||||
{
|
||||
$arr = $this->getArrayCopy();
|
||||
shuffle($arr);
|
||||
$this->exchangeArray($arr);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sort(callable $callback, $function = "uasort") : self
|
||||
{
|
||||
call_user_func_array([ $this, $function ], [ $callback ]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user