@@ -21,36 +21,43 @@ the [CONTRIBUTING.md](CONTRIBUTING.md) file.
2121# Usage
2222You put any value into a box.
2323
24- Then you can chain pipe (), dump() and assert() calls on it.
24+ Then you can chain map (), dump() and assert() calls on it.
2525
26- To get the value out of the box, call unbox() or pull().
26+ To get the value out of the box, call unbox() or get().
27+
28+ ## Note
29+ In an earlier version:
30+ - map() was called pipe()
31+ - get() was called pull()
2732
2833## Examples
2934
3035``` php
3136$value = Box::of(5)
32- ->pipe (fn($value) => $value + 1)
33- ->pipe (fn($value) => $value * 2)
37+ ->map (fn($value) => $value + 1)
38+ ->map (fn($value) => $value * 2)
3439 ->unbox();
3540
3641echo $value; // 12
3742```
3843
44+ Use get() to combine map() and unbox() in one call:
3945``` php
4046$value = Box::of(5)
41- ->pipe (fn($value) => $value + 1)
42- ->pull (fn($value) => $value * 2);
47+ ->map (fn($value) => $value + 1)
48+ ->get (fn($value) => $value * 2);
4349
4450echo $value; // 12
4551```
4652
53+ Perform assertions or dump the value inline:
4754``` php
55+ $isEven = fn($value) => $value % 2 === 0;
56+
4857$value = Box::of(5)
49- ->pipe(fn($it) => $it + 1)
50- ->assert(6) // throws a LogicException if the value is not 6 (strict equality)
51- ->assert(fn($it) => $it % 2 === 0) // throws a LogicException if the callback returns false
52- ->dump() // prints the value
53- ->pull(fn($it) => $it * 2); // doubles the value and unboxes it
58+ ->map(fn($it) => $it + 1)->assert(6)
59+ ->map(fn($it) => $it * 2)->assert($isEven)->dump()
60+ ->unbox();
5461
5562echo $value; // 12
5663```
@@ -61,7 +68,7 @@ $user = Box::of($inputEmail)
6168 ->assert(is_string(...))
6269 ->assert(fn($it) => strlen($it) > 0 && strlen($it) < 256)
6370 ->assert(fn($it) => filter_var($it, FILTER_VALIDATE_EMAIL) !== false)
64- ->pull (fn($it) => $userRepository->create(['email' => $it]));
71+ ->get (fn($it) => $userRepository->create(['email' => $it]));
6572```
6673
6774# Type Safety
@@ -102,8 +109,8 @@ import scala.util.chaining.*
102109 .toCharArray
103110 .map(_.toInt)
104111 .sum
105- .pipe (n => s " The number is $n" )
106- .pipe (println) // prints "The number is 1052"
112+ .map (n => s " The number is $n" )
113+ .map (println) // prints "The number is 1052"
107114```
108115/ad-break
109116
@@ -116,7 +123,7 @@ If you do not understand what these pointy brackets mean, you should read up on
116123Every language on the planet except PHP has them. Thanks to PHPStan, we can have them too.
117124Just with lots more (keyboard) typing.
118125
119- Have a look at the pipe () method
126+ Have a look at the map () method
120127``` php
121128/**
122129 * Apply a transformation function to the value.
@@ -125,7 +132,7 @@ Have a look at the pipe() method
125132 * @param callable(T): U $callback
126133 * @return Box<U >
127134 */
128- public function pipe (callable $callback): Box
135+ public function map (callable $callback): Box
129136{
130137 return new self($callback($this->value));
131138}
@@ -140,10 +147,10 @@ Please pick one of these and learn it.
140147
141148``` php
142149$value = Box::of('Hello World') // string
143- ->pipe (fn($value) => strtoupper($value)) // string
144- ->pipe (fn($value) => str_replace('WORLD', 'PHP', $value)) // string
145- ->pipe (fn($value) => str_split($value)) // array<string >
146- ->pull (fn($value) => $value - 1); // phpstan: Binary operation "-" between array<int , string > and 1 results in an error.
150+ ->map (fn($value) => strtoupper($value)) // string
151+ ->map (fn($value) => str_replace('WORLD', 'PHP', $value)) // string
152+ ->map (fn($value) => str_split($value)) // array<string >
153+ ->get (fn($value) => $value - 1); // phpstan: Binary operation "-" between array<int , string > and 1 results in an error.
147154```
148155In the last transformation, I try to subtract 1 from an array of strings. An unsound operation to say the least.
149156PHPStan will catch this guaranteed error and tell you about it. Without writing any tests.
0 commit comments