1 | <?php |
||
2 | |||
3 | namespace App\Factories; |
||
4 | |||
5 | use App\Models\Rule; |
||
6 | use Carbon\Carbon; |
||
7 | use InvalidArgumentException; |
||
8 | use Throwable; |
||
9 | |||
10 | class SmartPlaylistRuleParameterFactory |
||
11 | { |
||
12 | /** |
||
13 | * @param mixed[] $value |
||
14 | * |
||
15 | * @throws Throwable |
||
16 | * |
||
17 | * @return string[] |
||
18 | */ |
||
19 | 13 | public function createParameters(string $model, string $operator, array $value): array |
|
20 | { |
||
21 | $ruleParameterMap = [ |
||
22 | 13 | Rule::OPERATOR_BEGINS_WITH => [$model, 'LIKE', "{$value[0]}%"], |
|
23 | 13 | Rule::OPERATOR_ENDS_WITH => [$model, 'LIKE', "%{$value[0]}"], |
|
24 | 13 | Rule::OPERATOR_IS => [$model, '=', $value[0]], |
|
25 | 13 | Rule::OPERATOR_IS_NOT => [$model, '<>', $value[0]], |
|
26 | 13 | Rule::OPERATOR_CONTAINS => [$model, 'LIKE', "%{$value[0]}%"], |
|
27 | 13 | Rule::OPERATOR_NOT_CONTAIN => [$model, 'NOT LIKE', "%{$value[0]}%"], |
|
28 | 13 | Rule::OPERATOR_IS_LESS_THAN => [$model, '<', $value[0]], |
|
29 | 13 | Rule::OPERATOR_IS_GREATER_THAN => [$model, '>', $value[0]], |
|
30 | 13 | Rule::OPERATOR_IS_BETWEEN => [$model, $value], |
|
31 | Rule::OPERATOR_NOT_IN_LAST => static function () use ($model, $value): array { |
||
32 | 1 | return [$model, '<', (new Carbon())->subDay($value[0])]; |
|
33 | 13 | }, |
|
34 | Rule::OPERATOR_IN_LAST => static function () use ($model, $value): array { |
||
35 | 1 | return [$model, '>=', (new Carbon())->subDay($value[0])]; |
|
36 | 13 | }, |
|
37 | ]; |
||
38 | |||
39 | 13 | throw_unless(array_key_exists($operator, $ruleParameterMap), InvalidArgumentException::class, sprintf( |
|
40 | 13 | 'Invalid operator %s. Valid operators are: %s.', |
|
41 | 13 | $operator, |
|
42 | 13 | implode(', ', array_keys($ruleParameterMap)) |
|
43 | )); |
||
44 | |||
45 | 13 | return is_array($ruleParameterMap[$operator]) ? $ruleParameterMap[$operator] : $ruleParameterMap[$operator](); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
46 | } |
||
47 | } |
||
48 |