vinkla / extended-acf

Register advanced custom fields with object-oriented PHP
MIT License
455 stars 61 forks source link

Float type hints for min() and max() function #121

Closed menno-ll closed 1 year ago

menno-ll commented 1 year ago

Fixes https://github.com/vinkla/extended-acf/issues/120

Changes the typehints for the min and max field settings to be float instead of int.

Allows setting options like this:

$field = Number::make( _x( 'Some label', 'Fields', 'some-textdomain' ), 'some-field-name' )
    ->min( 0.5 ) // This
    ->max( 1.5 ) // And this
    ->step( 0.1 )
    ->defaultValue( 1.0 )
    ->required();
menno-ll commented 1 year ago

You were so fast fast with the response, i could not fix it that fast ;) Thanks for looking so quickly

vinkla commented 1 year ago

Thanks for the pull request Menno!

menno-ll commented 1 year ago

Please note that this change might be breaking for people now providing integers in there.

vinkla commented 1 year ago

I don't think that is the case. Tried it locally and it seems to be working fine. How would it break?

Number::make('Number')->min(1)->max(10);
menno-ll commented 1 year ago

Nope, you are right! Also tested locally (PHP 8.0) and didn't break.

I was thinking if you force cast a variable to an int, and provide that, it would crash. Like so:

$field = Number::make( _x( 'Some label', 'Fields', 'some-textdomain' ), 'some-field-name' )
    ->min( (int) 0.5 )
    ->max( (int) 1.5 )
    ->step( 0.1 )
    ->defaultValue( 1.0 )
    ->required();

But apparently PHP converts it automatically, so there's no problem there! So ignore my comment, my assumption was wrong.