2018-11-03 23:25:08 +00:00
|
|
|
<?php
|
|
|
|
|
2023-08-20 15:24:56 +00:00
|
|
|
namespace App\Values;
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2024-04-18 14:11:47 +00:00
|
|
|
use App\Enums\SmartPlaylistModel;
|
|
|
|
use App\Enums\SmartPlaylistOperator;
|
2021-10-08 10:19:44 +00:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2023-04-17 19:45:43 +00:00
|
|
|
use Illuminate\Support\Str;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
final class SmartPlaylistRule implements Arrayable
|
2018-11-03 23:25:08 +00:00
|
|
|
{
|
2023-04-17 19:45:43 +00:00
|
|
|
public string $id;
|
2024-04-18 14:11:47 +00:00
|
|
|
public SmartPlaylistModel $model;
|
|
|
|
public SmartPlaylistOperator $operator;
|
2021-10-08 16:23:45 +00:00
|
|
|
public array $value;
|
2018-11-03 23:25:08 +00:00
|
|
|
|
|
|
|
private function __construct(array $config)
|
|
|
|
{
|
2021-10-10 18:05:51 +00:00
|
|
|
self::assertConfig($config);
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2023-04-17 19:45:43 +00:00
|
|
|
$this->id = $config['id'] ?? Str::uuid()->toString();
|
2018-11-03 23:25:08 +00:00
|
|
|
$this->value = $config['value'];
|
2024-04-18 14:11:47 +00:00
|
|
|
$this->model = SmartPlaylistModel::from($config['model']);
|
|
|
|
$this->operator = SmartPlaylistOperator::from($config['operator']);
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 14:11:47 +00:00
|
|
|
/** @noinspection PhpExpressionResultUnusedInspection */
|
2021-10-10 18:05:51 +00:00
|
|
|
public static function assertConfig(array $config, bool $allowUserIdModel = true): void
|
|
|
|
{
|
2023-04-17 19:45:43 +00:00
|
|
|
if ($config['id'] ?? null) {
|
|
|
|
Assert::uuid($config['id']);
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:11:47 +00:00
|
|
|
SmartPlaylistOperator::from($config['operator']);
|
|
|
|
|
|
|
|
if (!$allowUserIdModel) {
|
|
|
|
Assert::false($config['model'] === SmartPlaylistModel::USER_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmartPlaylistModel::from($config['model']);
|
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
Assert::isArray($config['value']);
|
|
|
|
Assert::countBetween($config['value'], 1, 2);
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
public static function make(array $config): self
|
2018-11-03 23:25:08 +00:00
|
|
|
{
|
2021-10-10 18:05:51 +00:00
|
|
|
return new self($config);
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:19:44 +00:00
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-10-08 16:23:45 +00:00
|
|
|
'id' => $this->id,
|
2024-04-18 14:11:47 +00:00
|
|
|
'model' => $this->model->value,
|
|
|
|
'operator' => $this->operator->value,
|
2021-10-08 10:19:44 +00:00
|
|
|
'value' => $this->value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:05:21 +00:00
|
|
|
public function equals(array|self $rule): bool
|
2021-10-08 10:19:44 +00:00
|
|
|
{
|
2021-10-08 16:23:45 +00:00
|
|
|
if (is_array($rule)) {
|
2024-01-04 21:51:32 +00:00
|
|
|
$rule = self::make($rule);
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:19:44 +00:00
|
|
|
return $this->operator === $rule->operator
|
2021-10-08 16:23:45 +00:00
|
|
|
&& !array_diff($this->value, $rule->value)
|
2021-10-08 10:19:44 +00:00
|
|
|
&& $this->model === $rule->model;
|
|
|
|
}
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|