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
|
13 |
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param mixed[] $value |
14
|
13 |
|
* |
15
|
1 |
|
* @return array |
16
|
12 |
|
* @throws Throwable |
17
|
1 |
|
*/ |
18
|
11 |
|
public function createParameters(string $model, string $operator, array $value): array |
19
|
4 |
|
{ |
20
|
9 |
|
$ruleParameterMap = [ |
21
|
1 |
|
Rule::OPERATOR_BEGINS_WITH => [$model, 'LIKE', "{$value[0]}%"], |
22
|
8 |
|
Rule::OPERATOR_ENDS_WITH => [$model, 'LIKE', "%{$value[0]}"], |
23
|
1 |
|
Rule::OPERATOR_IS => [$model, '=', $value[0]], |
24
|
7 |
|
Rule::OPERATOR_IS_NOT => [$model, '<>', $value[0]], |
25
|
3 |
|
Rule::OPERATOR_CONTAINS => [$model, 'LIKE', "%{$value[0]}%"], |
26
|
5 |
|
Rule::OPERATOR_NOT_CONTAIN => [$model, 'NOT LIKE', "%{$value[0]}%"], |
27
|
1 |
|
Rule::OPERATOR_IS_LESS_THAN => [$model, '<', $value[0]], |
28
|
4 |
|
Rule::OPERATOR_IS_GREATER_THAN => [$model, '>', $value[0]], |
29
|
1 |
|
Rule::OPERATOR_IS_BETWEEN => [$model, $value], |
30
|
3 |
|
Rule::OPERATOR_NOT_IN_LAST => [$model, '<', (new Carbon())->subDay($value[0])], |
31
|
1 |
|
Rule::OPERATOR_IN_LAST => [$model, '>=', (new Carbon())->subDay($value[0])], |
32
|
2 |
|
]; |
33
|
1 |
|
|
34
|
1 |
|
throw_unless(array_key_exists($operator, $ruleParameterMap), InvalidArgumentException::class, sprintf( |
35
|
1 |
|
'Invalid operator %s. Valid operators are: %s.', |
36
|
|
|
$operator, |
37
|
|
|
implode(', ', array_keys($ruleParameterMap)) |
38
|
|
|
)); |
39
|
|
|
|
40
|
|
|
return $ruleParameterMap[$operator]; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|