cocur/chain

Find Method

TitanNano opened this issue ยท 11 comments

Please add a find method.

gries commented

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

I'm looking for something like

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

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

Opinions?

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

gries commented

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

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 commented

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 commented

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'

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

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

Fixed in #17, thanks @gries