Skip to content

Commit

Permalink
Merge pull request #1 from transprime-research/feature/statically-cal…
Browse files Browse the repository at this point in the history
…ling-piper-pipe

Feature/statically calling piper pipe
  • Loading branch information
omitobi authored Apr 24, 2020
2 parents 9117bc7 + 195cf63 commit ed5afe6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php

php:
- 7.4

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev

script: vendor/bin/phpunit
2 changes: 1 addition & 1 deletion Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
*/
function piper($value, $callback = null) {
return (new Piper())
->on($value, $callback);
->pipe($value, $callback);
}
}
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use Transprime\Piper\Piper;

// Normal
$piper = new Piper();
$piper->on(['AA'])->to('strtolower')->up();
$piper->pipe(['AA'])->to('strtolower')->up();

// Better
$piper->on(['AA'])->to('strtolower')();
Expand All @@ -65,16 +65,19 @@ $piper->on(['AA'])->to('strtolower')();
`piper()` function is a helper function. It is normally called like so:

```php
// Good
Piper::on('AA')->to('strtolower')->up();

//Better
Piper::on('AA')->to('strtolower')();

// Nifty
piper(['AA'])->to('strtolower')();
```

Piper `on()` method accepts a callable instance on the second parameter that would be immediately on the first parameter:
Piper `piper()` function accepts a callable instance on the second parameter that would be immediately on the first parameter:

Say we have this class:

Second callable parameter to `on()` method, which 'NAME' shall first be called on:
Second callable parameter to `piper()` function, which 'NAME' shall first be called on:

```php
// test global method
Expand All @@ -86,6 +89,7 @@ piper('NAME', 'strtolower') // NAME becomes name
Also accepts a class with an accessible method name:

Say we have this class:

```php
class StrManipulator
{
Expand Down Expand Up @@ -128,15 +132,6 @@ Please see `\Piper\Tests\PiperTest` for more examples.

## Coming Soon

- Calling piper statically
```php
// Good
Piper::on(['AA'])->to('strtolower')->up();

//Better
Piper::on(['AA'])->to('strtolower')();
```

- Backward Pipe with `fro()` looking like:

```php
Expand Down
7 changes: 6 additions & 1 deletion src/Piper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public function __invoke(callable $action = null)
return $this->up($action);
}

public function on($value, callable $callback = null)
public static function on($value, callable $callback = null)
{
return (new static())->pipe($value, $callback);
}

public function pipe($value, callable $callback = null)
{
if (!empty($this->piped)) {
throw new PiperException('on() must be called only once');
Expand Down
16 changes: 14 additions & 2 deletions tests/PiperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testPipeToUp()
{
$piper = new Piper();

$result = $piper->on('name')
$result = $piper->pipe('name')
->to(fn($name) => strtoupper($name))
->up();

Expand Down Expand Up @@ -63,7 +63,7 @@ public function testPipeMethodCannotBeCalledMoreThanOnce()
$this->expectExceptionMessage('on() must be called only once');

piper(fn() => 'NAME')
->on('1')
->pipe('1')
->to(fn($name) => strtolower($name))
->up();
}
Expand Down Expand Up @@ -149,6 +149,18 @@ public function testMakePiperInvokableWithoutCallingUpMethod()

$this->assertSame('Name', $result);
}

public function testPiperOnStaticCreation()
{
$this->assertEquals(
'n,a,m,e',
Piper::on('NAME')
->to('str_split')
->to('implode', ',')
->to('strtolower')
->up()
);
}
}

class StrManipulator
Expand Down

0 comments on commit ed5afe6

Please sign in to comment.