PHP code example of liyuze / method-chaining-proxy

1. Go to this page and download the library: Download liyuze/method-chaining-proxy 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/ */

    

liyuze / method-chaining-proxy example snippets

 
/**
 * Call the given Closure with the given value then return the value.
 *
 * @param  mixed  $value
 * @param  callable|null  $callback
 * @return mixed
 */
function tap($value, $callback = null)
{
    if (is_null($callback)) {
        return new HigherOrderTapProxy($value);
    }

    $callback($value);

    return $value;
}

namespace Illuminate\Support\Traits;

trait EnumeratesValues
{
    ...
    /**
     * Pass the collection to the given callback and return the result.
     *
     * @param  callable  $callback
     * @return mixed
     */
    public function pipe(callable $callback)
    {
        return $callback($this);
    }
    ...
}


$cat = new Cat('Tom', 5);
$proxy = MethodChainingFactory::create($cat);
//或
$proxy = MethodChainingFactory::mixed($cat);

$proxy = $proxy->setAge(9)->setName('Tony')->getName();
$proxy->popValue(); //Tony
$cat->getName();    //Tony

$cat = new Cat('Tom', 5);
$proxy = MethodChainingFactory::tap($cat);
$proxy = $proxy->setAge(9)->setName('Tony')->getName();
$proxy->popValue(); //Cat('Tony', 9)
$cat->getName();    //Tony

$cat = new Cat('Tom', 5);
$proxy = MethodChainingFactory::pipe($cat);
$proxy = $proxy->setAge(9);
$proxy->popValue(); //null
$cat->getName();    //Tony

$cat = new Cat('Tom', 5);
$proxy = MethodChainingFactory::tap($cat);
$proxy = $proxy->switchToPipeMode()->getName();;
$proxy->popValue(); //Tom
$cat->getName();    //Tom

$cat = new Cat('Tom', 5);
$proxy = MethodChainingFactory::tap($cat);
$proxy = $proxy->pipeOnce()->getName()
$proxy->popValue(); //Tom
$cat->getName();    //Tom

$cat = new Cat('Tom', 5);
$name = null;
$proxy = MethodChainingFactory::create($cat);
$proxy->setAge(9)->pick('name', $name)->setName('Tony');
$name; //Tom
$cat->getName();    //Tony

$cat = new Cat('Tom', 5);
$name = null;
$proxy = MethodChainingFactory::create($cat);
$proxy = $proxy->setAge(9)->methodPick($name, 'getName')->setName('Tony');;
$name; //Tom
$cat->getName();    //Tony

$cat = new Cat('Tom', 5);
$birthMonth = 3;
$proxy = MethodChainingFactory::create($cat);
$proxy = $proxy->setAge(9)->setName('Tony')->after(
    function ($proxyValue) use ($birthMonth) {
        //6月前出生的加1岁
        if ($birthMonth < 6) {
            $proxyValue->setAge($proxyValue->getAge() + 1);
        }
    });//->after(...)->after(...);
    
$proxy->popValue()->getAge();   // 10


$number = MethodChainingFactory::create($cat)->after(fn () => 3)->popValue();
// 3

$ifProxy = ControlChainingFactory::if(new Cat('Tom', 5), false);
// 或 $ifProxy = new IfChainingProxy(new Cat('Tom', 5), false);
$ifProxy->setName('Tony')
    ->elseChaining()
    ->setName('Alan');

$ifProxy->endSwitchChaining()->getName();   //Alan

$switchProxy = ControlChainingFactory::switch(new Cat('Tom', 5), 2);
// 或 $switchProxy = new SwitchChainingProxy(new Cat('Tom', 5), 2);
$cat = $switchProxy
    ->caseChaining(1)->setName('Tony')
    ->caseChaining(2)->setName('Alan')
    ->endSwitchChaining();
        
$cat->getName();   //Alan
$cat->getAge();   //10

 $switchProxy = ControlChainingFactory::switch(new Cat('Tom', 5), 2);
// 或 $switchProxy = new SwitchChainingProxy(new Cat('Tom', 5), 2);
$cat = $switchProxy
    ->caseChaining(1)->setName('Tony')->breakChaining()
    ->caseChaining(2)->setName('Alan')
    ->caseChaining(2)->setAge(10)->breakChaining()
    ->caseChaining(2)->setName('Andy')
    ->endSwitchChaining();
    
$cat->getName();   //Alan
$cat->getAge();   //10

 $switchProxy = ControlChainingFactory::switch(new Cat('Tom', 5), 2);
// 或 $switchProxy = new SwitchChainingProxy(new Cat('Tom', 5), 2);
$cat = $switchProxy
    ->caseChaining(1)->setName('Tony')->breakChaining()
    ->defaultChaining()->setName('Alan')->setAge(10)
    ->endSwitchChaining();
    
$cat->getName();   //Alan
$cat->getAge();   //10

class Cat
{
    public string $name;
    public int $age;

    public function __construct(string $name, int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getAge()
    {
        return $this->age;
    }

    public function setName(string $name)
    {
        $this->name = $name;
    }

    public function setAge(int $age)
    {
        $this->age = $age;
    }
    
    public function setNameForChaining(string $name)
    {
        $this->name = $name;
        return $this;
    }

    public function setAgeForChaining(int $age)
    {
        $this->age = $age;
        return $this;
    }

    public function do(\Closure $closure)
    {
        $closure();

        return $this;
    }
}