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

[8.x] Added whenContains and whenContainsAll to Stringable #40285

Merged
merged 1 commit into from
Jan 7, 2022

Conversation

telkins
Copy link
Contributor

@telkins telkins commented Jan 7, 2022

I mainly did this because I wanted to combine when() and contains(): whenContains(). I added whenContainsAll() since it was quite similar.

I would also have taken the time to implement whenXyz() for all of the "check-like" methods on the class:

  • endsWith()
  • exactly()
  • is()
  • isAscii()
  • isUuid()
  • test()
  • startsWith()

Please let me know if it's worthwhile or not. I can definitely see it being valuable, so I'm happy to add these as well. 🤓

@telkins
Copy link
Contributor Author

telkins commented Jan 7, 2022

I realized that I ought to maybe make an example to demonstrate how this can help.

Unless I'm missing something, here's an example of how things might have to be done before this PR:

$stringable = Str::of('some important announcement');

if ($stringable->contains(['important', 'emergency'])) {
    $stringable->upper();
}

return (string) $stringable;

// SOME IMPORTANT ANNOUNCEMENT

After:

return (string) Str::of('some important announcement')
    ->whenContains(
        ['important', 'emergency'],
        fn (Stringable $stringable) => $stringable->upper(),
    );
}

// SOME IMPORTANT ANNOUNCEMENT

@taylorotwell taylorotwell merged commit 310c270 into laravel:8.x Jan 7, 2022
@GrahamCampbell GrahamCampbell changed the title Added whenContains and whenContainsAll to Stringable. [8.x] Added whenContains and whenContainsAll to Stringable Jan 7, 2022
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.

2 participants