-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from pitchart/feature/resource-termination
Feature/resource termination
- Loading branch information
Showing
6 changed files
with
261 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
|
||
namespace Pitchart\Transformer\Reducer\Termination; | ||
|
||
|
||
use Pitchart\Transformer\Termination; | ||
use function Pitchart\Transformer\Transducer\to_array; | ||
use function Pitchart\Transformer\transform; | ||
|
||
class ToCsv implements Termination | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $delimiter; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $enclosure; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $escapeChar; | ||
|
||
/** | ||
* ToCsv constructor. | ||
* | ||
* @param string $delimiter | ||
* @param string $enclosure | ||
* @param string $escapeChar | ||
*/ | ||
public function __construct(string $delimiter = ',', string $enclosure = '"', string $escapeChar = "\\") | ||
{ | ||
$this->delimiter = $delimiter; | ||
$this->enclosure = $enclosure; | ||
$this->escapeChar = $escapeChar; | ||
} | ||
|
||
public function init() | ||
{ | ||
return fopen('php://temp', 'w+'); | ||
} | ||
|
||
/** | ||
* @param resource $result | ||
* @param mixed $current | ||
*/ | ||
public function step($result, $current) | ||
{ | ||
if (!is_array($current)) { | ||
if (is_iterable($current)) { | ||
$current = transform($current)->toArray(); | ||
} | ||
else { | ||
$current = [(string) $current]; | ||
} | ||
} | ||
fputcsv($result, $current, $this->delimiter, $this->enclosure, $this->escapeChar); | ||
return $result; | ||
} | ||
|
||
public function complete($result) | ||
{ | ||
return $result; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
|
||
namespace Pitchart\Transformer\Reducer\Termination; | ||
|
||
|
||
use Pitchart\Transformer\Termination; | ||
|
||
class ToResource implements Termination | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $callback; | ||
|
||
/** | ||
* ToResource constructor. | ||
* | ||
* @param callable $callback | ||
*/ | ||
public function __construct(callable $callback = null) | ||
{ | ||
if ($callback == null) { | ||
$callback = 'Pitchart\Transformer\identity'; | ||
} | ||
$this->callback = $callback; | ||
} | ||
|
||
public function init() | ||
{ | ||
return fopen('php://temp', 'w+'); | ||
} | ||
|
||
/** | ||
* @param resource $result | ||
* @param mixed $current | ||
*/ | ||
public function step($result, $current) | ||
{ | ||
fwrite($result, (string) ($this->callback)($current)); | ||
return $result; | ||
} | ||
|
||
public function complete($result) | ||
{ | ||
return $result; | ||
} | ||
|
||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace Pitchart\Transformer\Tests\Reducer\Termination; | ||
|
||
use Pitchart\Transformer\Reducer; | ||
use Pitchart\Transformer\Reducer\Termination\ToCsv; | ||
use PHPUnit\Framework\TestCase; | ||
use Pitchart\Transformer\Termination; | ||
use function Pitchart\Transformer\transform; | ||
|
||
class ToCsvTest extends TestCase | ||
{ | ||
public function test_is_a_reducer() | ||
{ | ||
$resource = new ToCsv(); | ||
self::assertInstanceOf(Reducer::class, $resource); | ||
} | ||
|
||
public function test_is_a_termination() | ||
{ | ||
$resource = new ToCsv(); | ||
self::assertInstanceOf(Termination::class, $resource); | ||
} | ||
|
||
public function test_initializes_with_a_resource() | ||
{ | ||
$resource = new ToCsv(); | ||
$init = $resource->init(); | ||
|
||
self::assertInternalType('resource', $init); | ||
} | ||
|
||
public function test_writes_array_content_on_resource() | ||
{ | ||
$resource = transform([[1, 2, 3, 4]])->toCsv(); | ||
rewind($resource); | ||
|
||
self::assertEquals("1,2,3,4\n", stream_get_contents($resource)); | ||
} | ||
|
||
public function test_writes_iterable_content_on_resource() | ||
{ | ||
$resource = transform([new \ArrayIterator([1, 2, 3, 4])])->toCsv(); | ||
rewind($resource); | ||
|
||
self::assertEquals("1,2,3,4\n", stream_get_contents($resource)); | ||
} | ||
|
||
public function test_writes_flat_content_on_resource() | ||
{ | ||
$resource = transform([1, 2, 3, 4])->toCsv(); | ||
rewind($resource); | ||
|
||
self::assertEquals("1\n2\n3\n4\n", stream_get_contents($resource)); | ||
} | ||
|
||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace Pitchart\Transformer\Tests\Reducer\Termination; | ||
|
||
use Pitchart\Transformer\Reducer; | ||
use Pitchart\Transformer\Reducer\Termination\ToResource; | ||
use PHPUnit\Framework\TestCase; | ||
use Pitchart\Transformer\Termination; | ||
use function Pitchart\Transformer\transform; | ||
|
||
class ToResourceTest extends TestCase | ||
{ | ||
public function test_is_a_reducer() | ||
{ | ||
$resource = new ToResource(function ($item) { return (string) $item; }); | ||
self::assertInstanceOf(Reducer::class, $resource); | ||
} | ||
|
||
public function test_is_a_termination() | ||
{ | ||
$resource = new ToResource(function ($item) { return (string) $item; }); | ||
self::assertInstanceOf(Termination::class, $resource); | ||
} | ||
|
||
public function test_initializes_with_a_resource() | ||
{ | ||
$resource = new ToResource(function ($item) { return (string) $item; }); | ||
$init = $resource->init(); | ||
|
||
self::assertInternalType('resource', $init); | ||
} | ||
|
||
public function test_writes_content_on_resource() | ||
{ | ||
$resource = transform([1, 2, 3, 4])->toResource(); | ||
rewind($resource); | ||
|
||
self::assertEquals('1234', stream_get_contents($resource)); | ||
} | ||
|
||
public function test_writes_content_on_resource_with_callback() | ||
{ | ||
$resource = transform([1, 2, 3, 4])->toResource(function ($item) { | ||
return sprintf("%s\n", $item); | ||
}); | ||
rewind($resource); | ||
|
||
self::assertEquals("1\n2\n3\n4\n", stream_get_contents($resource)); | ||
} | ||
} |