Skip to content

Commit

Permalink
Remove support to php 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiors committed Apr 19, 2017
1 parent 316d910 commit ee5591a
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 137 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*.{js,twig,php,xml,json,yml}]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_size = 4
indent_style = space
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
redis: false
rabbitmq: false
php:
version: 5.5
version: 7.0

tests:
override:
Expand Down
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ How to use
```php
use Sergiors\Pipeline\Pipeline;

$pipeline = (new Pipeline())
$pipeline = (new Pipeline)
->pipe(function ($payload) {
return $payload + 2;
})
Expand All @@ -29,7 +29,7 @@ echo $pipeline(10); // => 24
```

```php
$pipeline = (new Pipeline())
$pipeline = (new Pipeline)
->pipe(function ($payload, $container) {
...
})
Expand All @@ -47,7 +47,7 @@ You can use `Sergiors\Pipeline\Reduce`, `Sergiors\Pipeline\Filter` and `Sergiors
use Sergiors\Pipeline\Pipeline;
use Sergiors\Pipeline\Filter;

$getOrgs = (new Pipeline())
$getOrgs = (new Pipeline)
->pipe(new Filter(function ($org) {
return $org instanceof OrgInterface;
}));
Expand All @@ -58,18 +58,6 @@ $users = [...];
print_r($getOrgs($users));
```

Or call native functions.
```php
use Sergiors\Pipeline\Pipelne;

