File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed
Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -70,6 +70,16 @@ public function isPresent(): bool
7070 return !$ this ->isEmpty ();
7171 }
7272
73+ /**
74+ * @param callable(T): void $consumer
75+ */
76+ public function ifPresent (callable $ consumer ): void
77+ {
78+ if ($ this ->isPresent ()) {
79+ $ consumer ($ this ->get ());
80+ }
81+ }
82+
7383 /**
7484 * @template U
7585 *
Original file line number Diff line number Diff line change @@ -182,4 +182,10 @@ public function testOptionIsPresent(): void
182182 self ::assertTrue (Option::some (null )->isPresent ());
183183 self ::assertFalse (Option::none ()->isPresent ());
184184 }
185+
186+ public function testOptionIfPresent (): void
187+ {
188+ Option::none ()->ifPresent (fn ($ v ) => throw new \RuntimeException ('impossible is nothing ' ));
189+ Option::of ('a ' )->ifPresent (fn ($ v ) => self ::assertSame ('a ' , $ v ));
190+ }
185191}
You can’t perform that action at this time.
0 commit comments