Skip to content

Commit 55ea49b

Browse files
committed
Merge branch 'telkins/8.x' into 8.x
2 parents d320012 + 1689ba3 commit 55ea49b

File tree

2 files changed

+240
-22
lines changed

2 files changed

+240
-22
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -775,17 +775,7 @@ public function ucfirst()
775775
*/
776776
public function whenContains($needles, $callback, $default = null)
777777
{
778-
if ($this->contains($needles)) {
779-
$result = $callback($this);
780-
781-
return $result ?? $this;
782-
} elseif ($default) {
783-
$result = $default($this);
784-
785-
return $result ?? $this;
786-
}
787-
788-
return $this;
778+
return $this->when($this->contains($needles), $callback, $default);
789779
}
790780

791781
/**
@@ -798,17 +788,7 @@ public function whenContains($needles, $callback, $default = null)
798788
*/
799789
public function whenContainsAll(array $needles, $callback, $default = null)
800790
{
801-
if ($this->containsAll($needles)) {
802-
$result = $callback($this);
803-
804-
return $result ?? $this;
805-
} elseif ($default) {
806-
$result = $default($this);
807-
808-
return $result ?? $this;
809-
}
810-
811-
return $this;
791+
return $this->when($this->containsAll($needles), $callback, $default);
812792
}
813793

