Skip to content

Commit

Permalink
Merge pull request #8 from transprime-research/feature-make-shorter-s…
Browse files Browse the repository at this point in the history
…yntaxes

Feature make shorter syntaxes
  • Loading branch information
omitobi authored May 2, 2022
2 parents 688894c + 266eba2 commit 1f64fdf
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2

before_script:
- composer self-update
Expand Down
17 changes: 17 additions & 0 deletions Helpers/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Transprime\Piper\CallablePiper;
use Transprime\Piper\Piper;

if (! function_exists('piper')) {
Expand All @@ -14,4 +15,20 @@ function piper($value, $callback = null) {
return (new Piper())
->pipe($value, $callback);
}

/**
* @param $value
* @param callable|array ...$lines
* @return mixed|string
* @throws \Transprime\Piper\Exceptions\PiperException
*/
function ducter($value, ...$lines) {
$piper = \piper($value);

return $piper->ln(...$lines);
}

function _p($value) {
return new CallablePiper(\piper($value));
}
}
78 changes: 74 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Let us take an array and do the following:

- flip the array to make the keys become the values and vice versa
- get the new keys
- change (the keys now) values to to upper case
- change (the keys now) values to upper case
- take the exact item with `[0 => 'ADE']`

```php
Expand All @@ -32,6 +32,79 @@ piper(['name' => 'ade', 'hobby' => 'coding'])
->to('array_intersect', [0 => 'ADE'])(); //returns ['ADE']
```

Or this in PHP 8.1+
Getting `['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D']` examples:

Use line - as in the line in Pipe-"line"
```php
piper("Hello World")
->ln(
htmlentities(...),
str_split(...),
[array_map(...), fn(string $part) => strtoupper($part)],
);
```

Think about ductape on a pipe. Ducter allows such neat calls to your functions
```php
ducter(
"Hello World",
htmlentities(...),
str_split(...),
[array_map(...), fn(string $part) => strtoupper($part)],
)
```

Call functions like an array:
```php
_p("Hello World")
[htmlentities(...)]
[str_split(...)]
[[array_map(...), strtoupper(...)]]()
```

How about Closure() on Closure
```php
_p("Hello World")
(htmlentities(...))
(strtoupper(...))
(str_split(...))
(array_map(...), strtoupper(...))()
```

Cleaner with underscore `_`
```php
_p("Hello World")
->_(htmlentities(...))
->_(str_split(...))
->_(array_map(...), strtoupper(...)));
```

Shortcut to `pipe()` is `p()`
```php
_p("Hello World")
->p(htmlentities(...))
->p(str_split(...))
->p(array_map(...), strtoupper(...))()
```

PHP 7.4 `fn()` like
```php
_p("Hello World")
->fn(htmlentities(...))
->fn(str_split(...))
->fn(array_map(...), strtoupper())
->fn()
```

Proxied call to globally available functions
```php
_p("Hello World")
->htmlentities()
->str_split()
->array_map(strtoupper(...))()
```

Instead of:

