From 1a10168751e226da0fbb4c5b098e3a539cf16b70 Mon Sep 17 00:00:00 2001 From: Sebastiaan Luca Date: Thu, 17 Mar 2022 12:43:00 +0100 Subject: [PATCH] Add support for first class callable syntax --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++- tests/MethodsTest.php | 19 ++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9050fb9..1885379 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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(); @@ -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: @@ -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 diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php index 7f2ebca..d55f795 100644 --- a/tests/MethodsTest.php +++ b/tests/MethodsTest.php @@ -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', @@ -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 */