★ wanayoo — archive 1999 https://github.com/slimphp/Slim/pull/2250Nouvelle recherche | Portail wanayoo
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

Add `only` restriction to getParams() #2250

Merged
merged 2 commits into from Sep 21, 2017
Merged

Add `only` restriction to getParams() #2250

merged 2 commits into from Sep 21, 2017

Conversation

@Grafikart
Copy link

@Grafikart Grafikart commented Jun 18, 2017 •

Proposal

Enhanced Request::getParams() method with an extra argument to filter keys.

$request->getParams(['name', 'content']);

This will filter parameters exporting only the specified keys (defaulting to null if the key doesn't exist). I am unsure for the "defaulting to null" so feel free to comment or discard the whole PR if you think this feature should not be included by Slim.

Possible changes

  • Do not default to null if a key doesn't exist (let PHP throw an error or create a custom exception ?)
  • Replace the array argument with a spread operator or func_get_args so we can write $request->getParams('name', 'content');
@coveralls
Copy link

@coveralls coveralls commented Jun 18, 2017

Coverage Status

Coverage decreased (-0.5%) to 96.849% when pulling 25d59b6 on Grafikart:feat-params into 6987497 on slimphp:3.x.

@Grafikart
Copy link
Author

@Grafikart Grafikart commented Jun 18, 2017

I don't get the change in coveralls % since my changes are covered by new tests :(

*/
public function getParams()
public function getParams($only = null)

This comment has been minimized.

@pachico

pachico Jun 19, 2017

Regardless of if this feature will be implemented or not, please correct to:

public function getParams(array $only = null)

Your unit tests should have probably tested also non expected inputs.

Cheers

This comment has been minimized.

@coveralls
Copy link

@coveralls coveralls commented Jun 19, 2017

Coverage Status

Coverage increased (+0.01%) to 97.321% when pulling dde1ba9 on Grafikart:feat-params into 6987497 on slimphp:3.x.

@geggleto geggleto added the Slim 3 label Jul 20, 2017
@akrabat
Copy link
Member

@akrabat akrabat commented Aug 13, 2017

I'm on the fence about this one as I'm not sure it adds enough that it should be part of Slim or whether it should be in user-land.

@akrabat akrabat self-assigned this Aug 13, 2017
@danopz
Copy link
Member

@danopz danopz commented Aug 14, 2017

Should be user-land IMHO.

@silentworks
Copy link
Member

@silentworks silentworks commented Aug 14, 2017

This is not that easy to implement in user-land due to our current design and currently it won't break anything in core and its not doing any harm adding it in.

@geggleto
Copy link
Member

@geggleto geggleto commented Aug 14, 2017

I would like to see this PR implement the 2nd possible change.

@Grafikart
Copy link
Author

@Grafikart Grafikart commented Aug 14, 2017

I can implement the 2nd possible change but do I keep the "first argument as array" ? or only the spread operator ?

@akrabat
Copy link
Member

@akrabat akrabat commented Aug 15, 2017 •

This is interesting :)

If the key doesn't exist then it shouldn't be in the returned array as otherwise getParams() and getParams(['not_there']) will return different results which is inconsistent.

I disagree with @geggleto on point 2 as I think that getParams('foo', 'bar') isn't as clear that you're selecting a subset from the parameters array.

Finally, I should note that the user land implementation is a one liner:

$some = array_filter($request->getParams(), function ($k) use ($only) { return in_array($k, $only); }, ARRAY_FILTER_USE_KEY);
@geggleto
Copy link
Member

@geggleto geggleto commented Aug 22, 2017

@akrabat that is not a one-liner ;)

I am torn on the 2nd point; I could go either way with it.

@akrabat akrabat merged commit dde1ba9 into slimphp:3.x Sep 21, 2017
3 checks passed
3 checks passed
Scrutinizer 4 new issues, 1 updated code elements
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.01%) to 97.321%
Details
akrabat added a commit that referenced this pull request Sep 21, 2017
Closes #2250
@akrabat
Copy link
Member

@akrabat akrabat commented Sep 21, 2017

Merged after changing to not provide a default of null for '$only` keys that don't exist in the params list.

@akrabat
Copy link
Member

@akrabat akrabat commented Sep 21, 2017

Thanks @Grafikart for your work on this.

@akrabat akrabat added this to the 3.9.0 milestone Sep 21, 2017
@akrabat akrabat changed the title Add only restriction to getParams() Add `only` restriction to getParams() Nov 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants