{"id":13579233,"url":"https://github.com/phpfn/pipe","last_synced_at":"2026-01-11T12:51:50.186Z","repository":{"id":57040400,"uuid":"191606030","full_name":"phpfn/pipe","owner":"phpfn","description":"[READONLY] Library for implementing function call chains","archived":false,"fork":false,"pushed_at":"2020-11-22T21:18:17.000Z","size":124,"stargazers_count":73,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-02T20:48:06.393Z","etag":null,"topics":["chain","functional","php","pipe"],"latest_commit_sha":null,"homepage":"https://github.com/phpfn/phpfn","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phpfn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-12T16:17:26.000Z","updated_at":"2024-11-26T06:25:51.000Z","dependencies_parsed_at":"2022-08-24T01:10:33.066Z","dependency_job_id":null,"html_url":"https://github.com/phpfn/pipe","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpfn%2Fpipe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpfn%2Fpipe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpfn%2Fpipe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpfn%2Fpipe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpfn","download_url":"https://codeload.github.com/phpfn/pipe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399872,"owners_count":20932876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chain","functional","php","pipe"],"created_at":"2024-08-01T15:01:37.580Z","updated_at":"2026-01-11T12:51:50.179Z","avatar_url":"https://github.com/phpfn.png","language":"PHP","readme":"# Pipe\n\nObject-oriented pipe operator implementation based \non [RFC Pipe Operator](https://wiki.php.net/rfc/pipe-operator).\n\n## Installation\n\nLibrary can be installed into any PHP application:\n- Using [`Composer`](https://getcomposer.org/) dependency manager \n- [The Force](https://www.youtube.com/watch?v=o2we_B6hDrY) for the Jedi Developers\n\n```sh\n$ composer require phpfn/pipe\n```\n\nIn order to access library make sure to include `vendor/autoload.php` \nin your file.\n\n```php\n\u003c?php\n\nrequire __DIR__ . '/vendor/autoload.php';\n```\n\n\n## Usage\n\nA common PHP OOP pattern is the use of method chaining, or what is \nalso known as \"Fluent Expressions\". So named for the way one method \nflows into the next to form a conceptual hyper-expression.\n\nHowever, when using the functional approach this can lead to reduced \nreadability, polluted symbol tables, or static-analysis defying \ntype inconsistency such as in the following example:\n\n```php\n\u003c?php\n\n$snakeCase = strtolower(\n    preg_replace('/(.)(?=[A-Z])/u', '$1_', \n        preg_replace('/\\s+/u', '', \n            ucwords('HelloWorld')\n        )\n    )\n);\n             \nvar_dump($snakeCase); // \"hello_world\"\n```\n\nThe pipe library fixes this problem, allows you to \nchain the execution of pure functions:\n\n```php\npipe('Hello World')\n    -\u003eucwords(_)\n    -\u003epreg_replace('/\\s+/u', '', _)\n    -\u003epreg_replace('/(.)(?=[A-Z])/u', '$1_', _)\n    -\u003estrtolower(_)\n    -\u003evar_dump;\n//\n// string(11) \"hello_world\"\n//\n```\n\n### Another Example\n\nSee: [https://wiki.php.net/rfc/pipe-operator#file_collection_example](https://wiki.php.net/rfc/pipe-operator#file_collection_example)\n\n```php\n\u003c?php\n$result = array_merge(\n    $result,\n    namespaced\\func\\get_file_arg(\n        array_map(\n            function ($x) use ($arg) {\n                return $arg . '/' . $x;\n            },\n            array_filter(\n                scandir($arg),\n                function ($x) {\n                    return $x !== '.' \u0026\u0026 $x !== '..';\n                }\n            )\n        )\n    )\n);\n```\n\nWith this library, the above could be easily rewritten as:\n\n```php\n\u003c?php\n\n$result = pipe($arg)\n    -\u003escanDir($arg)\n    -\u003earray_filter(_, fn($x): bool =\u003e $x !== '.' \u0026\u0026 $x !== '..')\n    -\u003earray_map(fn($x): string =\u003e $arg . '/' . $x, _)\n    -\u003euse('namespaced\\func')-\u003eget_file_arg\n    -\u003earray_merge($result, _);\n```\n\n## Working With Value\n\nTo pass a value as an argument to a function, use the \nunderscore (`_`) character:\n\n```php\n\u003c?php\n\npipe('hello')\n    -\u003estr_replace('o', '', _)\n    -\u003evar_dump; // \"hell\"\n```\n\nYou can omit parentheses if only one argument is used:\n\n```php\n\u003c?php\n\npipe('some')\n    -\u003eis_array\n    -\u003evar_dump; // bool(false) \n```\n\nTo get the value, use one of the options:\n\n```php\n\u003c?php\n$context = pipe('hello')-\u003estrtoupper;\n\nvar_dump($context);\n// object(Fun\\Pipe\\Pipe)#8 (1) { ... } \n\nvar_dump($context());\n// string(5) \"HELLO\"\n```\n\n## Working With Namespace\n\nLet's take a simple example of such code:\n\n```php\nnamespace {\n    function foo() { return __FUNCTION__; }\n}\n\nnamespace Example {\n    function foo() { return __FUNCTION__; }\n}\n```\n\nLet's try to manage the namespace:\n\n```php\n$context = pipe()-\u003euse('Example')-\u003efoo;\n\necho $context(); // 'Example\\\\foo'\n\n$context = $context-\u003efoo;\n\necho $context(); // 'foo'\n```\n\nPlease note that the `use` function applies only to the subsequent function, \nall further operations performed in the current context:\n\n```php\npipe()\n    -\u003euse('Some\\\\Namespace')-\u003efoo // Call \"\\Some\\Namespace\\foo()\"\n    -\u003efoo // Call \"\\foo()\"\n;\n```\n\nIn order to perform several operations in another namespace, use an anonymous \nfunction as the second `use` argument.\n\n```php\npipe()\n    -\u003euse('Some\\\\Namespace', fn($pipe) =\u003e \n        $pipe\n            -\u003ea // Call \"\\Some\\Namespace\\a()\"\n            -\u003eb // Call \"\\Some\\Namespace\\b()\"\n    )\n    -\u003ea // Call \"a()\"\n;\n```\n\n\u003e Note that the behavior of the `-\u003euse()` method differs depending on whether \n\u003e the second argument is passed.\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpfn%2Fpipe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpfn%2Fpipe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpfn%2Fpipe/lists"}