48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\SearchRequest;
|
|
|
|
class ParsedSearchParameter
|
|
{
|
|
public string|\Stringable $operator;
|
|
|
|
public string|\Stringable $condition;
|
|
|
|
public function __construct(
|
|
public readonly SearchMethodEnum $searchMethod,
|
|
public readonly string|\Stringable $field,
|
|
public readonly mixed $value,
|
|
) {}
|
|
|
|
public function value() : mixed{
|
|
return match ($this->searchMethod) {
|
|
SearchMethodEnum::Like => "%$this->value%",
|
|
SearchMethodEnum::LikeLeft => "%$this->value",
|
|
SearchMethodEnum::LikeRight => "$this->value%",
|
|
default => $this->value,
|
|
};
|
|
}
|
|
|
|
public function operator() : string {
|
|
if (isset($this->operator)) {
|
|
return $this->operator;
|
|
}
|
|
|
|
return match ($this->searchMethod) {
|
|
SearchMethodEnum::Like => SearchRequestInterface::SEARCH_REQUEST_OPERATOR_LIKE,
|
|
default => SearchRequestInterface::SEARCH_REQUEST_OPERATOR_EQUAL,
|
|
};
|
|
}
|
|
|
|
|
|
public function condition() : string {
|
|
if (isset($this->condition)) {
|
|
return $this->condition;
|
|
}
|
|
|
|
return match ($this->searchMethod) {
|
|
SearchMethodEnum::Like => SearchRequestInterface::SEARCH_REQUEST_CONDITION_OR,
|
|
default => SearchRequestInterface::SEARCH_REQUEST_CONDITION_AND,
|
|
};
|
|
}
|
|
} |