{"id":16721117,"url":"https://github.com/khalyomede/piper-php","last_synced_at":"2025-07-09T04:34:42.013Z","repository":{"id":57006171,"uuid":"96573194","full_name":"khalyomede/piper-php","owner":"khalyomede","description":"Puts an end to unreadable PHP code.","archived":false,"fork":false,"pushed_at":"2017-07-12T21:01:37.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-07T11:53:17.880Z","etag":null,"topics":["cascading","input-output","php","pipe","pipeline","pipeline-framework","pipelines","reusable-class","reusable-functions"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khalyomede.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-07T20:10:22.000Z","updated_at":"2017-07-10T21:14:55.000Z","dependencies_parsed_at":"2022-08-21T14:30:50.646Z","dependency_job_id":null,"html_url":"https://github.com/khalyomede/piper-php","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/khalyomede/piper-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fpiper-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fpiper-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fpiper-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fpiper-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khalyomede","download_url":"https://codeload.github.com/khalyomede/piper-php/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fpiper-php/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264394773,"owners_count":23601359,"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":["cascading","input-output","php","pipe","pipeline","pipeline-framework","pipelines","reusable-class","reusable-functions"],"created_at":"2024-10-12T22:28:51.554Z","updated_at":"2025-07-09T04:34:41.992Z","avatar_url":"https://github.com/khalyomede.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# piper-php\nPuts an end to unreadable PHP code.\n\n```php\nuse Khalyomede\\Piper;\nuse You\\PiperArrayFilterNumber as ArrayFilterNumber;\nuse Her\\PiperArrayAverage as ArrayAverage;\n\nPiper::set([12, 8, 'apple', 19, 16, 'kiwi', 'banana'])\n  -\u003epipe( ArrayFilterNumber::do() )\n  -\u003epipe( ArrayAverage::do() )\n  -\u003epipe( 'intval' )\n  -\u003epipe( 'addOne' ) // your previously created function\n  -\u003epipe( function() { return Piper::input() + 3; } )\n  -\u003eecho();\n\n// 17\n```\n## Features\n- Cascading input-output pipe logic\n- Possibility to echo or get the final result\n- Large possibilites with either custom functions or Piper Comunity classes\n- Recursive pipe call\n\n## Why should I use Piper?\nIf you want to use a global class that let you mix multiple logic, from local functions to custom classes, from custom Piper classes to PHP functions, and you are eager to build beautiful, readable, and reusable codes, then Piper-PHP is made for you. If you love the [Gulp.js](http://gulpjs.com/) way, you will love Piper-PHP too!\n\n## Can I do a port of Piper-PHP in another language?\nYes for sure, we even encourage this! We want to build a better developper experience (DX), so feel free to copy and adapt this concept! Sharing is caring.\n\n## Who initiated the project?\nMe and my brother, [aminnairi](https://github.com/aminnairi).\n\n## Why creating Piper?\nWe literally fell in love with [Gulp.js](http://gulpjs.com/), which is a task automater that is famous for simplifying your front process, like minifying, processing files, ... We searched to hopefuly find a repository for using pipes in PHP but did not find anything that fit our needs! So we wanted to made it for you guys :)\n\n## How to install it without Composer?\nWorry no more, Piper-PHP got you covered. Simply copy the content of the file `/src/piper.php`, removes the `namespace Khalyomede;` and include this file in your project. You are good to go!\n\n## How to install it using Composer?\nRun the following command in your project folder:\n```bash\n$ composer require khalyomede/piper-php \u0026\u0026 composer update\n```\n\n## Examples\nAll the examples will assume you have the following arborescence:\n```\n/\nindex.php\nvendor/\ncomposer.json\ncomposer.lock\n```\n\n## Example 1: using Piper PHP Community class\n_this example features hypotetical classes to illustrate this example_\n\nDownload the Piper class of another friend (hypotetical class). Execute this command in your project folder:\n```bash\ncomposer require someone/piper-array-average \u0026\u0026 composer update\n```\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Khalyomede\\Piper;\nuse Someone\\PiperArrayAverage as ArrayAverage;\n\nPiper::set([5, 17, 12, 14, 9])\n  -\u003epipe( ArrayAverage::do() )\n  -\u003eecho();\n```\nThis will print:\n```bash\n11.4\n```\n\n## Example 2: using custom function\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Khalyomede\\Piper;\n\nfunction uppercase( $input ) {\n  return strtoupper( $input );\n}\n\nPiper::set('text')\n  -\u003epipe('uppercase')\n  -\u003eecho();\n```\nThis will print:\n```bash\nTEXT\n```\n\n## Example 3: mixing custom functions, PHP functions and Piper Comunity class\n_this example features hyptotetical classes to illustrate this example_\n\nDownload the Piper class of another friend (hypotetical class). Execute this command in your project folder:\n```bash\ncomposer require someone/piper-array-average \u0026\u0026 composer update\n```\nNow let the fun begin:\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Khalyomede\\Piper;\nuse Someone\\PiperArrayAverage as ArrayAverage;\n\nfunction convertInt( $input ) {\n  return (int) $input;\n}\n\nPiper::set([5, 17, 12, 14, 9])\n  -\u003epipe( ArrayAverage::do() )\n  -\u003epipe( 'convertInt' )\n  -\u003epipe( function() { return Piper::input() + 5; } )\n  -\u003eecho();\n```\nThis will print :\n```bash\n16\n```\n\n## Example 4: without initial `set` method\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Khalyomede\\Piper;\n\nPiper::pipe( function() { return 'test' } )\n  -\u003epipe('strtoupper')\n  -\u003eecho();\n```\nThis will print:\n```\nTEST\n```\n\n## Example 5: using `get` instead of `echo`\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\n$uppercase = Piper::set('test')\n  -\u003epipe( 'strtoupper' )\n  -\u003eget();\n\necho $uppercase;\n```\nThis will print:\n```bash\nTEST\n```\n## Example 6: using `Piper::input()` outside a pipeline\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Khalyomede\\Piper;\n\nfunction addOne() {\n  return Piper::input() + 1; // Possible via static property (works like a Javascript's Promise)\n}\n\nPiper::set(2)\n  -\u003epipe('addOne')\n  -\u003eecho();\n```\nThis will print:\n```bash\n3\n```\n## Example 7: inserting \"utilities\" inside a pipe logic\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Khalyomede\\Piper;\n\nPiper::set('TEST')\n  -\u003epipe('strtolower') // all lowercase\n  -\u003eecho()\n  -\u003epipe('ucfirst') // only first character uppercase\n  -\u003eecho()\n  -\u003eeol()\n  -\u003eecho()\n  -\u003eprint_r();\n```\nThis will print:\n```bash\ntestTest\nTest\nTest\n```\n## Available Piper methods\n- `mixed Piper::input()` : returns the input of the last `Piper::pipe()` or `Piper::set()`. In other terms, get the last item of you Piper chain.\n- `void Piper::set( $variable )` : set the input (available via `Piper::input()`) with the variable. It can be any variable possible.\n- `void Piper::pipe( callable $function )` : use a callback function to be applied to the input (available via `Piper::input()`). See example above.\n- `void Piper::pipe( PiperContract $class )` : use a class that implements the `PiperContract` interface (see Build my Piper class below).\n- `void Piper::pipe( callable $string )` : use a PHP function or a previously created function by you to be called on the input (available via `Piper::input()`).\n- `void Piper::echo()` : echo the last input (available via `Piper::input()`).\n- `mixed Piper::get()` : returns the last input (available via `Piper::input()`).\n- `mixed Piper::$input` : get the public static property representing the input.\n\n## Build my Piper class\nFollow this steps to be up and runing with your freshly Piper Comunity class and help people do less and better.\n\n### Guidelines for Piper Comunity class\nWe will give you some tips to do the cleanest Piper Comunity class possible:\n#### 1 Use Piper before your package name\nIf you deal with array average, instead of `me/array-average` instead use `me/piper-array-average`. This will improve your SEO and help the comunity to see which composer package deals with Piper.\n#### 2. Add \"-php\" in at the end of your package name\nYou might want to be clear that this github package (and soon this Packagist library) will be available only for PHP developpers so you might want to write `me/piper-array-average-php` to clearly set the goal of this project.\n#### 3. Use kebab case for package name\nInstead of naming your package `me/piperArrayAveragePHP`, prefer using `me/piper-array-average-php` for a better readability.\n#### 4. Use an uppercase and camel case for your class name and file name\nA good class name begins with an uppercase letter, and no dashes. An example of good class name would be:\n```php\nclass PiperArrayAverage {}\n```\n#### 5. Use camel case for you class file\nIf we assume you put your class in folder `src/`, prefer using `src/piperArrayAverage.php` instead of `src/piperarrayaverage.php`. This will improves the readability.\n#### 6. Precise you are dealing with PHP in your package description\nLet the users know, since this description might be visible from Google search results.\n#### 7. Use PHP pre-requisit \u003e= 5.3.0\nSet up your `composer.json` `require` attribute to at least `5.3.0` as the classes will use namespaces. For example, you `composer.json` could looks like this:\n```json\n{\n  \"name\": \"me/piper-array-average\",\n  \"description\": \"Piper Comunity class that returns the average of numeric values in an array.\",\n  \"type\": \"library\",\n  \"license\": \"MIT\",\n  \"minimum-stability\": \"stable\",\n  \"require\": {\n    \"php\": \"\u003e=5.3.0\"\n  },\n  \"autoload\": {\n    \"psr-4\": {\n  \t \"Me\\\\\": \"src/\"\n  \t}\n  }\n}\n```\n#### 8. Name your example file `getting-started.php`\nThis will let the user instantly know that it can rely on this file to learn more on the usage of your class.\n#### 9. Prefer example instead of long descriptive method text\nUsers want to get quicly started and, mostly on the web, will cease to read if the description gets too long or too broad. Prefer short and explicit example than short descriptives text.\n## Example step-by-step of how to build a Piper Comunity class\nFirst, let us create a folder whenever you need. Let us name it \"piper-add\" :\n```bash\nmkdir piper-add\n```\nWe will enter into this folder :\n```bash\ncd piper-add\n```\nNext thing, we will initiate Git repository :\n```bash\ngit init\n```\nIt is more convenient to do a Git init before a Composer init as Composer will then propose us to exclude `vendor` folder from every of your git push.\n\nSo naturally, the next thing you will want to do a initiate Composer :\n```bash\ncomposer init\n```\nOnce you filled all the question in the prompt command, Composer will build a `composer.json` file.\n\nNow, we will create the folder `src` that will contains `piperAdd.php` file, your next big community project...\n\nOnce you finished this last step, create another folder that will let us try our code. Create a `test` folder with a `test-pipe.php` file.\n\nYou will need to notify Composer that those files exists. To do so, update the `composer.json` file at the root of your folder, and add the following lines :\n```json\n...\n\"require\": {\n\t\"php\": \"\u003e=5.3.0\"\t\n},\n...\n\"autoload\": {\n\t\"psr-4\": {\n\t\t\"You\\\\\": \"src/\"\n\t}\n}\n```\nSo your final `composer.json` file should look like this :\n```json\n{\n    \"name\": \"you/piper-add\",\n    \"description\": \"Piper Comunity class that let you chain addition on your workflow.\",\n    \"type\": \"library\",\n    \"license\": \"MIT\",\n    \"minimum-stability\": \"stable\",\n    \"require\": {\n    \t\"php\": \"\u003e=5.3.0\"\n    },\n    \"autoload\": {\n    \t\"psr-4\": {\n    \t\t\"You\\\\\": \"src/\"\n    \t}\n    }\n}\n```\n*IMPORTANT* Your `minimum-stability` should be set to `stable` to use this library. We will make an effort trying to figure out which composition of requirement is the best for you and us but for the moment if you would like to work with Piper you will need to set it to `stable`.\n\nNow, in your project folder, use the command line again and type :\n```bash\ncomposer update\n```\nA new folder should have been created : `vendor`. Besides this folder, another file should also have been created : `composer.lock`, but do not mind (and do not update it).\n\nIt is time to begin the fun : writing your logic. Go to your file `src/piperAdd.php`, put PHP opening tags `\u003c?php`, and add :\n```php\nnamespace You;\n```\nThis line will let the users import your class in their own project. This is why the autoloader needs this line (according to the name you set in `composer.json` at the `autoload` attribute).\n\nThen add your class (empty) :\n```php\nclass PiperAdd {\n\t\n}\n```\nOn thing to know is, to works correctly, Piper needs you to implement the `PiperContract` class. An interface is like a contract that you pass between us (Me and Aminnairi, creators of the library) and you. We will not give you a salary (unfortunately), but it is more like a moral contract : you agree that your class you are going to build should an must include 2 important methods : `public static function go( $parameter ) {}` and `public static function execute( $input ) {}`. \n\n`do` method is called when one of our friend use your class to pipe its logic. For example :\n```php\nuse Khalyomede\\Piper;\nuse You\\PiperAdd as Add;\n\nPiper::set(1)\n\t-\u003epipe( Add::do(3) )\n\t-\u003eecho();\n// echo \"4\"\n```\nBut contrary as you could think, `do` does not handle the logic ! It must indeed only returns an instance of your class. We need it to make Piper works well, please trust us ;).\n\nSo, you will see a large majority of Piper Comunity class will have barely the same code :\n```php\nclass PiperAdd {\n  public static $parameter = 0;\n\n  public static function do( $parameter ) {\n    self::$parameter = $parameter;\n\n    return new self;\t\n  }\n}\n```\nThen, your logic will be located inside `execute` method. This is where you can have fun :\n```php\nclass PiperAdd {\n  public static $parameter = 0;\n\n  public static function execute( $input ) {\n    return self::$parameter + $input; // The addition is here\n  }\n\n  public static function do( $parameter ) {\n    self::$parameter = $parameter;\n\n    return new self;\n  }\n}\n```\nLike you can see, you will need to trust us again, and assume `$input` parameter of the method `execute` will be our `Piper::input()`, in other terms, will be the input of the last `::set()` or `::pipe()`. This two methods ensure the piping logic. \n\nOf course, you can tweak this base class to add as many other methods as you like, but this two methods should be still present to works correctly. If it is not the case, `PiperContract` will throw errors.\n\nHey, but where is this dear PiperContract ?? Dang ! Let fix it :\n```php\nuse Khalyomede\\PiperContract;\n\nclass PiperAdd {\n  public static $parameter = 0;\n\n  public static function execute( $input ) {\n    return self::$parameter + $input; // The addition is here\n  }\n\n  public static function do( $parameter ) {\n    self::$parameter = $parameter;\n\n    return new self;\n  }\n}\n```\nThis is better. But now, as you require a dependency, you need to import it via Packagist.org. Use your command line in your project folder, and do :\n```bash\ncomposer require khalyomede/piper-contract-php\n```\nThe full result for the `src/piper-add.php` file should look like this :\n```php\nnamespace You;\n\nuse Khalyomede\\PiperContract;\n\nclass PiperAdd {\n  public static $parameter = 0;\n\n  public static function execute( $input ) {\n    return self::$parameter + $input; // The addition is here\n  }\n\n  public static function do( $parameter ) {\n    self::$parameter = $parameter;\n\n    return new self;\n  }\n}\n```\nIt is now time to try our class. To do so, create a file `test/test-pipe.php` containing :\n```php\nrequire __DIR__ '/../vendor/autoload.php';\n\nuse Khalyomede\\Piper;\nuse You\\PiperAdd;\n\nPiper::set(1)\n  -\u003epipe( Add::do(2) )\n  -\u003eecho();\n```\nIf you test this file, the Composer autoloader will tell you that it does not know `Khalyomede\\Piper`. To fix it, you need to import it from Packagist.org using :\n```\ncomposer require Khalyomede\\Piper --dev\n```\nNoticed the `--dev` ? It is because your developer friend will already use Piper as a dependencies, so you do not want to overload your library with useless dependencies (not that Piper is useless, but you know a little in production for a custom library... Oh my god what I have said... Bare with me I slept 3 hours today).\n\nNow you should be up and runing for your first test. Let us try it out. In your folder, open a command line if you did not and type :\n```bash\nphp test/test-pipe.php\n```\nYou should see in output :\n```\n3\n```\nYou see this result because you make an addition of the set variable (1) and your Pipe Comunity class `Add` that add the parameter (2) to the input (1).\n\nIf you see any issue or something not running correctly as mentionned in this tutorial, please fill up an Issue and we will try our best to resolve it.\n\nCongratulations. Have fun with it, we hope we can build together a better PHP developpement network that make us be more efficient with this pipe-oriented approach and avoid us re-programming similar logic !\n## Need an example without parameters ?\nCheck [`Piper Array Average`](https://github.com/khalyomede/piper-array-average-php) Comunity class (PHP) and dive into the code to know how to quickly remove the needs of a parameter.\n## Need an example with shared properties ?\nPiperPHP seems to make shared variables like the one you share in a PDO logic complicated ? Not at all ! Using simple globals variable logic you can build powerful single-connection PDO logic (or any other object than PDO). With the use of defined conventions for naming your global Piper make this a breeze (for the end developper). Check [`Piper Pdo`](https://github.com/khalyomede/piper-pdo-php) to learn how to build this kind of advanced comunity class.\n## Need more ?\nFeel free to do a Pull Request and let us know which feature you would like to see the most.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Fpiper-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhalyomede%2Fpiper-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Fpiper-php/lists"}