Skip to content

Commit ece4cc3

Browse files
committed
Add the concat transformer
1 parent 69c96a8 commit ece4cc3

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).<Paste>
44

55
## [Unreleased]
66

7+
## [0.2.3] - 2016-03-22
8+
### Added
9+
- `concat()`
10+
711
## [0.2.2] - 2016-03-18
812
### Added
913
- `newInstance()`

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ function ($value) {
186186
}
187187
```
188188

189+
#### T\concat(...$arguments)
190+
191+
Use `__` as the placeholder for where you want the value inserted:
192+
193+
```php
194+
use const TomPHP\Transform\__;
195+
196+
T\concat('Hello ', __, '!');
197+
198+
// Is equivilent to:
199+
200+
function ($value) {
201+
return 'Hello ' . $value . '!';
202+
}
203+
```
204+
189205
### Generic Transformations
190206

191207
#### T\argumentTo(callable $callable, array $argments = [__])

src/transform.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ function append($suffix)
251251
};
252252
}
253253

254+
/**
255+
* Returns a transformer which concatenates all it's arguments together.
256+
*
257+
* @param string[] ...$arguments
258+
*
259+
* @return \Closure
260+
*/
261+
function concat(...$arguments)
262+
{
263+
return function ($value) use ($arguments) {
264+
$arguments = substituteArguments($arguments, $value);
265+
266+
return implode('', $arguments);
267+
};
268+
}
269+
254270
/**
255271
* Returns a transformer which instantiates a $className object on its arguments and
256272
* returns the result.
@@ -283,3 +299,21 @@ function ($arg) use ($value) {
283299
return new $className(...$arguments);
284300
};
285301
}
302+
303+
/**
304+
* @internal
305+
*
306+
* @param array $arguments
307+
* @param mixed $value
308+
*
309+
* @return array
310+
*/
311+
function substituteArguments(array $arguments, $value)
312+
{
313+
return array_map(
314+
function ($arg) use ($value) {
315+
return $arg === __ ? $value : $arg;
316+
},
317+
$arguments
318+
);
319+
}

tests/ConcatTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace tests\TomPHP\Transform;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use TomPHP\Transform as T;
7+
use TomPHP\Transform\Exception\InvalidArgumentException;
8+
use TomPHP\Transform\Exception\UnexpectedValueException;
9+
use const TomPHP\Transform\__;
10+
11+
final class ConcatTest extends PHPUnit_Framework_TestCase
12+
{
13+
/** @test */
14+
public function it_concatenates_all_the_arguments_together()
15+
{
16+
$fn = T\concat('abc', 'def', 'ghi');
17+
18+
$this->assertSame('abcdefghi', $fn('xxx'));
19+
}
20+
21+
/** @test */
22+
public function it_inserts_the_value_at_the_placeholder()
23+
{
24+
$fn = T\concat('abc', __, 'ghi');
25+
26+
$this->assertSame('abcDEFghi', $fn('DEF'));
27+
}
28+
}

0 commit comments

Comments
 (0)