Skip to content

Commit 3e127ed

Browse files
committed
Added whenEndsWith(), whenExactly(), whenStartsWith(), etc to Stringable.
1 parent a990b19 commit 3e127ed

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,165 @@ public function whenContainsAll(array $needles, $callback, $default = null)
811811
return $this;
812812
}
813813

814+
/**
815+
* Execute the given callback if the string ends with a given substring.
816+
*
817+
* @param string|array $needles
818+
* @param callable $callback
819+
* @param callable|null $default
820+
* @return static
821+
*/
822+
public function whenEndsWith($needles, $callback, $default = null)
823+
{
824+
if ($this->endsWith($needles)) {
825+
$result = $callback($this);
826+
827+
return $result ?? $this;
828+
} elseif ($default) {
829+
$result = $default($this);
830+
831+
return $result ?? $this;
832+
}
833+
834+
return $this;
835+
}
836+
837+
/**
838+
* Execute the given callback if the string is an exact match with the given value.
839+
*
840+
* @param string $value
841+
* @param callable $callback
842+
* @param callable|null $default
843+
* @return static
844+
*/
845+
public function whenExactly($value, $callback, $default = null)
846+
{
847+
if ($this->exactly($value)) {
848+
$result = $callback($this);
849+
850+
return $result ?? $this;
851+
} elseif ($default) {
852+
$result = $default($this);
853+
854+
return $result ?? $this;
855+
}
856+
857+
return $this;
858+
}
859+
860+
/**
861+
* Execute the given callback if the string matches a given pattern.
862+
*
863+
* @param string|array $pattern
864+
* @param callable $callback
865+
* @param callable|null $default
866+
* @return static
867+
*/
868+
public function whenIs($pattern, $callback, $default = null)
869+
{
870+
if ($this->is($pattern)) {
871+
$result = $callback($this);
872+
873+
return $result ?? $this;
874+
} elseif ($default) {
875+
$result = $default($this);
876+
877+
return $result ?? $this;
878+
}
879+
880+
return $this;
881+
}
882+
883+
/**
884+
* Execute the given callback if the string is 7 bit ASCII.
885+
*
886+
* @param callable $callback
887+
* @param callable|null $default
888+
* @return static
889+
*/
890+
public function whenIsAscii($callback, $default = null)
891+
{
892+
if ($this->isAscii()) {
893+
$result = $callback($this);
894+
895+
return $result ?? $this;
896+
} elseif ($default) {
897+
$result = $default($this);
898+
899+
return $result ?? $this;
900+
}
901+
902+
return $this;
903+
}
904+
905+
/**
906+
* Execute the given callback if the string is a valid UUID.
907+
*
908+
* @param callable $callback
909+
* @param callable|null $default
910+
* @return static
911+
*/
912+
public function whenIsUuid($callback, $default = null)
913+
{
914+
if ($this->isUuid()) {
915+
$result = $callback($this);
916+
917+
return $result ?? $this;
918+
} elseif ($default) {
919+
$result = $default($this);
920+
921+
return $result ?? $this;
922+
}
923+
924+
return $this;
925+
}
926+
927+
/**
928+
* Execute the given callback if the string matches the given pattern.
929+
*
930+
* @param string $pattern
931+
* @param callable $callback
932+
* @param callable|null $default
933+
* @return static
934+
*/
935+
public function whenTest($pattern, $callback, $default = null)
936+
{
937+
if ($this->test($pattern)) {
938+
$result = $callback($this);
939+
940+
return $result ?? $this;
941+
} elseif ($default) {
942+
$result = $default($this);
943+
944+
return $result ?? $this;
945+
}
946+
947+
return $this;
948+
}
949+
950+
/**
951+
* Execute the given callback if the string starts with a given substring.
952+
*
953+
* @param string|array $needles
954+
* @param callable $callback
955+
* @param callable|null $default
956+
* @return static
957+
*/
958+
public function whenStartsWith($needles, $callback, $default = null)
959+
{
960+
if ($this->startsWith($needles)) {
961+
$result = $callback($this);
962+
963+
return $result ?? $this;
964+
} elseif ($default) {
965+
$result = $default($this);
966+
967+
return $result ?? $this;
968+
}
969+
970+
return $this;
971+
}
972+
814973
/**
815974
* Execute the given callback if the string is empty.
816975
*

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)