added collection processing

This commit is contained in:
2023-03-29 14:56:03 +10:00
parent 76102637dd
commit e1468e82e4

View File

@@ -270,6 +270,29 @@ class Conductor
return [$conductor->collection, $total]; return [$conductor->collection, $total];
} }
/**
* Run the conductor on a collection with the data stored in a Request.
*
* @param Request $request The request data.
* @param Collection $collection The collection.
* @return array The processed and transformed model data.
*/
final public static function collection(Request $request, Collection $collection)
{
$conductor_class = get_called_class();
$conductor = new $conductor_class();
$transformedCollection = collect();
foreach ($collection as $item) {
if ($conductor->viewable($item)) {
$transformedCollection->push($conductor->transform($item));
}
}
return $transformedCollection;
}
/** /**
* Run the conductor on a Model with the data stored in a Request. * Run the conductor on a Model with the data stored in a Request.
* *
@@ -333,14 +356,14 @@ class Conductor
* @param Collection $collection If not null, use the passed collection. * @param Collection $collection If not null, use the passed collection.
* @return Collection The current conductor collection. * @return Collection The current conductor collection.
*/ */
final public function collection(Collection $collection = null) // final public function collection(Collection $collection = null)
{ // {
if ($collection !== null) { // if ($collection !== null) {
$this->collection = $collection; // $this->collection = $collection;
} // }
return $this->collection; // return $this->collection;
} // }
/** /**
* Return the current conductor collection count. * Return the current conductor collection count.
@@ -616,10 +639,14 @@ class Conductor
{ {
$visibleFields = $model->getVisible(); $visibleFields = $model->getVisible();
if (empty($visibleFields) === true) { if (empty($visibleFields) === true) {
$tableColumns = $model->getConnection() $visibleFields = $model->getConnection()
->getSchemaBuilder() ->getSchemaBuilder()
->getColumnListing($model->getTable()); ->getColumnListing($model->getTable());
return $tableColumns; }
$appends = $model->getAppends();
if(is_array($appends) === true) {
$visibleFields = array_merge($visibleFields, $appends);
} }
return $visibleFields; return $visibleFields;
@@ -633,7 +660,14 @@ class Conductor
*/ */
public function transform(Model $model) public function transform(Model $model)
{ {
return $model->toArray(); $result = $model->toArray();
$fields = $this->fields($model);
if(is_array($fields) === true) {
$result = array_intersect_key($result, array_flip($fields));
}
return $result;
} }
/** /**