Skip to content

Commit 5871d64

Browse files
committed
Reused when() to DRY things up a bit.
1 parent 3e127ed commit 5871d64

File tree

1 file changed

+7
-77
lines changed

1 file changed

+7
-77
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -821,17 +821,7 @@ public function whenContainsAll(array $needles, $callback, $default = null)
821821
*/
822822
public function whenEndsWith($needles, $callback, $default = null)
823823
{
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;
824+
return $this->when($this->endsWith($needles), $callback, $default);
835825
}
836826

837827
/**
@@ -844,17 +834,7 @@ public function whenEndsWith($needles, $callback, $default = null)
844834
*/
845835
public function whenExactly($value, $callback, $default = null)
846836
{
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;
837+
return $this->when($this->exactly($value), $callback, $default);
858838
}
859839

860840
/**
@@ -867,17 +847,7 @@ public function whenExactly($value, $callback, $default = null)
867847
*/
868848
public function whenIs($pattern, $callback, $default = null)
869849
{
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;
850+
return $this->when($this->is($pattern), $callback, $default);
881851
}
882852

883853
/**
@@ -889,17 +859,7 @@ public function whenIs($pattern, $callback, $default = null)
889859
*/
890860
public function whenIsAscii($callback, $default = null)
891861
{
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;
862+
return $this->when($this->isAscii(), $callback, $default);
903863
}
904864

905865
/**
@@ -911,17 +871,7 @@ public function whenIsAscii($callback, $default = null)
911871
*/
912872
public function whenIsUuid($callback, $default = null)
913873
{
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;
874+
return $this->when($this->isUuid(), $callback, $default);
925875
}
926876

927877
/**
@@ -934,17 +884,7 @@ public function whenIsUuid($callback, $default = null)
934884
*/
935885
public function whenTest($pattern, $callback, $default = null)
936886
{
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;
887+
return $this->when($this->test($pattern), $callback, $default);
948888
}
949889

950890
/**
@@ -957,17 +897,7 @@ public function whenTest($pattern, $callback, $default = null)
957897
*/
958898
public function whenStartsWith($needles, $callback, $default = null)
959899
{
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;
900+
return $this->when($this->startsWith($needles), $callback, $default);
971901
}
972902

973903
/**

0 commit comments

Comments
 (0)