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 first() and last() functions to Collection #948

Conversation

baijunyao
Copy link
Contributor

No description provided.

@baijunyao baijunyao force-pushed the feature/add-first-and-last-funciton-to-collection branch from 97e609f to e862605 Compare June 11, 2020 10:08
lib/Collection.php Outdated Show resolved Hide resolved
lib/Collection.php Outdated Show resolved Hide resolved
@richardm-stripe
Copy link
Contributor

richardm-stripe commented Jun 15, 2020

Hi @baijunyao, thank you for the pull request. I think these methods could be useful, but I suggested some modifications to the docstring to clarify that they operate on the current page, not the collection. Does this make sense to you?

@baijunyao
Copy link
Contributor Author

@richardm-stripe I agree, thank you very much.

@baijunyao baijunyao force-pushed the feature/add-first-and-last-funciton-to-collection branch from db27b66 to f6924ca Compare June 16, 2020 01:05
@richardm-stripe
Copy link
Contributor

Hi @baijunyao, still thinking about this.

I'm a little uneasy about using \end for ->last() and \reset for ->first() since they mutate.

That is, if somebody did

$custs = $stripe->customers->all(['limit' => 5]);

\next($custs->data);
var_dump(\current($custs->data)->id); // Customer 2
var_dump($custs->first()); // Customer 1
var_dump(\current($cs->data)->id); // Customer 1

They might expect this to print "Customer 2, Customer 1, Customer 2" but this will actually print "Customer 2, Customer 1, Customer 1".

(This is sort of contrived, but a scenario like this could matter especially if some of these calls were hidden inside some user's helper functions).

What do you think about implementing as \count($this->data) > 0 ? $this->data[0] : false and \count($this->data) > 0 ? $this->data[\count($this->data) - 1] : false, instead?

@baijunyao
Copy link
Contributor Author

You're right, but there may still be problems.

public function first()
{
    return \count($this->data) > 0 ? $this->data[0] : false;
}
$custs = $stripe->customers->all(['limit' => 5]);

unset($collection->data[0]);

var_dump($custs->first()); // Undefined offset: 0

So what do you think of changing to the following way?

public function first()
{
    foreach ($this->data as $item) {
        return $item;
    }

    return false;
}

public function last()
{
    foreach (array_reverse($this->data, true) as $item) {
        return $item;
    }

    return false;
}

And If $this->data is empty, is it better to return null than false?

@richardm-stripe
Copy link
Contributor

Hi @baijunyao, sorry for the delay. I do agree null is better than false.

I don't think Undefined offset: 0 is necessarily unexpected after calling unset, and array_reverse seems like it's doing more work than it should to navigate to the last (set) index. I think I still prefer \count($this->data) > 0 ? $this->data[0] : null.

baijunyao and others added 3 commits July 7, 2020 09:29
Co-authored-by: richardm-stripe <52928443+richardm-stripe@users.noreply.github.com>
Co-authored-by: richardm-stripe <52928443+richardm-stripe@users.noreply.github.com>
@baijunyao baijunyao force-pushed the feature/add-first-and-last-funciton-to-collection branch from f6924ca to 409c8ae Compare July 7, 2020 01:36
@baijunyao
Copy link
Contributor Author

Never mind.
Okay, I updated the code.
Thanks.

Copy link
Contributor

@ctrudeau-stripe ctrudeau-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @baijunyao

@richardm-stripe richardm-stripe merged commit 935036f into stripe:master Jul 20, 2020
@richardm-stripe
Copy link
Contributor

(This was released in v7.44.0. Thank you again @baijunyao for the contribution)

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

Successfully merging this pull request may close these issues.

3 participants