-
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.
- Loading branch information
Showing
12 changed files
with
63 additions
and
137 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,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 |
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ build: | |
redis: false | ||
rabbitmq: false | ||
php: | ||
version: 5.5 | ||
version: 7.0 | ||
|
||
tests: | ||
override: | ||
|
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
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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
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