From f34ffda2377d07c5e155fef2460338e14d59806f Mon Sep 17 00:00:00 2001 From: Travis Elkins Date: Fri, 7 Jan 2022 09:05:27 +0100 Subject: [PATCH 1/4] Added whenContains and whenContainsAll to Stringable. --- 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) { From 3e127ed261c9b09bd183ff685f7b4a34940b8ba4 Mon Sep 17 00:00:00 2001 From: Travis Elkins Date: Mon, 10 Jan 2022 10:23:54 +0100 Subject: [PATCH 2/4] Added whenEndsWith(), whenExactly(), whenStartsWith(), etc to Stringable. --- src/Illuminate/Support/Stringable.php | 159 ++++++++++++++++++++++++ tests/Support/SupportStringableTest.php | 149 ++++++++++++++++++++++ 2 files changed, 308 insertions(+) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index d6b29f3ea107..9fe53cbc0b79 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -811,6 +811,165 @@ public function whenContainsAll(array $needles, $callback, $default = null) return $this; } + /** + * Execute the given callback if the string ends with a given substring. + * + * @param string|array $needles + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenEndsWith($needles, $callback, $default = null) + { + if ($this->endsWith($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 an exact match with the given value. + * + * @param string $value + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenExactly($value, $callback, $default = null) + { + if ($this->exactly($value)) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + + /** + * Execute the given callback if the string matches a given pattern. + * + * @param string|array $pattern + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenIs($pattern, $callback, $default = null) + { + if ($this->is($pattern)) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + + /** + * Execute the given callback if the string is 7 bit ASCII. + * + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenIsAscii($callback, $default = null) + { + if ($this->isAscii()) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + + /** + * Execute the given callback if the string is a valid UUID. + * + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenIsUuid($callback, $default = null) + { + if ($this->isUuid()) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + + /** + * Execute the given callback if the string matches the given pattern. + * + * @param string $pattern + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenTest($pattern, $callback, $default = null) + { + if ($this->test($pattern)) { + $result = $callback($this); + + return $result ?? $this; + } elseif ($default) { + $result = $default($this); + + return $result ?? $this; + } + + return $this; + } + + /** + * Execute the given callback if the string starts with a given substring. + * + * @param string|array $needles + * @param callable $callback + * @param callable|null $default + * @return static + */ + public function whenStartsWith($needles, $callback, $default = null) + { + if ($this->startsWith($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 be19531aec0e..f3afdef9b18a 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -150,6 +150,155 @@ public function testWhenContainsAll() })); } + public function testWhenEndsWith() + { + $this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith('ark', function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + + $this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith(['kra', 'ark'], function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + + $this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenEndsWith(['xxx'], function ($stringable) { + return $stringable->title(); + })); + + $this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenEndsWith(['tony', 'xxx'], function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + } + + public function testWhenExactly() + { + $this->assertSame('Nailed it...!', (string) $this->stringable('Tony Stark')->whenExactly('Tony Stark', function ($stringable) { + return 'Nailed it...!'; + }, function ($stringable) { + return 'Swing and a miss...!'; + })); + + $this->assertSame('Swing and a miss...!', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) { + return 'Nailed it...!'; + }, function ($stringable) { + return 'Swing and a miss...!'; + })); + + $this->assertSame('Tony Stark', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) { + return 'Nailed it...!'; + })); + } + + public function testWhenIs() + { + $this->assertSame('Winner: /', (string) $this->stringable('/')->whenIs('/', function ($stringable) { + return $stringable->prepend('Winner: '); + }, function ($stringable) { + return 'Try again'; + })); + + $this->assertSame('/', (string) $this->stringable('/')->whenIs(' /', function ($stringable) { + return $stringable->prepend('Winner: '); + })); + + $this->assertSame('Try again', (string) $this->stringable('/')->whenIs(' /', function ($stringable) { + return $stringable->prepend('Winner: '); + }, function ($stringable) { + return 'Try again'; + })); + + $this->assertSame('Winner: foo/bar/baz', (string) $this->stringable('foo/bar/baz')->whenIs('foo/*', function ($stringable) { + return $stringable->prepend('Winner: '); + })); + } + + public function testWhenIsAscii() + { + $this->assertSame('Ascii: A', (string) $this->stringable('A')->whenIsAscii(function ($stringable) { + return $stringable->prepend('Ascii: '); + }, function ($stringable) { + return $stringable->prepend('Not Ascii: '); + })); + + $this->assertSame('ù', (string) $this->stringable('ù')->whenIsAscii(function ($stringable) { + return $stringable->prepend('Ascii: '); + })); + + $this->assertSame('Not Ascii: ù', (string) $this->stringable('ù')->whenIsAscii(function ($stringable) { + return $stringable->prepend('Ascii: '); + }, function ($stringable) { + return $stringable->prepend('Not Ascii: '); + })); + } + + public function testWhenIsUuid() + { + $this->assertSame('Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98e7b15', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98e7b15')->whenIsUuid(function ($stringable) { + return $stringable->prepend('Uuid: '); + }, function ($stringable) { + return $stringable->prepend('Not Uuid: '); + })); + + $this->assertSame('2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) { + return $stringable->prepend('Uuid: '); + })); + + $this->assertSame('Not Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) { + return $stringable->prepend('Uuid: '); + }, function ($stringable) { + return $stringable->prepend('Not Uuid: '); + })); + } + + public function testWhenTest() + { + $this->assertSame('Winner: foo bar', (string) $this->stringable('foo bar')->whenTest('/bar/', function ($stringable) { + return $stringable->prepend('Winner: '); + }, function ($stringable) { + return 'Try again'; + })); + + $this->assertSame('Try again', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) { + return $stringable->prepend('Winner: '); + }, function ($stringable) { + return 'Try again'; + })); + + $this->assertSame('foo bar', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) { + return $stringable->prepend('Winner: '); + })); + } + + public function testWhenStartsWith() + { + $this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith('ton', function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + + $this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith(['ton', 'not'], function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + + $this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenStartsWith(['xxx'], function ($stringable) { + return $stringable->title(); + })); + + $this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith(['tony', 'xxx'], function ($stringable) { + return $stringable->title(); + }, function ($stringable) { + return $stringable->studly(); + })); + } + public function testWhenEmpty() { tap($this->stringable(), function ($stringable) { From 5871d64ff2a133afe404247b1c2f0c7682dbaf15 Mon Sep 17 00:00:00 2001 From: Travis Elkins Date: Mon, 10 Jan 2022 10:47:59 +0100 Subject: [PATCH 3/4] Reused when() to DRY things up a bit. --- src/Illuminate/Support/Stringable.php | 84 +++------------------------ 1 file changed, 7 insertions(+), 77 deletions(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 9fe53cbc0b79..d99cba2b6ae9 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -821,17 +821,7 @@ public function whenContainsAll(array $needles, $callback, $default = null) */ public function whenEndsWith($needles, $callback, $default = null) { - if ($this->endsWith($needles)) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->endsWith($needles), $callback, $default); } /** @@ -844,17 +834,7 @@ public function whenEndsWith($needles, $callback, $default = null) */ public function whenExactly($value, $callback, $default = null) { - if ($this->exactly($value)) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->exactly($value), $callback, $default); } /** @@ -867,17 +847,7 @@ public function whenExactly($value, $callback, $default = null) */ public function whenIs($pattern, $callback, $default = null) { - if ($this->is($pattern)) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->is($pattern), $callback, $default); } /** @@ -889,17 +859,7 @@ public function whenIs($pattern, $callback, $default = null) */ public function whenIsAscii($callback, $default = null) { - if ($this->isAscii()) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->isAscii(), $callback, $default); } /** @@ -911,17 +871,7 @@ public function whenIsAscii($callback, $default = null) */ public function whenIsUuid($callback, $default = null) { - if ($this->isUuid()) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->isUuid(), $callback, $default); } /** @@ -934,17 +884,7 @@ public function whenIsUuid($callback, $default = null) */ public function whenTest($pattern, $callback, $default = null) { - if ($this->test($pattern)) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->test($pattern), $callback, $default); } /** @@ -957,17 +897,7 @@ public function whenTest($pattern, $callback, $default = null) */ public function whenStartsWith($needles, $callback, $default = null) { - if ($this->startsWith($needles)) { - $result = $callback($this); - - return $result ?? $this; - } elseif ($default) { - $result = $default($this); - - return $result ?? $this; - } - - return $this; + return $this->when($this->startsWith($needles), $callback, $default); } /** From de04718f679706e2b512018255a3c44ef3c731f7 Mon Sep 17 00:00:00 2001 From: Travis Elkins Date: Mon, 10 Jan 2022 10:49:55 +0100 Subject: [PATCH 4/4] Applied same re-use of when() to whenContains() and whenContainsAll(). --- src/Illuminate/Support/Stringable.php | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index d99cba2b6ae9..198a5a69629e 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -775,17 +775,7 @@ public function ucfirst() */ 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; + return $this->when($this->contains($needles), $callback, $default); } /** @@ -798,17 +788,7 @@ public function whenContains($needles, $callback, $default = null) */ 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; + return $this->when($this->containsAll($needles), $callback, $default); } /**