814794
/**
@@ -845,6 +825,95 @@ public function whenNotEmpty($callback)
845825
return $this;
846826
}
847827

828+
/**
829+
* Execute the given callback if the string ends with a given substring.
830+
*
831+
* @param string|array $needles
832+
* @param callable $callback
833+
* @param callable|null $default
834+
* @return static
835+
*/
836+
public function whenEndsWith($needles, $callback, $default = null)
837+
{
838+
return $this->when($this->endsWith($needles), $callback, $default);
839+
}
840+
841+
/**
842+
* Execute the given callback if the string is an exact match with the given value.
843+
*
844+
* @param string $value
845+
* @param callable $callback
846+
* @param callable|null $default
847+
* @return static
848+
*/
849+
public function whenExactly($value, $callback, $default = null)
850+
{
851+
return $this->when($this->exactly($value), $callback, $default);
852+
}
853+
854+
/**
855+
* Execute the given callback if the string matches a given pattern.
856+
*
857+
* @param string|array $pattern
858+
* @param callable $callback
859+
* @param callable|null $default
860+
* @return static
861+
*/
862+
public function whenIs($pattern, $callback, $default = null)
863+
{
864+
return $this->when($this->is($pattern), $callback, $default);
865+
}
866+
867+
/**
868+
* Execute the given callback if the string is 7 bit ASCII.
869+
*
870+
* @param callable $callback
871+
* @param callable|null $default
872+
* @return static
873+
*/
874+
public function whenIsAscii($callback, $default = null)
875+
{
876+
return $this->when($this->isAscii(), $callback, $default);
877+
}
878+
879+
/**
880+
* Execute the given callback if the string is a valid UUID.
881+
*
882+
* @param callable $callback
883+
* @param callable|null $default
884+
* @return static
885+
*/
886+
public function whenIsUuid($callback, $default = null)
887+
{
888+
return $this->when($this->isUuid(), $callback, $default);
889+
}
890+
891+
/**
892+
* Execute the given callback if the string starts with a given substring.
893+
*
894+
* @param string|array $needles
895+
* @param callable $callback
896+
* @param callable|null $default
897+
* @return static
898+
*/
899+
public function whenStartsWith($needles, $callback, $default = null)
900+
{
901+
return $this->when($this->startsWith($needles), $callback, $default);
902+
}
903+
904+
/**
905+
* Execute the given callback if the string matches the given pattern.
906+
*
907+
* @param string $pattern
908+
* @param callable $callback
909+
* @param callable|null $default
910+
* @return static
911+
*/
912+
public function whenTest($pattern, $callback, $default = null)
913+
{
914+
return $this->when($this->test($pattern), $callback, $default);
915+
}
916+
848917
/**
849918
* Limit the number of words in a string.
850919
*

tests/Support/SupportStringableTest.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,155 @@ public function testWhenContainsAll()
150150
}));
151151
}
152152

153+
public function testWhenEndsWith()
154+
{
155+
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith('ark', function ($stringable) {
156+
return $stringable->title();
157+
}, function ($stringable) {
158+
return $stringable->studly();
159+
}));
160+
161+
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith(['kra', 'ark'], function ($stringable) {
162+
return $stringable->title();
163+
}, function ($stringable) {
164+
return $stringable->studly();
165+
}));
166+
167+
$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenEndsWith(['xxx'], function ($stringable) {
168+
return $stringable->title();
169+
}));
170+
171+
$this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenEndsWith(['tony', 'xxx'], function ($stringable) {
172+
return $stringable->title();
173+
}, function ($stringable) {
174+
return $stringable->studly();
175+
}));
176+
}
177+
178+
public function testWhenExactly()
179+
{
180+
$this->assertSame('Nailed it...!', (string) $this->stringable('Tony Stark')->whenExactly('Tony Stark', function ($stringable) {
181+
return 'Nailed it...!';
182+
}, function ($stringable) {
183+
return 'Swing and a miss...!';
184+
}));
185+
186+
$this->assertSame('Swing and a miss...!', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) {
187+
return 'Nailed it...!';
188+
}, function ($stringable) {
189+
return 'Swing and a miss...!';
190+
}));
191+
192+
$this->assertSame('Tony Stark', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) {
193+
return 'Nailed it...!';
194+
}));
195+
}
196+
197+
public function testWhenIs()
198+
{
199+
$this->assertSame('Winner: /', (string) $this->stringable('/')->whenIs('/', function ($stringable) {
200+
return $stringable->prepend('Winner: ');
201+
}, function ($stringable) {
202+
return 'Try again';
203+
}));
204+
205+
$this->assertSame('/', (string) $this->stringable('/')->whenIs(' /', function ($stringable) {
206+
return $stringable->prepend('Winner: ');
207+
}));
208+
209+
$this->assertSame('Try again', (string) $this->stringable('/')->whenIs(' /', function ($stringable) {
210+
return $stringable->prepend('Winner: ');
211+
}, function ($stringable) {
212+
return 'Try again';
213+
}));
214+
215+
$this->assertSame('Winner: foo/bar/baz', (string) $this->stringable('foo/bar/baz')->whenIs('foo/*', function ($stringable) {
216+
return $stringable->prepend('Winner: ');
217+
}));
218+
}
219+
220+
public function testWhenIsAscii()
221+
{
222+
$this->assertSame('Ascii: A', (string) $this->stringable('A')->whenIsAscii(function ($stringable) {
223+
return $stringable->prepend('Ascii: ');
224+
}, function ($stringable) {
225+
return $stringable->prepend('Not Ascii: ');
226+
}));
227+
228+
$this->assertSame('ù', (string) $this->stringable('ù')->whenIsAscii(function ($stringable) {
229+
return $stringable->prepend('Ascii: ');
230+
}));
231+
232+
$this->assertSame('Not Ascii: ù', (string) $this->stringable('ù')->whenIsAscii(function ($stringable) {
233+
return $stringable->prepend('Ascii: ');
234+
}, function ($stringable) {
235+
return $stringable->prepend('Not Ascii: ');
236+
}));
237+
}
238+
239+
public function testWhenIsUuid()
240+
{
241+
$this->assertSame('Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98e7b15', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98e7b15')->whenIsUuid(function ($stringable) {
242+
return $stringable->prepend('Uuid: ');
243+
}, function ($stringable) {
244+
return $stringable->prepend('Not Uuid: ');
245+
}));
246+
247+
$this->assertSame('2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) {
248+
return $stringable->prepend('Uuid: ');
249+
}));
250+
251+
$this->assertSame('Not Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) {
252+
return $stringable->prepend('Uuid: ');
253+
}, function ($stringable) {
254+
return $stringable->prepend('Not Uuid: ');
255+
}));
256+
}
257+
258+
public function testWhenTest()
259+
{
260+
$this->assertSame('Winner: foo bar', (string) $this->stringable('foo bar')->whenTest('/bar/', function ($stringable) {
261+
return $stringable->prepend('Winner: ');
262+
}, function ($stringable) {
263+
return 'Try again';
264+
}));
265+
266+
$this->assertSame('Try again', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) {
267+
return $stringable->prepend('Winner: ');
268+
}, function ($stringable) {
269+
return 'Try again';
270+
}));
271+
272+
$this->assertSame('foo bar', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) {
273+
return $stringable->prepend('Winner: ');
274+
}));
275+
}
276+
277+
public function testWhenStartsWith()
278+
{
279+
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith('ton', function ($stringable) {
280+
return $stringable->title();
281+
}, function ($stringable) {
282+
return $stringable->studly();
283+
}));
284+
285+
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith(['ton', 'not'], function ($stringable) {
286+
return $stringable->title();
287+
}, function ($stringable) {
288+
return $stringable->studly();
289+
}));
290+
291+
$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenStartsWith(['xxx'], function ($stringable) {
292+
return $stringable->title();
293+
}));
294+
295+
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith(['tony', 'xxx'], function ($stringable) {
296+
return $stringable->title();
297+
}, function ($stringable) {
298+
return $stringable->studly();
299+
}));
300+
}
301+
153302
public function testWhenEmpty()
154303
{
155304
tap($this->stringable(), function ($stringable) {

0 commit comments

Comments
 (0)