-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Cocur\Chain\Link; | ||
|
||
use Cocur\Chain\Chain; | ||
|
||
/** | ||
* FlatMap. | ||
* | ||
* @author Nicolas Reynis | ||
*/ | ||
trait FlatMap | ||
{ | ||
/** | ||
* @return Chain | ||
*/ | ||
public function flatMap(callable $callback) | ||
{ | ||
$flattened = []; | ||
foreach ($this->array as $index => $element) { | ||
$transfomation = $callback($element, $index); | ||
if (is_array($transfomation)) { | ||
array_push($flattened, ...$transfomation); | ||
} else { | ||
$flattened[] = $transfomation; | ||
} | ||
} | ||
$this->array = $flattened; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Cocur\Chain\Link; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* MapTest. | ||
* | ||
* @author Nicolas Reynis | ||
* @group unit | ||
*/ | ||
class FlatMapTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
* @covers Cocur\Chain\Link\FlatMap::flatMap() | ||
*/ | ||
public function flatMapAppliesMapToArray() | ||
{ | ||
/** @var \Cocur\Chain\Link\FlatMap $mock */ | ||
$mock = $this->getMockForTrait('Cocur\Chain\Link\FlatMap'); | ||
$mock->array = ['foobar', 'bar']; | ||
$mock->flatMap(function ($v) { return [str_replace('bar', 'foo', $v)]; }); | ||
|
||
$this->assertEquals('foofoo', $mock->array[0]); | ||
$this->assertEquals('foo', $mock->array[1]); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Cocur\Chain\Link\FlatMap::flatMap() | ||
*/ | ||
public function flatMapCallbackReceivesAlsoArrayKeys() | ||
{ | ||
/** @var \Cocur\Chain\Link\FlatMap $mock */ | ||
$mock = $this->getMockForTrait('Cocur\Chain\Link\FlatMap'); | ||
$mock->array = ['foo' => 'fizz', 'bar' => 'buzz']; | ||
$mock->flatMap(function ($v, $k) { | ||
return $k == 'foo' ? [str_replace('fizz', 'bang', $v)] : [$v]; | ||
}); | ||
|
||
$this->assertEquals(['bang', 'buzz'], $mock->array); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Cocur\Chain\Link\FlatMap::flatMap() | ||
*/ | ||
public function flatMapCallbackCanReturnScalarOrArray() | ||
{ | ||
/** @var \Cocur\Chain\Link\FlatMap $mock */ | ||
$mock = $this->getMockForTrait('Cocur\Chain\Link\FlatMap'); | ||
$mock->array = ['fizz', 'buzz']; | ||
$mock->flatMap(function ($v, $k) { | ||
return $k == 1 ? [$v] : $v; | ||
}); | ||
|
||
$this->assertEquals(['fizz', 'buzz'], $mock->array); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Cocur\Chain\Link\FlatMap::flatMap() | ||
*/ | ||
public function flatMapAcceptsEmptyArray() | ||
{ | ||
/** @var \Cocur\Chain\Link\FlatMap $mock */ | ||
$mock = $this->getMockForTrait('Cocur\Chain\Link\FlatMap'); | ||
$mock->array = []; | ||
$mock->flatMap(function ($v) { | ||
return $v; | ||
}); | ||
|
||
$this->assertEquals([], $mock->array); | ||
} | ||
} |