Skip to content

Conversation

pirs1337
Copy link
Contributor

@pirs1337 pirs1337 commented Jul 4, 2022

No description provided.

@pirs1337 pirs1337 self-assigned this Jul 4, 2022
@pirs1337 pirs1337 assigned DenTray and unassigned pirs1337 Jul 4, 2022
Comment on lines 36 to 37
'from',
'to'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these reserved filters?


if (!empty($this->filter)) {
foreach ($this->filter as $field => $values) {
if (!in_array($field, $this->reservedFilters) && is_array($values) || !in_array($field, $this->reservedFilters) && is_string($values)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!in_array($field, $this->reservedFilters) && is_array($values) || !in_array($field, $this->reservedFilters) && is_string($values)) {
if (!in_array($field, $this->reservedFilters) && (is_array($values) || is_string($values)) {

Comment on lines 106 to 122
protected function filter($postfix, $where, $field, $values)
{
$fieldWithoutPostfix = Str::replace($postfix, '', $field);

if (!is_array($values) || !Arr::isAssoc($values)) {
$this->query->{$where}($fieldWithoutPostfix, Arr::wrap($values));
} else {
foreach ($values as $relationFiled => $value) {
$fieldWithRelation = $fieldWithoutPostfix . '.' . $relationFiled;
}

$this->applyWhereCallback($this->query, $fieldWithRelation, function (&$query, $conditionField) use ($value, $where) {
$query->{$where}($conditionField, Arr::wrap($value));
});
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something strange happens here, let's implement simple logic

public function searchQuery(array $filter): self
{
     // some initial logic

    foreach($filters as $fieldName => $value) {
        if ($isNotReserverFilter || Str::endWith($field, 'in_list')) {
            $field = Str::replace($field, 'in_list', '');
            $this->filterBy($field, $value);
        } elseif (Str::endWith($field, 'not_in_list')) {
            $this->query->whereNotIn($field, $value);
        } elseif (Str::endWith($field, '_from')) {
            $field = Str::replace($field, '_from', '');
            $this->filterFrom($field, $value);
        } elseif (Str::endWith($field, '_to')) {
            $field = Str::replace($field, '_to', '');
            $this->filterTo($field, $value);
        }
    }
}

@DenTray DenTray assigned pirs1337 and unassigned DenTray Jul 6, 2022
@pirs1337 pirs1337 assigned DenTray and unassigned pirs1337 Jul 8, 2022

$this->filter = $filter;

if (!empty($filter)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's set the default value for the $filter argument to [] it will allow to skip thid check for empty

foreach($filter as $fieldName => $value) {
$isNotReservedFilter = (!in_array($fieldName, $this->reservedFilters));

$isValidValue = (is_array($value) || is_string($value) || is_integer($value) || is_double($value) || is_bool($value));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove as we discussed

@DenTray DenTray assigned pirs1337 and unassigned DenTray Jul 8, 2022
@pirs1337 pirs1337 assigned DenTray and unassigned pirs1337 Jul 8, 2022
Comment on lines 120 to 121
$this->filter[$field] = $value;
$this->filterFrom($field, true, $field);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->filter[$field] = $value;
$this->filterFrom($field, true, $field);
$this->filterFrom($field, false, $fieldName);

$this->filterTo($field, true, $field);
} elseif ($isNotReservedFilter || Str::endsWith($fieldName, '_in_list')) {
$field = Str::replace('_in_list', '', $fieldName);
$this->filter[$field] = $value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->filter[$field] = $value;

return $this;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@DenTray DenTray merged commit 3b128ab into master Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants