cleanup comparitors

This commit is contained in:
2023-03-10 17:59:53 +10:00
parent a9b480994a
commit 0ab92d95ea

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace App\Conductors; namespace App\Conductors;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
@@ -7,7 +8,8 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class Conductor { class Conductor
{
protected $class = null; protected $class = null;
protected $sort = "id"; protected $sort = "id";
protected $limit = 50; protected $limit = 50;
@@ -17,9 +19,11 @@ class Conductor {
private $collection = null; private $collection = null;
private $query = null; private $query = null;
final public static function request(Request $request) {
final public static function request(Request $request)
{
$conductor_class = get_called_class(); $conductor_class = get_called_class();
$conductor = new $conductor_class; $conductor = new $conductor_class();
$total = 0; $total = 0;
@@ -28,13 +32,13 @@ class Conductor {
} catch (\Throwable $e) { } catch (\Throwable $e) {
throw new \Exception('Failed to create query builder instance for ' . $conductor->class . '.', 0, $e); throw new \Exception('Failed to create query builder instance for ' . $conductor->class . '.', 0, $e);
} }
// Scope query // Scope query
$conductor->scope($conductor->query); $conductor->scope($conductor->query);
// Filter request // Filter request
$fields = $conductor->fields(new $conductor->class); $fields = $conductor->fields(new $conductor->class());
if(is_array($fields) == false) { if (is_array($fields) === false) {
$fields = []; $fields = [];
} }
@@ -53,7 +57,7 @@ class Conductor {
// Limit fields // Limit fields
$limitFields = $request->input('fields'); $limitFields = $request->input('fields');
if($limitFields == null) { if ($limitFields === null) {
$limitFields = $fields; $limitFields = $fields;
} else { } else {
$limitFields = array_intersect($limitFields, $fields); $limitFields = array_intersect($limitFields, $fields);
@@ -64,11 +68,11 @@ class Conductor {
// Transform and Includes // Transform and Includes
$includes = $conductor->includes; $includes = $conductor->includes;
if($request->has('includes')) { if ($request->has('includes') === true) {
$includes = explode(',', $request->input('includes')); $includes = explode(',', $request->input('includes'));
} }
$conductor->collection = $conductor->collection->map(function ($model) use($conductor, $includes) { $conductor->collection = $conductor->collection->map(function ($model) use ($conductor, $includes) {
$conductor->includes($model, $includes); $conductor->includes($model, $includes);
$model = $conductor->transform($model); $model = $conductor->transform($model);
@@ -78,50 +82,52 @@ class Conductor {
return [$conductor->collection, $total]; return [$conductor->collection, $total];
} }
final public static function model(Request $request, Model $model) { final public static function model(Request $request, Model $model)
{
$conductor_class = get_called_class(); $conductor_class = get_called_class();
$conductor = new $conductor_class; $conductor = new $conductor_class();
$fields = $conductor->fields(new $conductor->class); $fields = $conductor->fields(new $conductor->class());
// Limit fields // Limit fields
$limitFields = $fields; $limitFields = $fields;
if($request != null && $request->has('fields')) { if ($request !== null && $request->has('fields') === true) {
$requestFields = $request->input('fields'); $requestFields = $request->input('fields');
if($requestFields != null) { if ($requestFields !== null) {
$limitFields = array_intersect(explode(',', $requestFields), $fields); $limitFields = array_intersect(explode(',', $requestFields), $fields);
} }
} }
if(empty($limitFields) === false) { if (empty($limitFields) === false) {
$modelSubset = new $conductor->class; $modelSubset = new $conductor->class();
foreach($limitFields as $field) { foreach ($limitFields as $field) {
$modelSubset->setAttribute($field, $model->$field); $modelSubset->setAttribute($field, $model->$field);
} }
$model = $modelSubset; $model = $modelSubset;
} }
// Includes // Includes
$includes = $conductor->includes; $includes = $conductor->includes;
if($request != null && $request->has('includes')) { if ($request !== null && $request->has('includes') === true) {
$includes = explode(',', $request->input('includes', '')); $includes = explode(',', $request->input('includes', ''));
} }
$conductor->includes($model, $includes); $conductor->includes($model, $includes);
// Transform // Transform
$model = $conductor->transform($model); $model = $conductor->transform($model);
return $model; return $model;
} }
final public function filterField(Builder $builder, string $field, mixed $value) { final public function filterField(Builder $builder, string $field, mixed $value)
{
// Split by comma, but respect quotation marks // Split by comma, but respect quotation marks
if (is_string($value)) { if (is_string($value) === true) {
$values = preg_split('/(?<!\\\\),/', $value); $values = preg_split('/(?<!\\\\),/', $value);
$values = array_map(function ($val) { $values = array_map(function ($val) {
return str_replace('\,', ',', $val); return str_replace('\,', ',', $val);
}, $values); }, $values);
} else if (is_array($value)) { } elseif (is_array($value) === true) {
$values = $value; $values = $value;
} else { } else {
throw new \InvalidArgumentException('Expected string or array, got ' . gettype($value)); throw new \InvalidArgumentException('Expected string or array, got ' . gettype($value));
@@ -131,28 +137,28 @@ class Conductor {
$this->query->where(function ($query) use ($field, $values) { $this->query->where(function ($query) use ($field, $values) {
foreach ($values as $value) { foreach ($values as $value) {
$value = trim($value); $value = trim($value);
// Check if value has a prefix and remove it if it's a number // Check if value has a prefix and remove it if it's a number
if (preg_match('/^([<>!=]=?)(\d+\.?\d*)$/', $value, $matches)) { if (preg_match('/^([<>!=]=?)(\d+\.?\d*)$/', $value, $matches) !== false) {
$prefix = $matches[1]; $prefix = $matches[1];
$value = $matches[2]; $value = $matches[2];
} else { } else {
$prefix = ''; $prefix = '';
} }
// If the value starts with '=', exact match // If the value starts with '=', exact match
if (strpos($value, '=') === 0) { if (strpos($value, '=') === 0) {
$query->where($field, '=', substr($value, 1)); $query->where($field, '=', substr($value, 1));
} else if (strpos($value, '!=') === 0) { } elseif (strpos($value, '!=') === 0) {
$query->where($field, '<>', substr($value, 2)); $query->where($field, '<>', substr($value, 2));
} else if (strpos($value, '!') === 0) { } elseif (strpos($value, '!') === 0) {
$query->where($field, 'NOT LIKE', '%'.substr($value, 1).'%'); $query->where($field, 'NOT LIKE', '%' . substr($value, 1) . '%');
} else { } else {
$query->where($field, 'LIKE', "%$value%"); $query->where($field, 'LIKE', "%$value%");
} }
// Apply the prefix to the query if the value is a number // Apply the prefix to the query if the value is a number
if (is_numeric($value)) { if (is_numeric($value) === true) {
switch ($prefix) { switch ($prefix) {
case '>': case '>':
$query->where($field, '>', $value); $query->where($field, '>', $value);
@@ -172,41 +178,44 @@ class Conductor {
break; break;
} }
} }
} }//end foreach
}); });
} }
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;
} }
final public function count() { final public function count()
if($this->query != null) { {
if ($this->query !== null) {
return $this->query->count(); return $this->query->count();
} }
return 0; return 0;
} }
final public function sort(mixed $fields = null) { final public function sort(mixed $fields = null)
if(is_string($fields)) { {
if (is_string($fields) === true) {
$fields = explode(',', $fields); $fields = explode(',', $fields);
} else if($fields == null) { } elseif ($fields === null) {
$fields = $this->sort; $fields = $this->sort;
} }
if(is_array($fields)) { if (is_array($fields) === true) {
foreach ($fields as $orderByField) { foreach ($fields as $orderByField) {
$direction = 'asc'; $direction = 'asc';
$directionChar = substr($orderByField, 0, 1); $directionChar = substr($orderByField, 0, 1);
if(in_array($directionChar, ['-', '+'])) { if (in_array($directionChar, ['-', '+']) === true) {
$orderByField = substr($orderByField, 1); $orderByField = substr($orderByField, 1);
if($directionChar == '-') { if ($directionChar === '-') {
$direction = 'desc'; $direction = 'desc';
} }
} }
@@ -217,16 +226,18 @@ class Conductor {
throw new \InvalidArgumentException('Expected string or array, got ' . gettype($fields)); throw new \InvalidArgumentException('Expected string or array, got ' . gettype($fields));
} }
} }
final public function filter(array $filters) { final public function filter(array $filters)
{
foreach ($filters as $param => $value) { foreach ($filters as $param => $value) {
$this->filterField($this->query, $param, $value); $this->filterField($this->query, $param, $value);
} }
} }
final public function paginate(int $page = 1, int $limit = -1) { final public function paginate(int $page = 1, int $limit = -1)
{
// Limit // Limit
if($limit < 1) { if ($limit < 1) {
$limit = $this->limit; $limit = $this->limit;
} else { } else {
$limit = min($limit, $this->maxLimit); $limit = min($limit, $this->maxLimit);
@@ -234,39 +245,42 @@ class Conductor {
$this->query->limit($limit); $this->query->limit($limit);
// Page // Page
if($page < 1) { if ($page < 1) {
$page = 1; $page = 1;
} }
$this->query->offset(($page - 1) * $limit); $this->query->offset(($page - 1) * $limit);
} }
final public function includes(Model $model, array $includes) { final public function includes(Model $model, array $includes)
foreach($includes as $include) { {
foreach ($includes as $include) {
$includeMethodName = 'include' . Str::studly($include); $includeMethodName = 'include' . Str::studly($include);
if (method_exists($this, $includeMethodName)) { if (method_exists($this, $includeMethodName) === true) {
$attributeName = Str::snake($include); $attributeName = Str::snake($include);
$attributeValue = $this->{$includeMethodName}($model); $attributeValue = $this->{$includeMethodName}($model);
if($attributeValue !== null) { if ($attributeValue !== null) {
$model->$attributeName = $this->{$includeMethodName}($model); $model->$attributeName = $this->{$includeMethodName}($model);
} }
} }
} }
} }
final public function limitFields(array $fields) { final public function limitFields(array $fields)
if(empty($fields) !== true) { {
if (empty($fields) !== true) {
$this->query->select($fields); $this->query->select($fields);
} }
} }
/** overrides */ /** overrides */
public function scope(Builder $builder) { public function scope(Builder $builder)
{
} }
public function fields(Model $model) { public function fields(Model $model)
{
$visibleFields = $model->getVisible(); $visibleFields = $model->getVisible();
if (empty($visibleFields)) { if (empty($visibleFields) === true) {
$tableColumns = $model->getConnection() $tableColumns = $model->getConnection()
->getSchemaBuilder() ->getSchemaBuilder()
->getColumnListing($model->getTable()); ->getColumnListing($model->getTable());
@@ -276,23 +290,28 @@ class Conductor {
return $visibleFields; return $visibleFields;
} }
public function transform(Model $model) { public function transform(Model $model)
{
return $model->toArray(); return $model->toArray();
} }
public static function viewable(Model $model) { public static function viewable(Model $model)
{
return true; return true;
} }
public static function creatable() { public static function creatable()
return true; {
}
public static function updatable(Model $model) {
return true; return true;
} }
public static function destroyable(Model $model) { public static function updatable(Model $model)
{
return true; return true;
} }
}
public static function destroyable(Model $model)
{
return true;
}
}