refactoring of filters
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Query\BuilderConverter;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Lucent\Query\Filter\Argument;
|
||||
|
||||
readonly class LessThanEquals implements IBuilderConverter
|
||||
{
|
||||
|
||||
public function __construct(private Argument $argument)
|
||||
{
|
||||
}
|
||||
|
||||
public function toAndQueryBuilder(Builder $builder): Builder
|
||||
{
|
||||
|
||||
return $builder->where($this->argument->field, "<=", $this->formatValue());
|
||||
}
|
||||
|
||||
public function toOrQueryBuilder(Builder $builder): Builder
|
||||
{
|
||||
return $builder->orWhere($this->argument->field, "<=", $this->formatValue());
|
||||
}
|
||||
|
||||
private function formatValue(): int|float
|
||||
{
|
||||
$value = trim($this->argument->value);
|
||||
if (is_numeric($value)) {
|
||||
return str_contains($value, ".") ? floatval($value) : intval($value);
|
||||
}
|
||||
return $value;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user