Skip to content

Commit a2db64b

Browse files
committed
Add parameter to argumentTo()
1 parent 0b306f2 commit a2db64b

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 0.1.6 (2016-01-14)
4+
5+
* Added: Argument list for `argumentTo($callable, $arguments)`
6+
37
### 0.1.5 (2016-01-14)
48

59
* Added: `Builder` for prettier object/array traversal.

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function ($value) {
172172

173173
### Generic Transformations
174174

175-
#### T\argumentTo(callable $callable)
175+
#### T\argumentTo(callable $callable, array $argments = [__])
176176

177177
```php
178178
T\argumentTo('strtolower');
@@ -184,9 +184,17 @@ function ($value) {
184184
}
185185
```
186186

187-
`$callable` can be any of the following:
187+
You can also provide a list of arguments using `__` as the placeholder for where
188+
you want the value inserted:
188189

189-
* `'functionName'`
190-
* `function ($value) { /* ... */ }`
191-
* `[$object, 'methodName']`
192-
* `['ClassName', 'staticMethodName']`
190+
```php
191+
use const TomPHP\Transform\__;
192+
193+
T\argumentTo('strpos', ['Tom: My name is Tom', __, 4]);
194+
195+
// Is equivalent to:
196+
197+
function ($value) {
198+
return strpos('Tom: My name is Tom', $value, 4);
199+
}
200+
```

src/transform.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use TomPHP\Transform\Exception\InvalidArgumentException;
66

7+
/**
8+
* Represents a placeholder in an argument list.
9+
*/
10+
const __ = 'Super Unique Placeholder String - 6f74f07a-bc60-494a-93e0-eefedb69849b';
11+
712
/**
813
* Chains a number of arity 1 functions together, passing the output of
914
* each to the next.
@@ -113,15 +118,23 @@ function getProperty($name)
113118
* Returns a transformer calls the given callable with its value as the
114119
* argument and returns the result.
115120
*
116-
* @param string|string[] $name Providing an array will walk multiple levels
117-
* into the array.
121+
* @param callable $name Providing an array will walk multiple levels into
122+
* the array.
123+
* @param mixed[] $arguments Use __ to indicate where the value should be
124+
* placed in the argument list.
118125
*
119126
* @return \Closure
120127
*/
121-
function argumentTo($callable)
128+
function argumentTo(callable $callable, array $arguments = [__])
122129
{
123-
return function ($value) use ($callable) {
124-
return $callable($value);
130+
return function ($value) use ($callable, $arguments) {
131+
$arguments = array_map(
132+
function ($arg) use ($value) {
133+
return $arg === __ ? $value : $arg;
134+
},
135+
$arguments
136+
);
137+
return $callable(...$arguments);
125138
};
126139
}
127140

tests/ArgumentToTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit_Framework_TestCase;
66
use TomPHP\Transform as T;
7+
use const TomPHP\Transform\__;
78

89
final class ArgumentToTest extends PHPUnit_Framework_TestCase
910
{
@@ -41,6 +42,14 @@ public function it_calls_a_static_method_with_the_value_as_an_argument_and_retur
4142
$this->assertSame(3, $fn(4));
4243
}
4344

45+
/** @test */
46+
public function it_calls_a_function_with_a_positional_argument()
47+
{
48+
$fn = T\argumentTo('strpos', ['Tom: My name is Tom', __, 4]);
49+
50+
$this->assertSame(16, $fn('Tom'));
51+
}
52+
4453
/**
4554
* @param int $value
4655
*

0 commit comments

Comments
 (0)