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 private function parseAnd(Builder $builder, array $arguments): Builder
{ {
$ignoredFilters = [];
foreach ($arguments as $argument) { foreach ($arguments as $argument) {
if (in_array($argument->field, $ignoredFilters)) { if ($argument->operator == "in") {
continue;
} else if ($argument->operator == "in") {
$builder->whereIn($argument->field, $argument->value); $builder->whereIn($argument->field, $argument->value);
} else if ($argument->operator == "nin") { } else if ($argument->operator == "nin") {
$builder->whereNotIn($argument->field, $argument->value); $builder->whereNotIn($argument->field, $argument->value);
} elseif ($argument->operator == "eqobject") { } else if ($argument->operator == "exists") {
$builder->where($argument->field, "!=", "");
$object = $argument->value; $builder->where($argument->field, "!=", null);
// unset related filters used here } elseif ($argument->operator == "filter") {
$addToIgnored = collect($arguments) $builder->whereJsonContains($argument->field, [$argument->value]);
->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;
});
// target result // target result
// filter[data.previousNames_object]=previousNames&filter[previousNames.name_eq]=alpha&filter[previousNames.id_eqnum]=24 // filter[data.previousNames_object]=previousNames&filter[previousNames.name_eq]=alpha&filter[previousNames.id_eqnum]=24
// $query->whereJsonContains("data->previousNames", [["name" => "alpha", "id" => 24]]); // $query->whereJsonContains("data->previousNames", [["name" => "alpha", "id" => 24]]);
@@ -245,35 +226,16 @@ final class FilterParser
private function parseOr(Builder $builder, array $arguments): Builder private function parseOr(Builder $builder, array $arguments): Builder
{ {
$builder->where(function (Builder $orBuilder) use ($arguments) { $builder->where(function (Builder $orBuilder) use ($arguments) {
$ignoredFilters = [];
foreach ($arguments as $argument) { foreach ($arguments as $argument) {
if (in_array($argument->field, $ignoredFilters)) { if ($argument->operator == "in") {
continue;
} else if ($argument->operator == "in") {
$orBuilder->orWhereIn($argument->field, $argument->value); $orBuilder->orWhereIn($argument->field, $argument->value);
} else if ($argument->operator == "nin") { } else if ($argument->operator == "nin") {
$orBuilder->orWhereNotIn($argument->field, $argument->value); $orBuilder->orWhereNotIn($argument->field, $argument->value);
} elseif ($argument->operator == "eqobject") { } else if ($argument->operator == "exists") {
$orBuilder->where($argument->field, "!=", "");
$object = $argument->value; $orBuilder->where($argument->field, "!=", null);
// unset related filters used here } elseif ($argument->operator == "filter") {
$addToIgnored = collect($arguments) $orBuilder->whereJsonContains($argument->field, [$argument->value]);
->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;
});
// target result // target result
// filter[data.previousNames_object]=previousNames&filter[previousNames.name_eq]=alpha&filter[previousNames.id_eqnum]=24 // filter[data.previousNames_object]=previousNames&filter[previousNames.name_eq]=alpha&filter[previousNames.id_eqnum]=24
// $query->whereJsonContains("data->previousNames", [["name" => "alpha", "id" => 24]]); // $query->whereJsonContains("data->previousNames", [["name" => "alpha", "id" => 24]]);
+3 -3
View File
@@ -60,11 +60,11 @@ final class Operator
db: '$ne', db: '$ne',
uis: ["number"], uis: ["number"],
), ),
"object" => new Operator( "filter" => new Operator(
name: "object", name: "object",
label: "Equals Object", label: "Equals Object",
symbol: "is", symbol: "is",
db: 'eqobject', db: 'filter',
uis: [], uis: [],
), ),
"eqtrue" => new Operator( "eqtrue" => new Operator(
@@ -169,7 +169,7 @@ final class Operator
name: "exists", name: "exists",
label: "Exists", label: "Exists",
symbol: "exists", symbol: "exists",
db: '$exists', db: 'exists',
uis: ["*"], uis: ["*"],
), ),
"nexists" => new Operator( "nexists" => new Operator(
+2 -2
View File
@@ -26,10 +26,10 @@ class Record implements JsonSerializable
return trim(Str::lower(collect($arrObject) return trim(Str::lower(collect($arrObject)
->map(function($value){ ->map(function($value){
if(!is_string($value)){ if(is_array($value)){
return $this->indexValues($value ?? []); return $this->indexValues($value ?? []);
} }
return $value; return (string)$value;
}) })
->values()->join(" ")." ". $this->_file?->originalName ?? "")); ->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 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)){ if(empty($value)){
$value = Str::slug($input[$this->source]); $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 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(); $output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output; return $output;
} }
+1 -1
View File
@@ -31,7 +31,7 @@ class Textarea implements FieldInterface, RequiredInterface
public function format(array $input, array $output): array 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(); $output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output; return $output;
} }