2018-11-03 23:25:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Factories;
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Values\SmartPlaylistRule;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
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
|
|
|
*
|
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 = [
|
2021-10-08 16:23:45 +00:00
|
|
|
SmartPlaylistRule::OPERATOR_BEGINS_WITH => [$model, 'LIKE', "$value[0]%"],
|
|
|
|
SmartPlaylistRule::OPERATOR_ENDS_WITH => [$model, 'LIKE', "%$value[0]"],
|
|
|
|
SmartPlaylistRule::OPERATOR_IS => [$model, '=', $value[0]],
|
|
|
|
SmartPlaylistRule::OPERATOR_IS_NOT => [$model, '<>', $value[0]],
|
|
|
|
SmartPlaylistRule::OPERATOR_CONTAINS => [$model, 'LIKE', "%$value[0]%"],
|
|
|
|
SmartPlaylistRule::OPERATOR_NOT_CONTAIN => [$model, 'NOT LIKE', "%$value[0]%"],
|
|
|
|
SmartPlaylistRule::OPERATOR_IS_LESS_THAN => [$model, '<', $value[0]],
|
|
|
|
SmartPlaylistRule::OPERATOR_IS_GREATER_THAN => [$model, '>', $value[0]],
|
|
|
|
SmartPlaylistRule::OPERATOR_IS_BETWEEN => [$model, $value],
|
|
|
|
SmartPlaylistRule::OPERATOR_NOT_IN_LAST => static fn (): array => [$model, '<', now()->subDays($value[0])],
|
|
|
|
SmartPlaylistRule::OPERATOR_IN_LAST => static fn (): array => [$model, '>=', now()->subDays($value[0])],
|
2019-11-30 07:52:25 +00:00
|
|
|
];
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
Assert::keyExists($ruleParameterMap, $operator);
|
2019-11-30 07:52:25 +00:00
|
|
|
|
2021-10-08 10:19:44 +00:00
|
|
|
return is_callable($ruleParameterMap[$operator])
|
|
|
|
? $ruleParameterMap[$operator]()
|
|
|
|
: $ruleParameterMap[$operator];
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|
|
|
|
}
|