PHP code example of gameplayjdk / task-chain

1. Go to this page and download the library: Download gameplayjdk/task-chain 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/ */

    

gameplayjdk / task-chain example snippets




askChain\TaskChain;
use TaskChain\TaskInterface;

$chain = new TaskChain();

/**
 * Class TaskAdd
 */
class TaskAdd implements TaskInterface {

    /**
     * @var int
     */
    private $number;

    /**
     * TaskAdd constructor.
     * @param int $number
     */
    public function __construct(int $number)
    {
        $this->number = $number;
    }

    /**
     * @inheritDoc
     */
    public function accept($input): bool
    {
        return is_int($input);
    }

    /**
     * @inheritDoc
     */
    public function process($input, callable $nextCallback)
    {
        echo print_r($this, true);
        echo PHP_EOL;

        return $nextCallback($input + $this->number);
    }
}

/**
 * Class TaskMul
 */
class TaskMul implements TaskInterface {

    /**
     * @var int
     */
    private $number;

    /**
     * TaskMul constructor.
     * @param int $number
     */
    public function __construct(int $number)
    {
        $this->number = $number;
    }

    /**
     * @inheritDoc
     */
    public function accept($input): bool
    {
        return is_int($input);
    }

    /**
     * @inheritDoc
     */
    public function process($input, callable $nextCallback)
    {
        echo print_r($this, true);
        echo PHP_EOL;

        return $nextCallback($input * $this->number);
    }
}

// Add "1", priority 2.
$chain->add(new TaskAdd(1), 2);
// Multiply by "2", priority 3.
$chain->add(new TaskMul(2), 3);

// Once a chain is ran, the priority information gets lost (with the built-in implementation).
echo $chain->run(1);
echo PHP_EOL;