Skip to content

Commit 1a10168

Browse files
Add support for first class callable syntax
1 parent 21c0b44 commit 1a10168

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Method chaining (or fluent expressions) **for any value using any method.**
1919
- [How to install](#how-to-install)
2020
- [How to use](#how-to-use)
2121
- [The basics](#the-basics)
22+
- [Using first class callable syntax](#using-first-class-callable-syntax-enabling-ide-autocompletion)
2223
- [Using closures](#using-closures)
2324
- [Using class methods](#using-class-methods)
2425
- [What does it solve?](#what-does-it-solve)
@@ -60,7 +61,7 @@ Pipe::from('hello')->strtoupper()->get();
6061
// "HELLO"
6162
```
6263

63-
A few alternatives to write the same:
64+
A few alternatives to create the same instance:
6465

6566
```php
6667
take('hello')->strtoupper()->get();
@@ -107,6 +108,33 @@ Pipe::from(['key' => 'value'])
107108
// "key"
108109
```
109110

111+
### Using first class callable syntax (enabling IDE autocompletion)
112+
113+
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**.
114+
115+
```php
116+
take('STRING')
117+
->pipe(strtolower(...))
118+
->get()
119+
120+
// "string"
121+
```
122+
123+
Or using parameters:
124+
125+
```php
126+
Pipe::from('https://sebastiaanluca.com/blog')
127+
->pipe(parse_url(...))
128+
->end()
129+
->pipe(substr(...), PIPED_VALUE, 3)
130+
->pipe(strtoupper(...))
131+
->get(),
132+
133+
// "OG"
134+
```
135+
136+
137+
110138
### Using closures
111139

112140
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:
@@ -151,6 +179,32 @@ class MyClass
151179

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

182+
Using first class callable syntax:
183+
184+
```php
185+
class MyClass
186+
{
187+
public function __construct()
188+
{
189+
Pipe::from('HELLO')
190+
->pipe($this->lowercase(...))
191+
->get();
192+
193+
// "hello"
194+
}
195+
196+
/**
197+
* @param string $value
198+
*
199+
* @return string
200+
*/
201+
public function lowercase(string $value) : string
202+
{
203+
return mb_strtolower($value);
204+
}
205+
}
206+
```
207+
154208
Using an array (for public methods only):
155209

156210
```php

tests/MethodsTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function it can transform a value using a callable string method
5353
* @test
5454
* @requires PHP >= 8.1
5555
*/
56-
public function it can transform a value using a first call callable string method using the method directly(): void
56+
public function it can transform a value using a first class callable method(): void
5757
{
5858
$this->assertSame(
5959
'stringggg',
@@ -63,6 +63,23 @@ public function it can transform a value using a first call callable s
6363
);
6464
}
6565

66+
/**
67+
* @test
68+
* @requires PHP >= 8.1
69+
*/
70+
public function it can transform a value using a first class callable method with parameters(): void
71+
{
72+
$this->assertSame(
73+
'OG',
74+
Pipe::from('https://sebastiaanluca.com/blog')
75+
->pipe(parse_url(...))
76+
->end()
77+
->pipe(substr(...), PIPED_VALUE, 3)
78+
->pipe(strtoupper(...))
79+
->get(),
80+
);
81+
}
82+
6683
/**
6784
* @test
6885
*/

0 commit comments

Comments
 (0)