Skip to content

Commit 56fa7ec

Browse files
committed
add more examples
1 parent 55dd151 commit 56fa7ec

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

src/Box.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*

tests/Unit/BoxTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
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.
6474
test('performing a mutation on an object using map() will produce side effects', function () {
6575
$object = (object)['number' => 5];

0 commit comments

Comments
 (0)