id = $config['id'] ?? null; $this->value = $config['value']; $this->model = $config['model']; $this->operator = $config['operator']; } public static function assertConfig(array $config, bool $allowUserIdModel = true): void { Assert::oneOf($config['operator'], self::VALID_OPERATORS); Assert::oneOf( $config['model'], $allowUserIdModel ? array_prepend(self::VALID_MODELS, self::MODEL_USER_ID) : self::VALID_MODELS ); Assert::isArray($config['value']); Assert::countBetween($config['value'], 1, 2); } public static function create(array $config): self { return new self($config); } /** @return array */ public function toArray(): array { return [ 'id' => $this->id, 'model' => $this->model, 'operator' => $this->operator, 'value' => $this->value, ]; } /** @param array|self $rule */ public function equals($rule): bool { if (is_array($rule)) { $rule = self::create($rule); } return $this->operator === $rule->operator && !array_diff($this->value, $rule->value) && $this->model === $rule->model; } }