Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NumericRange filter which gives the possibility of filtering … #146

Merged

Conversation

christopherhero
Copy link
Contributor

@christopherhero christopherhero commented Mar 29, 2021

Q A
Branch? 1.13
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets #
License MIT

While working on adding a custom filter, I thought that it could be in the bundle.
I add range numerical filter which can filter on numerical fields in range (from-to) Like existing DateFilter.

Also in this pull request is a small change in two classes:

  • Doctrine\ORM\ExpressionBuilder
  • Doctrine\ORM\DataSource.

I suggested adding filtering capability also when using HAVING instead of WHERE in the query. For example, when we use count (*). In current architecture is hard to distinguish the type of condition (WHERE or HAVING). Another way than suggested by me is to add pubilic methods e.g havingLessThan to ORM\ExpressionBuilder and new interface "HavingExpressionBuilder"

Please give your opinion what do you think about it?

This idea also came while working on this filter because I had to filter in queries with the HAVING clause.

@christopherhero christopherhero marked this pull request as ready for review March 29, 2021 03:12
@Roshyo
Copy link
Contributor

Roshyo commented Feb 15, 2022

Hi @christopherhero and thank you for your PR.

But I feel this one is a bit too complex for the value it should bring.
I would also love to have a numeric filter out of the box. But IMO it should just look like the current MoneyFilter without the currency part. What do you think ?

@christopherhero christopherhero force-pushed the add_numerical_range_filter branch 2 times, most recently from 3b8e339 to cf854c9 Compare June 19, 2023 08:01
@christopherhero
Copy link
Contributor Author

@Roshyo Hi thanks :)

I have lightly changed RangeNumericalFiolter to NumericalFIlter (I also removed the logic for the expression builder, I will add that in a separate pr)

@christopherhero
Copy link
Contributor Author

@Roshyo what you think now :) ?

@Roshyo
Copy link
Contributor

Roshyo commented Jun 27, 2023

LGTM. Let's wait for another approval before merging

@Roshyo Roshyo requested a review from a team June 27, 2023 09:59
@Roshyo
Copy link
Contributor

Roshyo commented Jun 27, 2023

@christopherhero Could you try to rebase on 1.13?

@christopherhero christopherhero changed the base branch from master to 1.13 August 3, 2023 23:25
@christopherhero
Copy link
Contributor Author

@Roshyo updated

@christopherhero christopherhero force-pushed the add_numerical_range_filter branch 2 times, most recently from 8595a1b to 6222fd8 Compare September 15, 2023 14:36
@diimpp
Copy link
Member

diimpp commented Sep 15, 2023

+1 for adding numeric filter, current list looks lonely without it 😃

I've a naming concern, it feels more like RangeFilter, rather than NumericFilter.

How about to synchronize naming and capabilities with api-platform?
https://api-platform.com/docs/core/filters/#numeric-filter <eq>
https://api-platform.com/docs/core/filters/#range-filter <lt|gt|lte|gte|between>

@christopherhero christopherhero force-pushed the add_numerical_range_filter branch 2 times, most recently from eb5262c to 4d7c3c8 Compare September 28, 2023 00:53
@christopherhero christopherhero changed the title Added RangeNumerical filter which gives the possibility of filtering … Added RangeNumeric filter which gives the possibility of filtering … Sep 28, 2023
@christopherhero
Copy link
Contributor Author

@diimpp Thanks for review :)
Regarding Api platfrom, could you give some example of what you mean ?
Because I don't fully understand, thanks :)

@christopherhero
Copy link
Contributor Author

@diimpp What else to change :) ?

Copy link
Member

@diimpp diimpp left a comment

Choose a reason for hiding this comment

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

Hi @christopherhero, sorry for late reply. 🙏

Please see the comments.

Filter should allow to specify comparison strictness, like lt|gt|lte|gte, e.g. greater than and greater than or equel

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class RangeNumericFilterType extends AbstractType
Copy link
Member

Choose a reason for hiding this comment

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

Either

Suggested change
final class RangeNumericFilterType extends AbstractType
final class NumericRangeFilterType extends AbstractType

or

Suggested change
final class RangeNumericFilterType extends AbstractType
final class RangeFilterType extends AbstractType

@loic425 Hi, wdyt?

