The only way to implement the pipe operator in PHP.

Overview

Pipe Operator in PHP

Latest Version on Packagist Total Downloads GitHub
Total Downloads

Introduction

This package is based on the pipe operator RFC by Sara Golemon and Marcelo Camargo (2016), who explains the problem as:

A common PHP OOP pattern is the use of method chaining, or what is also known as “Fluent Expressions”. […] This works well enough for OOP classes which were designed for fluent calling, however it is impossible, or at least unnecessarily arduous, to adapt non-fluent classes to this usage style, harder still for functional interfaces.

Short example

Say you want to get the subdomain from a URL, you may get quite verbose and repetitive code like this:

$result = 'https://blog.github.com';
$result = parse_url($result);
$result = end($result);
$result = explode('.', $result);
$result = reset($result);

// blog

More examples

See the RFC by Golemon and Marcelo Camargo for more complex and real-world examples.

Installation

You can install the package via Composer:

composer require boostphp/pipe-operator

Usage

You could use the package like this:

use Boost\PipeOperator\PipeOperator;

$result = PipeOperator::from('https://blog.github.com')
    ->parse_url()
    ->end()
    ->explode('.', PIPED_VALUE)
    ->reset()
    ->get();

// blog

Using closures

You could also use closures for more flexibility:

use Boost\PipeOperator\PipeOperator;

$result = PipeOperator::from('https://blog.github.com')
    ->pipe(fn ($value) => md5($value))
    ->pipe(fn ($value) => sprintf('prefixed-%s', $value))
    ->get();

// prefixed-740b375fe175853f9d4c5635194daf84

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Provides simple interfaces to implement a webhook-based tweeting system

webhook-tweeter This package aims to provide simple interfaces to implement a webhook-based tweeting system. This can, for example, be used to tweet a

This Pocketmine-MP plugin lets you implement the ultimate birthday wishing system on your server.

BirthdaysPE This Pocketmine-MP plugin will let you wish player(s) a happy birthday and notify others to wish them too. Commands /birthday set/reset

Implement a
Implement a "Where's Wally" (or Waldo/Charlie) Captcha in a simple Symfony Project

Where's Wally ? Nowadays, we are often confronted with "Captcha". These tests to know if we are robots or not. They are all very boring and not very f

A Simplistic Plugin to Implement Server Claims to your Minecraft: Bedrock Server.
A Simplistic Plugin to Implement Server Claims to your Minecraft: Bedrock Server.

Claims This plugin allows administrators to create, edit, list, and teleport to land claims on a PocketMine server. These claims have a variety of cus

A super simple, clean and pretty error handler that replace the default error handler of PHP. You need only include this file!

php-custom-error-handler A super simple, clean and pretty error handler that replace the default error handler of PHP. You need just include only this

Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm

Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 Ip2region特性

[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

Humanize values that are readable only for developers.

PHP Humanizer Tests - 4.x Readme for 4.x version Humanize values to make them readable for regular people ;) Installation Run the following command: c

Enforce that your classes get only instantiated by the factories you define!

Enforce that your classes get only instantiated by the factories you define!

Releases(v1.2.1)
Owner
BoostPHP
BoostPHP
YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify that a number is indeed a number of an operator X

phone-number-checker This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify t

faso-dev 4 Feb 7, 2022
PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule for validation Japanese string only full-width or only half-width.

Japanese String Helpers PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule f

Deha 54 Mar 22, 2022
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

null 2 Nov 20, 2021
YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic. It is built based on the combination of ideas from the Yii framework and Laravel framework (yl).

Tan Nguyen 3 Jan 3, 2023
a simple pastebin implement in php

alcohol/pastebin-php I mostly use this demo application to keep myself up to date with the various changes introduced by new (major) Symfony releases.

Rob 26 Nov 5, 2022
Simple class that implement a CAPTCHA for your PHP App.

simple-captcha Simple class that implement a CAPTCHA for your PHP App. Installation Use the package manager composer to install. composer require will

WILLIAM B. SAMPAIO 3 Nov 9, 2022
Here are few exercises to practice how to implement API Security with NGINX App-Protect WAF.

api-security-lab This repo contains files for customers and partners to practice an API Security with NGINX App-Protect WAF. To demonstrate the capabi

null 4 Mar 30, 2022
A light-weight RPC implement of google protobuf RPC framework.

sofa-pbrpc A light-weight RPC implementation of Google's protobuf RPC framework. Wiki: https://github.com/baidu/sofa-pbrpc/wiki Features High performa

Baidu 2.1k Dec 10, 2022
Implement event systems, signal slots, intercepting filters, and observers.

zend-eventmanager Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-eventmanager. zend-eventmanager is designed for the fol

Zend Framework 1.7k Dec 9, 2022