File tree Expand file tree Collapse file tree 3 files changed +25
-1
lines changed
Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -96,7 +96,7 @@ function isValidEmail(mixed $email): bool
9696 && filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
9797}
9898
99- $validEmail = Box::of($input)->assert (isValidEmail(...))->unbox( );
99+ $validEmail = Box::of($input)->assertGet (isValidEmail(...));
100100```
101101
102102# Type Safety
Original file line number Diff line number Diff line change @@ -100,6 +100,20 @@ public function assert(mixed $check): Box
100100 return $ this ;
101101 }
102102
103+ /**
104+ * Run an assertion against the value and return it.
105+ *
106+ * @template U
107+ * @param U|callable(T):bool $check
108+ * @return T
109+ */
110+ public function assertGet (mixed $ check ): mixed
111+ {
112+ $ this ->assert ($ check );
113+
114+ return $ this ->unbox ();
115+ }
116+
103117 /**
104118 * Dump the value to the console.
105119 *
Original file line number Diff line number Diff line change 6060 expect ($ result ->number )->toBe (5 );
6161});
6262
63+ test ('assertGet returns the value when the assertion passes ' , function () {
64+ $ result = Box::of (5 )->assertGet (5 );
65+
66+ expect ($ result )->toBe (5 );
67+ });
68+
69+ test ('assertGet throws an exception when the assertion fails ' , function () {
70+ expect (fn () => Box::of (5 )->assertGet (6 ))->toThrow (LogicException::class);
71+ });
72+
6373// This test is not here to "lock in" desired behavior. It is here to document a pitfall.
6474test ('performing a mutation on an object using map() will produce side effects ' , function () {
6575 $ object = (object )['number ' => 5 ];
You can’t perform that action at this time.
0 commit comments