->add('greaterThan', NumberType::class, [
'label' => 'sylius.ui.greater_than',
'required' => false,
'scale' => $options['scale'],
Copy link
Member

Choose a reason for hiding this comment

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

Scale depends on rounding mode, so it should be possible to configure as well.

image

https://symfony.com/doc/current/reference/forms/types/number.html#rounding-mode

->add('lessThan', NumberType::class, [
'label' => 'sylius.ui.less_than',
'required' => false,
'scale' => $options['scale'],
Copy link
Member

Choose a reason for hiding this comment

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

Same

@@ -67,6 +67,16 @@
</service>
<service id="sylius.form.type.grid_filter.exists" alias="Sylius\Bundle\GridBundle\Form\Type\Filter\ExistsFilterType" />

<service id="Sylius\Component\Grid\Filter\RangeNumericFilter">
<tag name="sylius.grid_filter" type="numeric" form-type="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType" />
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<tag name="sylius.grid_filter" type="numeric" form-type="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType" />
<tag name="sylius.grid_filter" type="numeric_range" form-type="Sylius\Bundle\GridBundle\Form\Type\Filter\NumericRangeFilterType" />
Suggested change
<tag name="sylius.grid_filter" type="numeric" form-type="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType" />
<tag name="sylius.grid_filter" type="range" form-type="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeFilterType" />


public function getBlockPrefix(): string
{
return 'sylius_grid_filter_range_numeric';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return 'sylius_grid_filter_range_numeric';
return 'sylius_grid_filter_numeric_range';
Suggested change
return 'sylius_grid_filter_range_numeric';
return 'sylius_grid_filter_range';

<service id="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType">
<tag name="form.type" />
</service>
<service id="sylius.form.type.grid_filter.numeric" alias="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType" />
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<service id="sylius.form.type.grid_filter.numeric" alias="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType" />
<service id="sylius.form.type.grid_filter.numeric_range" alias="Sylius\Bundle\GridBundle\Form\Type\Filter\NumericRangeFilterType" />
Suggested change
<service id="sylius.form.type.grid_filter.numeric" alias="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeNumericFilterType" />
<service id="sylius.form.type.grid_filter.range" alias="Sylius\Bundle\GridBundle\Form\Type\Filter\RangeFilterType" />

src/Component/Filter/RangeNumericFilter.php Outdated Show resolved Hide resolved
return;
}

$field = $options['field'] ?? $name;
Copy link
Member

Choose a reason for hiding this comment

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

I assume if I will write field: 1, then it would be int and we need string. Date filter do this conversion as well.

Suggested change
$field = $options['field'] ?? $name;
$field = (string) $options['field'] ?? $name;


final class RangeNumericFilter implements FilterInterface
{
public const DEFAULT_SCALE = 0;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder which default scale would be sensible.

@Sylius/development-team wdyt?

SF numeric form type uses 3.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think 0 is the most sensible. I don't think filtering with decimal precision is more common than with whole numbers.

Comment on lines 50 to 52
/**
* @param string[] $data
*/
Copy link
Member

Choose a reason for hiding this comment

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

Since phpstan now understands more complex docblocks, something like this should be done.

Suggested change
/**
* @param string[] $data
*/
/** @param array<array-key, string> $data */

@christopherhero christopherhero changed the title Added RangeNumeric filter which gives the possibility of filtering … Added NumericRange filter which gives the possibility of filtering … Jan 15, 2024
@christopherhero
Copy link
Contributor Author

@diimpp

@christopherhero
Copy link
Contributor Author

what do you think about it now ;)
@diimpp @vvasiloi

@christopherhero
Copy link
Contributor Author

@vvasiloi

Copy link
Contributor

@vvasiloi vvasiloi left a comment

Choose a reason for hiding this comment

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

Only 2 more observations I missed in the previous review, but it can be merged even without addressing those.

src/Component/Filter/NumericRangeFilter.php Show resolved Hide resolved
src/Component/Filter/NumericRangeFilter.php Outdated Show resolved Hide resolved
…n the range (gt|gte|lt|lte) from to on numeric type fields.
@NoResponseMate NoResponseMate merged commit e079cfb into Sylius:1.13 Feb 22, 2024
7 checks passed
@NoResponseMate
Copy link
Contributor

Thank you, @christopherhero!

@NoResponseMate NoResponseMate added the Feature New feature proposals. label Feb 22, 2024
@vvasiloi
Copy link
Contributor

Thank you @christopherhero, for having patience and finishing the work even after so much time passed! 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature proposals.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants