Skip to content

Commit a69479a

Browse files
authored
[8.x] Add Stringable::whenNotEmpty() (#37080)
1 parent 597f5ab commit a69479a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,23 @@ public function whenEmpty($callback)
744744
return $this;
745745
}
746746

747+
/**
748+
* Execute the given callback if the string is not empty.
749+
*
750+
* @param callable $callback
751+
* @return static
752+
*/
753+
public function whenNotEmpty($callback)
754+
{
755+
if ($this->isNotEmpty()) {
756+
$result = $callback($this);
757+
758+
return is_null($result) ? $this : $result;
759+
}
760+
761+
return $this;
762+
}
763+
747764
/**
748765
* Limit the number of words in a string.
749766
*

0 commit comments

Comments
 (0)