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

Find Method #14

Closed
TitanNano opened this issue Oct 18, 2017 · 11 comments
Closed

Find Method #14

TitanNano opened this issue Oct 18, 2017 · 11 comments

Comments

@TitanNano
Copy link

Please add a find method.

gries added a commit to gries/chain that referenced this issue Oct 18, 2017
Adds missing method to README.
@gries
Copy link
Contributor

gries commented Oct 18, 2017

Hi,
a search method is actually implemented but not documented.
$chain->search('foo');

@TitanNano
Copy link
Author

I'm looking for something like

$chain->find(function ($item) use ($id) {
    return $item['id'] === $id;
});

florianeckerstorfer added a commit that referenced this issue Oct 18, 2017
Add seach Method to README #14
@florianeckerstorfer
Copy link
Member

Sounds like a good idea. Should we make Cocur\Chain\Link\Search::search() accept a callback or add a separate function find()?

Opinions?

@TitanNano
Copy link
Author

to have one method is probably less confusing. So i'd say extend search()

@gries
Copy link
Contributor

gries commented Oct 18, 2017

hm changing the search method would either require a BC break or a very wonky signature:
search($needle, $strict = false, callable $matcher)

@florianeckerstorfer
Copy link
Member

I don't think this is a BC break. The signature of search is mixed, we check first if $needle is a callback and array_search in the else.

@gries
Copy link
Contributor

gries commented Oct 19, 2017

ok sounds good, maybe we can utilize http://php.net/manual/en/function.array-filter.php for the implementation as it nearly covers the functionality we're looking for.

@gries
Copy link
Contributor

gries commented Oct 19, 2017

i've written an implementation but I'm still not sure if search is the right way to go for,
the current behavior of search is that it returns the matching key of the item inside the array.

So:

$chain = new Chain['foo', 'bar'];
$chain->search('bar'); // returns 1

My current implementation behaves the same way:

$chain = new Chain['foo', 'bar'];
$chain->search(function($item) {
  return $item === 'bar';
}); // returns 1

I think what @TitanNano want's is to return 'bar'

@TitanNano
Copy link
Author

@gries yes that's what I'm looking for.

@florianeckerstorfer
Copy link
Member

Good point @gries . Then we should add a new method find().

florianeckerstorfer added a commit that referenced this issue Oct 20, 2017
@florianeckerstorfer
Copy link
Member

Fixed in #17, thanks @gries

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

No branches or pull requests

3 participants