```php
Expand Down Expand Up @@ -160,9 +233,6 @@ piper('array_intersect', [0 => 'ADE'])
> Api implementation to be decided
## Additional Information

Be aware that this package is part of a series of "The Proof Of Concept".

See other packages in this series here:

- https://github.com/omitobi/conditional [A smart PHP if...elseif...else statement]
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": ">=6.0"
"phpunit/phpunit": ">=6.0",
"symfony/var-dumper": "^6.0"
},
"autoload": {
"psr-4": {
Expand All @@ -40,4 +41,4 @@
"Transprime\\Piper\\Tests\\": "tests/"
}
}
}
}
75 changes: 75 additions & 0 deletions src/CallablePiper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);

namespace Transprime\Piper;

class CallablePiper implements \ArrayAccess
{
private Piper $piper;

public function __construct(Piper $piper)
{
$this->piper = $piper->to(fn($vl) => $vl);
}

/**
* @param callable|array|null $callable
* @return mixed|self
*/
public function __invoke(...$callable): mixed
{
return $callable ? $this->to(...$callable) : $this->up();
}

public function to(callable $action, ...$extraParameters): static
{
$this->piper->to($action, ...$extraParameters);

return $this;
}

public function up(callable $action = null)
{
return $this->piper->up($action);
}

public function p(callable ...$callable)
{
return $this->__invoke(...$callable);
}

public function _(callable ...$callable)
{
return $this->__invoke(...$callable);
}

public function fn(callable ...$callable)
{
return $this->__invoke(...$callable);
}

public function offsetExists(mixed $offset): bool
{
return true;
}

public function offsetGet(mixed ...$offset): mixed
{
if (is_array($offset[0])) {
return $this->__invoke(...$offset[0]);
}

return $this->__invoke(...$offset);
}

public function offsetSet(mixed $offset, mixed $value): void {}

public function offsetUnset(mixed $offset): void {}

/**
* @return mixed|self
*/
public function __call(string $name, array $arguments)
{
return $this->__invoke($name, ...$arguments);
}
}
21 changes: 21 additions & 0 deletions src/On.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Transprime\Piper;

trait On
{
/**
* @param callable|array ...$functions
* @return mixed|string
* @throws Exceptions\PiperException
*/
public function ln(...$functions)
{
foreach ($functions as $function) {
$callables = is_array($function) ? $function : [$function];
$this->to(...$callables);
}

return $this->up();
}
}
2 changes: 2 additions & 0 deletions src/Piper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Piper
{
use On;

private $piped = [];

private $extraParameters = [];
Expand Down
110 changes: 109 additions & 1 deletion tests/PiperTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Piper\Tests;

Expand Down Expand Up @@ -230,6 +230,114 @@ public function testStringPassedIntoFirstPipeShouldBeTreatedAsString()
->up()
);
}

private function runIfOnPHP81()
{
return (float)phpversion() >= 8.1;
}

public function testPiperWithPHP81FirstClassCallable()
{
$this->assertEquals(['ADE'],
piper(['name' => 'ade', 'hobby' => 'coding'])
->to(array_flip(...))
->to(array_keys(...))
->to(array_map(...), fn($val) => strtoupper($val))
->to(array_intersect(...), [0 => 'ADE'])(),
);

$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
piper("Hello World")
->to(htmlentities(...))
->to(str_split(...))
->to(array_map(...), fn(string $part) => strtoupper($part))()
);
}

public function testPiperWithLn()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
piper("Hello World")
->ln(
htmlentities(...),
str_split(...),
[array_map(...), strtoupper(...)],
)
);
}

public function testPiperWithDucter()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
ducter(
"Hello World",
htmlentities(...),
str_split(...),
[array_map(...), strtoupper(...)],
)
);
}

public function testCallablePiper()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
_p("Hello World")
(htmlentities(...))
(str_split(...))
(array_map(...), strtoupper(...))()
);
}

public function testUnderscoredPiper()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
_p("Hello World")
->_(htmlentities(...))
->_(str_split(...))
->_(array_map(...), strtoupper(...))()
);
}

public function testPPiper()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
_p("Hello World")
->p(htmlentities(...))
->p(str_split(...))
->p(array_map(...), strtoupper(...))()
);
}

public function testFnPiper()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
_p("Hello World")
->fn(htmlentities(...))
->fn(str_split(...))
->fn(array_map(...), strtoupper(...))
->fn()
);
}

public function testIndexPiper()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
_p("Hello World")
[htmlentities(...)]
[str_split(...)]
[[array_map(...), strtoupper(...)]]()
);
}

public function testProxiedPiper()
{
$this->assertEquals(['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'],
_p("Hello World")
->htmlentities()
->str_split()
->array_map(strtoupper(...))()
);
}
}

class StrManipulator
Expand Down

0 comments on commit 1f64fdf

Please sign in to comment.