PHP code example of sebastiaanluca / php-pipe-operator

1. Go to this page and download the library: Download sebastiaanluca/php-pipe-operator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sebastiaanluca / php-pipe-operator example snippets


use SebastiaanLuca\PipeOperator\Pipe;

Pipe::from('hello')->strtoupper()->get();

// "HELLO"

take('hello')->strtoupper()->get();

// "HELLO"

pipe('hello')->strtoupper()->get();

// "HELLO"

$subdomain = Pipe::from('https://blog.sebastiaanluca.com')
    ->parse_url()
    ->end()
    ->explode('.', PIPED_VALUE)
    ->reset()
    ->get();

// "blog"

Pipe::from('hello')->strtoupper()->get();

// "HELLO"

Pipe::from('hello')->strtoupper(PIPED_VALUE)->get();

// "HELLO"

Pipe::from(['key' => 'value'])
    ->array_search('value', PIPED_VALUE)
    ->get();

// "key"

take('STRING')
    ->pipe(strtolower(...))
    ->get()

// "string"

Pipe::from('https://sebastiaanluca.com/blog')
    ->pipe(parse_url(...))
    ->end()
    ->pipe(substr(...), PIPED_VALUE, 3)
    ->pipe(strtoupper(...))
    ->get(),

// "OG"

Pipe::from('string')
    ->pipe(fn(string $value): string => 'prefixed-' . $value)
    ->get();

// "prefixed-string"

class MyClass
{
    public function __construct()
    {
        Pipe::from('HELLO')
            ->pipe($this)->lowercase()
            ->get();

        // "hello"
    }

    /**
     * @param string $value
     *
     * @return string
     */
    private function lowercase(string $value) : string
    {
        return mb_strtolower($value);
    }
}

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);
    }
}

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);
    }
}

use Closure;

class MyClass
{
    public function __construct()
    {
        Pipe::from('HELLO')
            ->pipe(Closure::fromCallable([$this, 'lowercase']))
            ->get();

        // "hello"
    }

    /**
     * @param string $value
     *
     * @return string
     */
    private function lowercase(string $value) : string
    {
        return mb_strtolower($value);
    }
}

$subdomain = 'https://blog.sebastiaanluca.com/';
$subdomain = parse_url($subdomain, PHP_URL_HOST);
$subdomain = explode('.', $subdomain);
$subdomain = reset($subdomain);

// "blog"

$subdomain = explode('.', parse_url('https://blog.sebastiaanluca.com/', PHP_URL_HOST))[0];

// "blog"
bash
composer