Open nozavroni opened 6 years ago
As a matter of fact, in my library, I made getPosition throw an exception so I could make this even simpler:
$items = Factory::create($arr);
try {
$column = $items->getPosition(-5);
} except (Collection\Exception\InvalidPositionException $e) {
// there is no fifth-from-last column
}
Sometimes I have found it useful to determine if a collection has an item at a certain position, regardless of index/key. I used it in my csv library to be able to easily determine if there was an item in the "3rd" column for instance. Normally I would have to do something like this:
When I was writing my own collection library I simply implemented these methods to make this process simpler:
I also made it work with negative numbers to index from the end rather than the beginning.
@Plopix Do you see any value in this? Your thoughts? Perhaps you already have a solution for something like this? I think the name could be shorter. Something like "hasPos()" and "getPos()".