Skip to content

Commit

Permalink
Add support for first class callable syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiaanluca committed Mar 17, 2022
1 parent 21c0b44 commit 1a10168
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Method chaining (or fluent expressions) **for any value using any method.**
- [How to install](#how-to-install)
- [How to use](#how-to-use)
- [The basics](#the-basics)
- [Using first class callable syntax](#using-first-class-callable-syntax-enabling-ide-autocompletion)
- [Using closures](#using-closures)
- [Using class methods](#using-class-methods)
- [What does it solve?](#what-does-it-solve)
Expand Down Expand Up @@ -60,7 +61,7 @@ Pipe::from('hello')->strtoupper()->get();
// "HELLO"
```

A few alternatives to write the same:
A few alternatives to create the same instance:

```php
take('hello')->strtoupper()->get();
Expand Down Expand Up @@ -107,6 +108,33 @@ Pipe::from(['key' => 'value'])
// "key"
```

### Using first class callable syntax (enabling IDE autocompletion)

Since PHP 8.1, you can use a first class callable syntax, or simply put an anonymous function, to pipe the value through. This enables **full method autocompletion**.

```php
take('STRING')
->pipe(strtolower(...))
->get()

// "string"
```

Or using parameters:

```php
Pipe::from('https://sebastiaanluca.com/blog')
->pipe(parse_url(...))
->end()
->pipe(substr(...), PIPED_VALUE, 3)
->pipe(strtoupper(...))
->get(),

// "OG"
```



### Using closures

Sometimes standard methods don't cut it and you need to perform a custom operation on a value in the process. You can do so using a closure:
Expand Down Expand Up @@ -151,6 +179,32 @@ class MyClass

If you don't want to use the internal pipe proxy and pass `$this`, there are two other ways you can use class methods.

Using first class callable syntax:

```php
class MyClass
{
public function __construct()
{
Pipe::from('HELLO')
->pipe($this->lowercase(...))
->get();

// "hello"
}

/**
* @param string $value
*
* @return string
*/
public function lowercase(string $value) : string
{
return mb_strtolower($value);
}
}
```

Using an array (for public methods only):

```php
Expand Down
19 changes: 18 additions & 1 deletion tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function it can transform a value using a callable string method
* @test
* @requires PHP >= 8.1
*/
public function it can transform a value using a first call callable string method using the method directly(): void
public function it can transform a value using a first class callable method(): void
{
$this->assertSame(
'stringggg',
Expand All @@ -63,6 +63,23 @@ public function it can transform a value using a first call callable s
);
}

/**
* @test
* @requires PHP >= 8.1
*/
public function it can transform a value using a first class callable method with parameters(): void
{
$this->assertSame(
'OG',
Pipe::from('https://sebastiaanluca.com/blog')
->pipe(parse_url(...))
->end()
->pipe(substr(...), PIPED_VALUE, 3)
->pipe(strtoupper(...))
->get(),
);
}

/**
* @test
*/
Expand Down

0 comments on commit 1a10168

Please sign in to comment.