Skip to content

Commit f34ffda

Browse files
committed
Added whenContains and whenContainsAll to Stringable.
1 parent 26bfb14 commit f34ffda

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,52 @@ public function ucfirst()
765765
return new static(Str::ucfirst($this->value));
766766
}
767767

768+
/**
769+
* Execute the given callback if the string contains a given substring.
770+
*
771+
* @param string|array $needles
772+
* @param callable $callback
773+
* @param callable|null $default
774+
* @return static
775+
*/
776+
public function whenContains($needles, $callback, $default = null)
777+
{
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;
789+
}
790+
791+
/**
792+
* Execute the given callback if the string contains all array values.
793+
*
794+
* @param array $needles
795+
* @param callable $callback
796+
* @param callable|null $default
797+
* @return static
798+
*/
799+
public function whenContainsAll(array $needles, $callback, $default = null)
800+
{
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;
812+
}
813+
768814
/**
769815
* Execute the given callback if the string is empty.
770816
*

tests/Support/SupportStringableTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,44 @@ public function testUnless()
112112
}));
113113
}
114114

115+
public function testWhenContains()
116+
{
117+
$this->assertSame('Tony Stark', (string) $this->stringable('stark')->whenContains('tar', function ($stringable) {
118+
return $stringable->prepend('Tony ')->title();
119+
}, function ($stringable) {
120+
return $stringable->prepend('Arno ')->title();
121+
}));
122+
123+
$this->assertSame('stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) {
124+
return $stringable->prepend('Tony ')->title();
125+
}));
126+
127+
$this->assertSame('Arno Stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) {
128+
return $stringable->prepend('Tony ')->title();
129+
}, function ($stringable) {
130+
return $stringable->prepend('Arno ')->title();
131+
}));
132+
}
133+
134+
public function testWhenContainsAll()
135+
{
136+
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'stark'], function ($stringable) {
137+
return $stringable->title();
138+
}, function ($stringable) {
139+
return $stringable->studly();
140+
}));
141+
142+
$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenContainsAll(['xxx'], function ($stringable) {
143+
return $stringable->title();
144+
}));
145+
146+
$this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'xxx'], function ($stringable) {
147+
return $stringable->title();
148+
}, function ($stringable) {
149+
return $stringable->studly();
150+
}));
151+
}
152+
115153
public function testWhenEmpty()
116154
{
117155
tap($this->stringable(), function ($stringable) {

0 commit comments

Comments
 (0)