codefog / contao-haste

Haste is a collection of tools and classes to ease working with Contao
http://codefog.pl/extension/haste.html
MIT License
43 stars 24 forks source link

Add default value support for Input::get and Input::post #92

Closed Tastaturberuf closed 7 years ago

Tastaturberuf commented 7 years ago

Now we can use

Input::get('key', 'defaultValue')

instead of

Input::get('key') ?: 'defaultValue'

It's more readable when you use it directly as params like

Model::findByDate(Input::get('date', date('Y-m-d'))

instead of

Model::findByDate(Input::get('date') ?: date('Y-m-d'))

I know this is a very little change but i use this very often and get confused from the syntax.

qzminski commented 7 years ago

Your code is a BC break. If I used \Haste\Input\Input::post('foobar', true) my code will no longer work. Thus I think you should move the default parameter to the last position in the arguments list... but then it would be uncomfortable to use it.

It works in Symfony because the getter there does not accept extra params such as decodeEntities or has consistent argument order, e.g. $name, $default, $options. Not possible here in my opinion.

Tastaturberuf commented 7 years ago

Ooops. I don't think enough about that. One possibility is to use new method names like

Input::getDefault(...)
Input::postDefault(...)
Input::postHtmlDefault(...)
Input::postRawDefault(...)
Input::postUnsafeRawDefault(...)

or use an additional Class like InputDefault.

What do you think about that?

qzminski commented 7 years ago

Yeah that would work but does it make sense to have yet another class just for that ternary operator? I mean I like the concept but only if it's in the same place (Symfony does that), i.e. same class/method as regular fetch of the value. The separate class just for that does not look handy to me.

@Toflar @aschempp

Toflar commented 7 years ago

I don't see the advantage really. It's handy when it's the 2nd parameter as it is in SF but we cannot have that and I honestly don't see why we'd need that here.

Tastaturberuf commented 7 years ago

Yep, that isn't handy.

Nice try