$implodeUpper = (new Pipeline())
->implode(',', Pipeline::_)
->strtoupper();
echo $implodeUpper(['a', 'b']); // => A,B
```

`Pipeline::_` is the placeholder for payload. If you don't set the placeholder, the payload will be last argument.

Motivation
----------
[Collection Pipeline](http://martinfowler.com/articles/collection-pipeline/)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
}
],
"require": {
"php": "^5.5|^7.0"
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"phpunit/phpunit": "^5.2",
"pdepend/pdepend": "~2.0",
"phing/phing": "~2.10",
"phploc/phploc": "~2.1",
Expand Down
20 changes: 7 additions & 13 deletions src/Filter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace Sergiors\Pipeline;

/**
Expand All @@ -8,25 +10,17 @@
final class Filter
{
/**
* @var \Closure
* @var callable
*/
private $fn;
private $callable;

/**
* @param \Closure $fn
*/
public function __construct(\Closure $fn)
public function __construct(callable $callable)
{
$this->fn = $fn;
$this->callable = $callable;
}

/**
* @param mixed $payload
*
* @return mixed
*/
public function __invoke($payload)
{
return array_filter($payload, $this->fn);
return array_filter($payload, $this->callable);
}
}
22 changes: 8 additions & 14 deletions src/Map.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace Sergiors\Pipeline;

/**
Expand All @@ -8,25 +10,17 @@
final class Map
{
/**
* @var \Closure
* @var callable
*/
private $fn;
private $callable;

/**
* @param \Closure $fn
*/
public function __construct(\Closure $fn)
public function __construct(callable $callable)
{
$this->fn = $fn;
$this->callable = $callable;
}

/**
* @param mixed $payload
*
* @return mixed
*/

public function __invoke($payload)
{
return array_map($this->fn, $payload);
return array_map($this->callable, $payload);
}
}
72 changes: 13 additions & 59 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
@@ -1,85 +1,39 @@
<?php

declare(strict_types = 1);

namespace Sergiors\Pipeline;

/**
* @author Sérgio Rafael Siqueira <sergio@inbep.com.br>
*/
final class Pipeline
{
/**
* Placeholder
*/
const _ = '%';

/**
* @var callable[]
*/
private $callbacks;

/**
* @param array $callbacks
*/
public function __construct(array $callbacks = [])
{
$this->callbacks = array_filter($callbacks, 'is_callable');
}

/**
* @param callable $callback
*
* @return Pipeline
*/
public function pipe(callable $callback)
public function __construct(callable ...$callbacks)
{
return new self(array_merge($this->callbacks, [$callback]));
$this->callbacks = $callbacks;
}

/**
* @param mixed $payload
*
* @return mixed
*/
public function process($payload /* ...$args */)
public function pipe(callable $callback): self
{
$rest = array_slice(func_get_args(), 1);

return array_reduce($this->callbacks, function ($payload, $callback) use ($rest) {
return call_user_func_array($callback, array_merge([$payload], $rest));
}, $payload);
return new self(...array_merge($this->callbacks, [$callback]));
}

/**
* @param callable $fn
* @param array $args
*
* @return Pipeline
*/
public function __call(callable $fn, array $args)
public function process($payload, ...$restParams)
{
$ks = (new self())
->pipe(new Filter(function ($x) {
return self::_ === $x;
}))
->pipe('array_keys')
->process($args);

return $this->pipe(function ($payload) use ($fn, $args, $ks) {
if ([] === $ks) {
return call_user_func_array($fn, array_merge($args, [$payload]));
}

return call_user_func_array($fn, array_replace($args, [
$ks[0] => $payload
]));
});
return array_reduce($this->callbacks,
function ($payload, callable $callback) use ($restParams) {
return $callback(...array_merge([$payload], $restParams));
}, $payload);
}

/**
* @return mixed
*/
public function __invoke(/* ...$args */)
public function __invoke(...$args)
{
return call_user_func_array([$this, 'process'], func_get_args());
return $this->process(...$args);
}
}
21 changes: 7 additions & 14 deletions src/Reduce.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace Sergiors\Pipeline;

/**
Expand All @@ -8,32 +10,23 @@
final class Reduce
{
/**
* @var \Closure
* @var callable
*/
private $fn;
private $callable;

/**
* @var mixed
*/
private $initial;

/**
* @param \Closure $fn
* @param null $initial
*/
public function __construct(\Closure $fn, $initial = null)
public function __construct(callable $callable, $initial = null)
{
$this->fn = $fn;
$this->callable = $callable;
$this->initial = $initial;
}

/**
* @param mixed $payload
*
* @return mixed
*/
public function __invoke($payload)
{
return array_reduce($payload, $this->fn, $this->initial);
return array_reduce($payload, $this->callable, $this->initial);
}
}
3 changes: 2 additions & 1 deletion tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Sergiors\Pipeline\Tests;

use PHPUnit\Framework\TestCase;
use Sergiors\Pipeline\Pipeline;
use Sergiors\Pipeline\Filter;
use Sergiors\Pipeline\Map;

class FilterTest extends \PHPUnit_Framework_TestCase
class FilterTest extends TestCase
{
/**
* @test
Expand Down
3 changes: 2 additions & 1 deletion tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Sergiors\Pipeline\Tests;

use PHPUnit\Framework\TestCase;
use Sergiors\Pipeline\Pipeline;
use Sergiors\Pipeline\Map;

class MapTest extends \PHPUnit_Framework_TestCase
class MapTest extends TestCase
{
/**
* @test
Expand Down
20 changes: 4 additions & 16 deletions tests/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Sergiors\Pipeline\Tests;

use PHPUnit\Framework\TestCase;
use Sergiors\Pipeline\Pipeline;
use Sergiors\Pipeline\Tests\Fixture\Bar;
use Sergiors\Pipeline\Tests\Fixture\Foo;

class PipelineTest extends \PHPUnit_Framework_TestCase
class PipelineTest extends TestCase
{
/**
* @test
Expand All @@ -26,24 +27,11 @@ public function shouldConcat()

/**
* @test
*/
public function shouldCallPhpFuncs()
{
$implode = (new Pipeline())->implode(',', Pipeline::_)->strtoupper();
$this->assertEquals('HELLO,WORLD', $implode(['hello','world']));

$hello = (new Pipeline())
->strrev()
->strtoupper();
$this->assertEquals('DLROW OLLEH', $hello('hello world'));
}

/**
* @test
* @expectedException \Throwable
*/
public function shouldSkipNonCallable()
{
$pipeline = (new Pipeline([new Bar()]))
$pipeline = (new Pipeline(new Bar()))
->pipe(new Foo());

$this->assertEquals('pipefoo', $pipeline('pipe'));
Expand Down
3 changes: 2 additions & 1 deletion tests/ReduceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Sergiors\Pipeline\Tests;

use PHPUnit\Framework\TestCase;
use Sergiors\Pipeline\Pipeline;
use Sergiors\Pipeline\Reduce;

class ReduceTest extends \PHPUnit_Framework_TestCase
class ReduceTest extends TestCase
{
/**
* @test
Expand Down

0 comments on commit ee5591a

Please # to comment.