mirror of
https://github.com/koel/koel
synced 2024-11-10 14:44:13 +00:00
chore: improve SmartPlaylistRuleParameterFactory a bit
This commit is contained in:
parent
c42c770acf
commit
46f64b9970
1 changed files with 28 additions and 27 deletions
|
@ -5,37 +5,38 @@ namespace App\Factories;
|
|||
use App\Models\Rule;
|
||||
use Carbon\Carbon;
|
||||
use InvalidArgumentException;
|
||||
use Throwable;
|
||||
|
||||
class SmartPlaylistRuleParameterFactory
|
||||
{
|
||||
/**
|
||||
* @param mixed[] $value
|
||||
*
|
||||
* @return array
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function createParameters(string $model, string $operator, array $value): array
|
||||
{
|
||||
switch ($operator) {
|
||||
case Rule::OPERATOR_BEGINS_WITH:
|
||||
return [$model, 'LIKE', "{$value[0]}%"];
|
||||
case Rule::OPERATOR_ENDS_WITH:
|
||||
return [$model, 'LIKE', "%{$value[0]}"];
|
||||
case Rule::OPERATOR_IS:
|
||||
return [$model, '=', $value[0]];
|
||||
case Rule::OPERATOR_NOT_IN_LAST:
|
||||
return [$model, '<', (new Carbon())->subDay($value[0])];
|
||||
case Rule::OPERATOR_NOT_CONTAIN:
|
||||
return [$model, 'NOT LIKE', "%{$value[0]}%"];
|
||||
case Rule::OPERATOR_IS_NOT:
|
||||
return [$model, '<>', $value[0]];
|
||||
case Rule::OPERATOR_IS_LESS_THAN:
|
||||
return [$model, '<', $value[0]];
|
||||
case Rule::OPERATOR_IS_GREATER_THAN:
|
||||
return [$model, '>', $value[0]];
|
||||
case Rule::OPERATOR_IS_BETWEEN:
|
||||
return [$model, $value];
|
||||
case Rule::OPERATOR_IN_LAST:
|
||||
return [$model, '>=', (new Carbon())->subDay($value[0])];
|
||||
case Rule::OPERATOR_CONTAINS:
|
||||
return [$model, 'LIKE', "%{$value[0]}%"];
|
||||
default:
|
||||
// should never reach here actually
|
||||
throw new InvalidArgumentException('Invalid operator.');
|
||||
}
|
||||
$ruleParameterMap = [
|
||||
Rule::OPERATOR_BEGINS_WITH => [$model, 'LIKE', "{$value[0]}%"],
|
||||
Rule::OPERATOR_ENDS_WITH => [$model, 'LIKE', "%{$value[0]}"],
|
||||
Rule::OPERATOR_IS => [$model, '=', $value[0]],
|
||||
Rule::OPERATOR_IS_NOT => [$model, '<>', $value[0]],
|
||||
Rule::OPERATOR_CONTAINS => [$model, 'LIKE', "%{$value[0]}%"],
|
||||
Rule::OPERATOR_NOT_CONTAIN => [$model, 'NOT LIKE', "%{$value[0]}%"],
|
||||
Rule::OPERATOR_IS_LESS_THAN => [$model, '<', $value[0]],
|
||||
Rule::OPERATOR_IS_GREATER_THAN => [$model, '>', $value[0]],
|
||||
Rule::OPERATOR_IS_BETWEEN => [$model, $value],
|
||||
Rule::OPERATOR_NOT_IN_LAST => [$model, '<', (new Carbon())->subDay($value[0])],
|
||||
Rule::OPERATOR_IN_LAST => [$model, '>=', (new Carbon())->subDay($value[0])],
|
||||
];
|
||||
|
||||
throw_unless(array_key_exists($operator, $ruleParameterMap), InvalidArgumentException::class, sprintf(
|
||||
'Invalid operator %s. Valid operators are: %s.',
|
||||
$operator,
|
||||
implode(', ', array_keys($ruleParameterMap))
|
||||
));
|
||||
|
||||
return $ruleParameterMap[$operator];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue