Skip to content

Commit

Permalink
Improved model attribute conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sampoyigi committed Sep 8, 2023
1 parent 95f2f30 commit 8214f2a
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions classes/BaseModelAttributesCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class BaseModelAttributesCondition extends BaseCondition
'less' => 'less than',
];

protected ?array $modelAttributes = null;

public function initConfigData($model)
{
$model->operator = 'is';
Expand Down Expand Up @@ -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');
Expand All @@ -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;
});

Expand All @@ -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;
Expand All @@ -112,19 +120,25 @@ 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;

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;
Expand All @@ -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;
}
}

0 comments on commit 8214f2a

Please sign in to comment.