From 8214f2a219a7247f7d145238b47f0b5f55d2a94e Mon Sep 17 00:00:00 2001 From: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> Date: Fri, 8 Sep 2023 08:56:36 +0100 Subject: [PATCH] Improved model attribute conditions --- classes/BaseModelAttributesCondition.php | 72 +++++++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/classes/BaseModelAttributesCondition.php b/classes/BaseModelAttributesCondition.php index df73b88..878d43d 100644 --- a/classes/BaseModelAttributesCondition.php +++ b/classes/BaseModelAttributesCondition.php @@ -17,6 +17,8 @@ class BaseModelAttributesCondition extends BaseCondition 'less' => 'less than', ]; + protected ?array $modelAttributes = null; + public function initConfigData($model) { $model->operator = 'is'; @@ -76,9 +78,8 @@ public function getOperatorOptions() */ public function evalIsTrue($modelToEval) { - $model = $this->model; $attributes = $this->listModelAttributes(); - $subConditions = $model->options ?? []; + $subConditions = $this->model->options ?? []; collect($subConditions)->sortBy('priority')->each(function ($subCondition) use (&$success, $modelToEval, $attributes) { $attribute = array_get($subCondition, 'attribute'); @@ -87,6 +88,9 @@ public function evalIsTrue($modelToEval) if ($attributeType == 'string') $success = $this->evalAttributeStringType($modelToEval, $subCondition); + if ($attributeType == 'custom') + $success = $this->evalAttributeCustomType($modelToEval, $subCondition); + return $success; }); @@ -99,6 +103,10 @@ protected function listModelAttributes() return $this->modelAttributes; $attributes = array_map(function ($info) { + if (is_string($info)) { + $info = ['label' => $info]; + } + isset($info['type']) || $info['type'] = 'string'; return $info; @@ -112,7 +120,7 @@ protected function evalAttributeStringType($model, $subCondition) $attribute = array_get($subCondition, 'attribute'); $operator = array_get($subCondition, 'operator'); $conditionValue = mb_strtolower(trim(array_get($subCondition, 'value'))); - $modelValue = $this->getModelEvalAttribute($model, $attribute); + $modelValue = $this->getModelEvalAttribute($model, $attribute, $subCondition); if ($operator === 'is') return $modelValue == $conditionValue; @@ -120,11 +128,17 @@ protected function evalAttributeStringType($model, $subCondition) if ($operator === 'is_not') return $modelValue != $conditionValue; - if ($operator === 'contains') - return mb_strpos($modelValue, $conditionValue) !== false; + if ($operator === 'contains') { + return is_array($conditionValue) + ? in_array($modelValue, $conditionValue) !== false + : mb_strpos($modelValue, $conditionValue) !== false; + } - if ($operator === 'does_not_contain') - return mb_strpos($modelValue, $conditionValue) === false; + if ($operator === 'does_not_contain') { + return is_array($conditionValue) + ? in_array($modelValue, $conditionValue) === false + : mb_strpos($modelValue, $conditionValue) === false; + } if ($operator === 'equals_or_greater') return $modelValue >= $conditionValue; @@ -141,13 +155,53 @@ protected function evalAttributeStringType($model, $subCondition) return false; } - protected function getModelEvalAttribute($model, $attribute) + protected function getModelEvalAttribute($model, $attribute, $condition = []) { $value = $model->{$attribute}; if (method_exists($this, 'get'.Str::studly($attribute).'Attribute')) - $value = $this->{'get'.Str::studly($attribute).'Attribute'}($value, $model); + $value = $this->{'get'.Str::studly($attribute).'Attribute'}($value, $model, $condition); return mb_strtolower(trim($value)); } + + protected function applyDateRange($query, $attribute, $options) + { + $from = $this->getDateRangeFrom($options); + $to = $this->getDateRangeTo($options); + if ($from && $to) { + $query->whereBetween($attribute, [$from, $to]); + } + + return $query; + } + + protected function getDateRangeFrom(array $options) + { + if (array_get($options, 'when') === 'is_current'){ + return now()->startOf(array_get($options, 'current', 'day'))->toDateTimeString(); + } + + if (array_get($options, 'when') === 'is_past'){ + return now() + ->parse('- '.str_replace('_', ' ', array_get($options, 'range', '1_day'))) + ->startOfDay() + ->toDateTimeString(); + } + + return null; + } + + protected function getDateRangeTo(array $options) + { + if (array_get($options, 'when') === 'is_current'){ + return now()->endOf(array_get($options, 'current', 'day'))->toDateTimeString(); + } + + if (array_get($options, 'when') === 'is_past'){ + return now()->endOfDay()->toDateTimeString(); + } + + return null; + } }