File tree Expand file tree Collapse file tree 3 files changed +100
-3
lines changed Expand file tree Collapse file tree 3 files changed +100
-3
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ With code like this:
7474$names = array_map(Transform::callMethod('getName'), $allUsers);
7575```
7676
77- ### Transform::callMethod
77+ ### Transform::callMethod($methodName)
7878
7979``` php
8080Transform::classMethod('getName');
@@ -86,8 +86,7 @@ function ($object) {
8686}
8787```
8888
89-
90- ### Transform::getEntry
89+ ### Transform::getEntry($name)
9190
9291``` php
9392Transform::getEntry('name');
@@ -108,3 +107,22 @@ function ($array) {
108107 return $array['user']['name'];
109108}
110109```
110+
111+ ### Transform::argumentTo($callable)
112+
113+ ``` php
114+ Transform::getEntry('strtolower');
115+
116+ // Generates:
117+
118+ function ($value) {
119+ return strtolower($value);
120+ }
121+ ```
122+
123+ ` $callable ` can be any of the following:
124+
125+ * ` 'functionName' `
126+ * ` function ($value) { /* ... */ } `
127+ * ` [$object, 'methodName'] `
128+ * ` ['ClassName', 'staticMethodName'] `
Original file line number Diff line number Diff line change @@ -44,4 +44,20 @@ public static function getEntry($name)
4444 return $ array ;
4545 };
4646 }
47+
48+ /**
49+ * Returns a transformer calls the given callable with its value as the
50+ * argument and returns the result.
51+ *
52+ * @param string|string[] $name Providing an array will walk multiple levels
53+ * into the array.
54+ *
55+ * @return callable
56+ */
57+ public static function argumentTo ($ callable )
58+ {
59+ return function ($ value ) use ($ callable ) {
60+ return $ callable ($ value );
61+ };
62+ }
4763}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace tests \TomPHP \Predicate ;
4+
5+ use PHPUnit_Framework_TestCase ;
6+ use TomPHP \Predicate \Transform ;
7+
8+ final class ArgumentToTest extends PHPUnit_Framework_TestCase
9+ {
10+ /** @test */
11+ public function it_calls_a_function_by_name_with_the_value_as_an_argument_and_returns_the_result ()
12+ {
13+ $ fn = Transform::argumentTo ('strtolower ' );
14+
15+ $ this ->assertSame ('tom ' , $ fn ('Tom ' ));
16+ }
17+
18+ /** @test */
19+ public function it_calls_a_function_with_the_value_as_an_argument_and_returns_the_result ()
20+ {
21+ $ fn = Transform::argumentTo (function ($ value ) {
22+ return $ value + 1 ;
23+ });
24+
25+ $ this ->assertSame (5 , $ fn (4 ));
26+ }
27+
28+ /** @test */
29+ public function it_calls_an_object_method_with_the_value_as_an_argument_and_returns_the_result ()
30+ {
31+ $ fn = Transform::argumentTo ([$ this , 'decrement ' ]);
32+
33+ $ this ->assertSame (3 , $ fn (4 ));
34+ }
35+
36+ /** @test */
37+ public function it_calls_a_static_method_with_the_value_as_an_argument_and_returns_the_result ()
38+ {
39+ $ fn = Transform::argumentTo ([__CLASS__ , 'staticDecrement ' ]);
40+
41+ $ this ->assertSame (3 , $ fn (4 ));
42+ }
43+
44+ /**
45+ * @param int $value
46+ *
47+ * @return int
48+ */
49+ public function decrement ($ value )
50+ {
51+ return $ value - 1 ;
52+ }
53+
54+ /**
55+ * @param int $value
56+ *
57+ * @return int
58+ */
59+ public static function staticDecrement ($ value )
60+ {
61+ return $ value - 1 ;
62+ }
63+ }
You can’t perform that action at this time.
0 commit comments