fix text fields and exists operator

This commit is contained in:
2023-10-25 14:19:44 +03:00
parent 535266db64
commit 944ba96d53
6 changed files with 20 additions and 58 deletions
+12 -50
View File
@@ -198,35 +198,16 @@ final class FilterParser
private function parseAnd(Builder $builder, array $arguments): Builder
{
$ignoredFilters = [];
foreach ($arguments as $argument) {
if (in_array($argument->field, $ignoredFilters)) {
continue;
} else if ($argument->operator == "in") {
if ($argument->operator == "in") {
$builder->whereIn($argument->field, $argument->value);
} else if ($argument->operator == "nin") {
$builder->whereNotIn($argument->field, $argument->value);
} elseif ($argument->operator == "eqobject") {
$object = $argument->value;
// unset related filters used here
$addToIgnored = collect($arguments)
->filter(fn(Argument $f) => str_starts_with($f->field, $object))
->values()
->map(fn($f) => $f->field)
->toArray();
$ignoredFilters = array_merge($ignoredFilters, $addToIgnored);
$objectFilters = collect($arguments)
->filter(fn($f) => str_starts_with($f->field, $object))
->values()
->reduce(function ($c, $f) use ($object) {
$field = str_replace($object . "->", "", $f->field);
$c[$field] = $f->value;
return $c;
});
} else if ($argument->operator == "exists") {
$builder->where($argument->field, "!=", "");
$builder->where($argument->field, "!=", null);
} elseif ($argument->operator == "filter") {
$builder->whereJsonContains($argument->field, [$argument->value]);
// target result
// filter[data.previousNames_object]=previousNames&filter[previousNames.name_eq]=alpha&filter[previousNames.id_eqnum]=24
// $query->whereJsonContains("data->previousNames", [["name" => "alpha", "id" => 24]]);
@@ -245,35 +226,16 @@ final class FilterParser
private function parseOr(Builder $builder, array $arguments): Builder
{
$builder->where(function (Builder $orBuilder) use ($arguments) {
$ignoredFilters = [];
foreach ($arguments as $argument) {
if (in_array($argument->field, $ignoredFilters)) {
continue;
} else if ($argument->operator == "in") {
if ($argument->operator == "in") {
$orBuilder->orWhereIn($argument->field, $argument->value);
} else if ($argument->operator == "nin") {
$orBuilder->orWhereNotIn($argument->field, $argument->value);
} elseif ($argument->operator == "eqobject") {
$object = $argument->value;
// unset related filters used here
$addToIgnored = collect($arguments)
->filter(fn(Argument $f) => str_starts_with($f->field, $object))
->values()
->map(fn($f) => $f->field)
->toArray();
$ignoredFilters = array_merge($ignoredFilters, $addToIgnored);
$objectFilters = collect($arguments)
->filter(fn($f) => str_starts_with($f->field, $object))
->values()
->reduce(function ($c, $f) use ($object) {
$field = str_replace($object . "->", "", $f->field);
$c[$field] = $f->value;
return $c;
});
} else if ($argument->operator == "exists") {
$orBuilder->where($argument->field, "!=", "");
$orBuilder->where($argument->field, "!=", null);
} elseif ($argument->operator == "filter") {
$orBuilder->whereJsonContains($argument->field, [$argument->value]);
// target result
// filter[data.previousNames_object]=previousNames&filter[previousNames.name_eq]=alpha&filter[previousNames.id_eqnum]=24
// $query->whereJsonContains("data->previousNames", [["name" => "alpha", "id" => 24]]);
+3 -3
View File
@@ -60,11 +60,11 @@ final class Operator
db: '$ne',
uis: ["number"],
),
"object" => new Operator(
"filter" => new Operator(
name: "object",
label: "Equals Object",
symbol: "is",
db: 'eqobject',
db: 'filter',
uis: [],
),
"eqtrue" => new Operator(
@@ -169,7 +169,7 @@ final class Operator
name: "exists",
label: "Exists",
symbol: "exists",
db: '$exists',
db: 'exists',
uis: ["*"],
),
"nexists" => new Operator(
+2 -2
View File
@@ -26,10 +26,10 @@ class Record implements JsonSerializable
return trim(Str::lower(collect($arrObject)
->map(function($value){
if(!is_string($value)){
if(is_array($value)){
return $this->indexValues($value ?? []);
}
return $value;
return (string)$value;
})
->values()->join(" ")." ". $this->_file?->originalName ?? ""));
}
+1 -1
View File
@@ -32,7 +32,7 @@ class Slug implements FieldInterface, RequiredInterface
public function format(array $input, array $output): array
{
$value = (string)$input[$this->name] ?? null;
$value = !empty($input[$this->name]) ? (string)$input[$this->name] : null;
if(empty($value)){
$value = Str::slug($input[$this->source]);
}
+1 -1
View File
@@ -34,7 +34,7 @@ class Text implements FieldInterface, RequiredInterface
public function format(array $input, array $output): array
{
$value = (string)$input[$this->name] ?? null;
$value = !empty($input[$this->name]) ? (string)$input[$this->name] : null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
+1 -1
View File
@@ -31,7 +31,7 @@ class Textarea implements FieldInterface, RequiredInterface
public function format(array $input, array $output): array
{
$value = (string)$input[$this->name] ?? null;
$value = !empty($input[$this->name]) ? (string)$input[$this->name] : null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}