From 310c270f5f98d5c74fc44798a24284e51b22a732 Mon Sep 17 00:00:00 2001 From: Travis Elkins Date: Fri, 7 Jan 2022 16:14:18 +0100 Subject: [PATCH] Added whenContains and whenContainsAll to Stringable. (#40285) --- src/Illuminate/Support/Stringable.php | 46 +++++++++++++++++++++++++ tests/Support/SupportStringableTest.php | 38 ++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 3489c1c60a65..d6b29f3ea107 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -765,6 +765,52 @@ public function ucfirst() return new static(Str::ucfirst($this->value)); } + /** + * Execute the given callback if the string contains a given substring. + * + * @param string|array $needles + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenContains($needles, $callback, $default = null) + { + if ($this->contains($needles)) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + + /** + * Execute the given callback if the string contains all array values. + * + * @param array $needles + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenContainsAll(array $needles, $callback, $default = null) + { + if ($this->containsAll($needles)) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + /** * Execute the given callback if the string is empty. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 3d97b0cb3004..be19531aec0e 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -112,6 +112,44 @@ public function testUnless() })); } + public function testWhenContains() + { + $this->assertSame('Tony Stark', (string) $this->stringable('stark')->whenContains('tar', function ($stringable) { + return $stringable->prepend('Tony ')->title(); + }, function ($stringable) { + return $stringable->prepend('Arno ')->title(); + })); + + $this->assertSame('stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) { + return $stringable->prepend('Tony ')->title(); + })); + + $this->assertSame('Arno Stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) { + return $stringable->prepend('Tony ')->title(); + }, function ($stringable) { + return $stringable->prepend('Arno ')->title(); + })); + } + + public function testWhenContainsAll() + { + $this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'stark'], function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + + $this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenContainsAll(['xxx'], function ($stringable) { + return $stringable->title(); + })); + + $this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'xxx'], function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + } + public function testWhenEmpty() { tap($this->stringable(), function ($stringable) {