2018-11-04 00:25:08 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-08 18:23:45 +02:00
|
|
|
namespace App\Values;
|
2018-11-04 00:25:08 +01:00
|
|
|
|
2021-10-08 12:19:44 +02:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2021-06-05 12:47:56 +02:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-11-04 00:25:08 +01:00
|
|
|
|
2021-10-08 18:23:45 +02:00
|
|
|
final class SmartPlaylistRule implements Arrayable
|
2018-11-04 00:25:08 +01:00
|
|
|
{
|
|
|
|
public const OPERATOR_IS = 'is';
|
|
|
|
public const OPERATOR_IS_NOT = 'isNot';
|
|
|
|
public const OPERATOR_CONTAINS = 'contains';
|
|
|
|
public const OPERATOR_NOT_CONTAIN = 'notContain';
|
|
|
|
public const OPERATOR_IS_BETWEEN = 'isBetween';
|
|
|
|
public const OPERATOR_IS_GREATER_THAN = 'isGreaterThan';
|
|
|
|
public const OPERATOR_IS_LESS_THAN = 'isLessThan';
|
|
|
|
public const OPERATOR_BEGINS_WITH = 'beginsWith';
|
|
|
|
public const OPERATOR_ENDS_WITH = 'endsWith';
|
|
|
|
public const OPERATOR_IN_LAST = 'inLast';
|
|
|
|
public const OPERATOR_NOT_IN_LAST = 'notInLast';
|
|
|
|
|
|
|
|
public const VALID_OPERATORS = [
|
|
|
|
self::OPERATOR_BEGINS_WITH,
|
|
|
|
self::OPERATOR_CONTAINS,
|
|
|
|
self::OPERATOR_ENDS_WITH,
|
|
|
|
self::OPERATOR_IN_LAST,
|
|
|
|
self::OPERATOR_IS,
|
|
|
|
self::OPERATOR_IS_BETWEEN,
|
|
|
|
self::OPERATOR_IS_GREATER_THAN,
|
|
|
|
self::OPERATOR_IS_LESS_THAN,
|
|
|
|
self::OPERATOR_IS_NOT,
|
|
|
|
self::OPERATOR_NOT_CONTAIN,
|
|
|
|
self::OPERATOR_NOT_IN_LAST,
|
|
|
|
];
|
|
|
|
|
2021-10-08 18:23:45 +02:00
|
|
|
public ?int $id;
|
|
|
|
public string $operator;
|
|
|
|
public array $value;
|
|
|
|
public string $model;
|
2018-11-04 00:25:08 +01:00
|
|
|
|
|
|
|
private function __construct(array $config)
|
|
|
|
{
|
2021-06-05 12:47:56 +02:00
|
|
|
Assert::oneOf($config['operator'], self::VALID_OPERATORS);
|
2018-11-04 00:25:08 +01:00
|
|
|
|
2021-10-08 18:23:45 +02:00
|
|
|
$this->id = $config['id'] ?? null;
|
2018-11-04 00:25:08 +01:00
|
|
|
$this->value = $config['value'];
|
|
|
|
$this->model = $config['model'];
|
|
|
|
$this->operator = $config['operator'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function create(array $config): self
|
|
|
|
{
|
|
|
|
return new static($config);
|
|
|
|
}
|
|
|
|
|
2021-10-08 12:19:44 +02:00
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-10-08 18:23:45 +02:00
|
|
|
'id' => $this->id,
|
2021-10-08 12:19:44 +02:00
|
|
|
'model' => $this->model,
|
|
|
|
'operator' => $this->operator,
|
|
|
|
'value' => $this->value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:23:45 +02:00
|
|
|
/** @param array|self $rule */
|
|
|
|
public function equals($rule): bool
|
2021-10-08 12:19:44 +02:00
|
|
|
{
|
2021-10-08 18:23:45 +02:00
|
|
|
if (is_array($rule)) {
|
|
|
|
$rule = self::create($rule);
|
|
|
|
}
|
|
|
|
|
2021-10-08 12:19:44 +02:00
|
|
|
return $this->operator === $rule->operator
|
2021-10-08 18:23:45 +02:00
|
|
|
&& !array_diff($this->value, $rule->value)
|
2021-10-08 12:19:44 +02:00
|
|
|
&& $this->model === $rule->model;
|
|
|
|
}
|
2018-11-04 00:25:08 +01:00
|
|
|
}
|