2018-11-03 23:25:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Factories;
|
|
|
|
|
|
|
|
use App\Models\Rule;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use InvalidArgumentException;
|
2019-11-30 07:52:25 +00:00
|
|
|
use Throwable;
|
2018-11-03 23:25:08 +00:00
|
|
|
|
|
|
|
class SmartPlaylistRuleParameterFactory
|
|
|
|
{
|
2019-11-30 07:52:25 +00:00
|
|
|
/**
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<mixed> $value
|
2019-11-30 07:52:25 +00:00
|
|
|
*
|
|
|
|
* @throws Throwable
|
2019-11-30 07:52:42 +00:00
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @return array<string>
|
2019-11-30 07:52:25 +00:00
|
|
|
*/
|
2018-11-03 23:25:08 +00:00
|
|
|
public function createParameters(string $model, string $operator, array $value): array
|
|
|
|
{
|
2019-11-30 07:52:25 +00:00
|
|
|
$ruleParameterMap = [
|
|
|
|
Rule::OPERATOR_BEGINS_WITH => [$model, 'LIKE', "{$value[0]}%"],
|
|
|
|
Rule::OPERATOR_ENDS_WITH => [$model, 'LIKE', "%{$value[0]}"],
|
|
|
|
Rule::OPERATOR_IS => [$model, '=', $value[0]],
|
2020-09-06 21:20:42 +00:00
|
|
|
Rule::OPERATOR_IS_NOT => [$model, '<>', $value[0]],
|
2019-11-30 07:52:25 +00:00
|
|
|
Rule::OPERATOR_CONTAINS => [$model, 'LIKE', "%{$value[0]}%"],
|
|
|
|
Rule::OPERATOR_NOT_CONTAIN => [$model, 'NOT LIKE', "%{$value[0]}%"],
|
|
|
|
Rule::OPERATOR_IS_LESS_THAN => [$model, '<', $value[0]],
|
2020-09-06 21:20:42 +00:00
|
|
|
Rule::OPERATOR_IS_GREATER_THAN => [$model, '>', $value[0]],
|
2019-11-30 07:52:25 +00:00
|
|
|
Rule::OPERATOR_IS_BETWEEN => [$model, $value],
|
2019-11-30 08:11:26 +00:00
|
|
|
Rule::OPERATOR_NOT_IN_LAST => static function () use ($model, $value): array {
|
|
|
|
return [$model, '<', (new Carbon())->subDay($value[0])];
|
|
|
|
},
|
|
|
|
Rule::OPERATOR_IN_LAST => static function () use ($model, $value): array {
|
|
|
|
return [$model, '>=', (new Carbon())->subDay($value[0])];
|
|
|
|
},
|
2019-11-30 07:52:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
throw_unless(array_key_exists($operator, $ruleParameterMap), InvalidArgumentException::class, sprintf(
|
|
|
|
'Invalid operator %s. Valid operators are: %s.',
|
|
|
|
$operator,
|
|
|
|
implode(', ', array_keys($ruleParameterMap))
|
|
|
|
));
|
|
|
|
|
2019-11-30 08:11:26 +00:00
|
|
|
return is_array($ruleParameterMap[$operator]) ? $ruleParameterMap[$operator] : $ruleParameterMap[$operator]();
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|
|
|
|
}
|